Skip to content

Show suggestions in error messages in Python 3.10#13450

Closed
tirkarthi wants to merge 2 commits into
ipython:mainfrom
tirkarthi:show-suggestions
Closed

Show suggestions in error messages in Python 3.10#13450
tirkarthi wants to merge 2 commits into
ipython:mainfrom
tirkarthi:show-suggestions

Conversation

@tirkarthi
Copy link
Copy Markdown
Contributor

Fixes #13445

@Carreau
Copy link
Copy Markdown
Member

Carreau commented Jan 18, 2022

Hum , I think we might be able to get something better with

def structured_traceback(...):
    from difflib import get_close_matches
    get_close_matches(value.name, dir(value.obj)

And re-implemented the match logic (even if slightly different). That way we can properly highlight tokens.

@aroberge
Copy link
Copy Markdown

I believe that the suggestion to use difflib would only work for AttributeError cases but not for NameError (where the misspelled name can be found in the local or global scope).

@aroberge
Copy link
Copy Markdown

Furthermore, difflib gives different results from those given by cPython.

>>> import math
>>> math.Pi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'math' has no attribute 'Pi'. Did you mean: 'pi'?
>>> from difflib import get_close_matches
>>> get_close_matches('Pi', dir(math))
[]

@tirkarthi
Copy link
Copy Markdown
Contributor Author

CPython has done several tweaks to get better results. IMO, it's better to use the suggestions from the compiler itself to avoid any discrepancy and to avoid reimplementing/maintaining the algorithm.

python/cpython#25412
python/cpython#25443
python/cpython#25460
python/cpython#25584
python/cpython#25776

@Carreau
Copy link
Copy Markdown
Member

Carreau commented Feb 23, 2025

Closing old stale PRs, sorry if this was not addressed, but this has been a few years, so there are few chances of someone actually pushing this through.

Feel free to resubmit if you feel this has a chance to be revived.

@Carreau Carreau closed this Feb 23, 2025
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.

NameError/AttributeError suggestions in Python 3.10 error messages

3 participants