Skip to content
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

Show suggestions in error messages in Python 3.10 #13450

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

tirkarthi
Copy link
Contributor

Fixes #13445

@Carreau
Copy link
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

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

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
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

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