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

Provide NameError/AttributeError suggestions in traceback.py #97008

Closed
ambv opened this issue Sep 22, 2022 · 6 comments
Closed

Provide NameError/AttributeError suggestions in traceback.py #97008

ambv opened this issue Sep 22, 2022 · 6 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@ambv
Copy link
Contributor

ambv commented Sep 22, 2022

Python 3.10 added NameError and AttributeError suggestions in raised exceptions, in the form of:

>>> dim
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'dim' is not defined. Did you mean: 'dir'?

This is implemented in Python/suggestions.c and unavailable in the pure Python implementation of traceback formatting in Lib/traceback.py. This makes the output of both different, which is problematic for tooling using traceback.py, as well as for alternative implementations of Python like PyPy and GraalPy.

Let's implement the equivalent of suggestions.c in Lib/traceback.py to bring both in line.

@ambv ambv added type-bug An unexpected behavior, bug, or error type-feature A feature request or enhancement stdlib Python modules in the Lib dir and removed type-bug An unexpected behavior, bug, or error labels Sep 22, 2022
@ambv
Copy link
Contributor Author

ambv commented Sep 22, 2022

cc @cfbolz

@sweeneyde
Copy link
Member

I'm not sure if exact equivalence with suggestions.c is required, but if not, IIRC there was at one point a call to use difflib's get_close_matches(), which might simplify things.

ambv added a commit to ambv/cpython that referenced this issue Sep 22, 2022
…eError suggestions

Relevant tests moved from test_exceptions to test_traceback to be able to
compare both implementations.

Co-authored-by: Carl Friedrich Bolz-Tereick <cfbolz@gmx.de>
@cfbolz
Copy link
Contributor

cfbolz commented Sep 22, 2022

@sweeneyde the plan is to start from PyPy's implementation, @ambv and I started working on that today (it's a port of Pablo's original commit, at the time I didn't your later changes hadn't landed).

@gpshead
Copy link
Member

gpshead commented Sep 26, 2022

cc @Yhg1s

ambv added a commit that referenced this issue Oct 4, 2022
… suggestions (#97022)

Relevant tests moved from test_exceptions to test_traceback to be able to
compare both implementations.

Co-authored-by: Carl Friedrich Bolz-Tereick <cfbolz@gmx.de>
@ambv
Copy link
Contributor Author

ambv commented Oct 4, 2022

Fixed!

@ambv ambv closed this as completed Oct 4, 2022
@cfbolz
Copy link
Contributor

cfbolz commented Oct 5, 2022

wonderful, thank you @ambv, @pablogsal and @sweeneyde 😊

mpage pushed a commit to mpage/cpython that referenced this issue Oct 11, 2022
…eError suggestions (python#97022)

Relevant tests moved from test_exceptions to test_traceback to be able to
compare both implementations.

Co-authored-by: Carl Friedrich Bolz-Tereick <cfbolz@gmx.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants