Show suggestions in error messages in Python 3.10#13450
Conversation
e8f457c to
b3aeaaa
Compare
|
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. |
|
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). |
|
Furthermore, >>> 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))
[] |
|
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 |
|
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. |
Fixes #13445