Skip to content

gh-116871: Improve name suggestions in tracebacks #116930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Mar 17, 2024

Only include underscored names in name suggestions for AttributeError and ImportError if the original name was underscored.

Only include underscored names in name suggestions for AttributeError and
ImportError if the original name was underscored.
Lib/traceback.py Outdated
@@ -1472,12 +1472,16 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
obj = exc_value.obj
try:
d = dir(obj)
if wrong_name[:1] != '_':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you're using [:1] instead of [0] to handle empty strings. But there are no tests for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems difficult to create tests for this with the current code.

If write wrong_name[0], an IndexError raised for empty wrong_name will be caught by except Exception. If write wrong_name[:1], then any non-empty names will be filtered out in the code below as too different from empty string. In any case the result is the same -- no suggestions.

@Eclips4 Eclips4 changed the title gh-116871: Imporove name suggestions in tracebacks gh-116871: Improve name suggestions in tracebacks Mar 17, 2024
@gaogaotiantian
Copy link
Member

Please take a look at the discussion before merging this. I think ImportError is fine because import is always external use, but AttributeError could be self._private_member which is a perfect valid usage (and arguably a common mistake that could benefit from the name suggestion).

@serhiy-storchaka serhiy-storchaka merged commit 0085c3a into python:main May 6, 2024
@serhiy-storchaka serhiy-storchaka deleted the traceback-underscored-name-suggestions branch May 6, 2024 12:53
SonicField pushed a commit to SonicField/cpython that referenced this pull request May 8, 2024
)

Only include underscored names in name suggestions for AttributeError and
ImportError if the original name was underscored.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants