mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-99139: Improve NameError error suggestion for instances (#99140)
This commit is contained in:
parent
a0bc75e2fd
commit
99e2e60cb2
7 changed files with 92 additions and 0 deletions
|
@ -75,6 +75,27 @@ Important deprecations, removals or restrictions:
|
|||
Improved Error Messages
|
||||
=======================
|
||||
|
||||
* Improve the error suggestion for :exc:`NameError` exceptions for instances.
|
||||
Now if a :exc:`NameError` is raised in a method and the instance has an
|
||||
attribute that's exactly equal to the name in the exception, the suggestion
|
||||
will include ``self.<NAME>`` instead of the closest match in the method
|
||||
scope. Contributed by Pablo Galindo in :gh:`99139`.
|
||||
|
||||
>>> class A:
|
||||
... def __init__(self):
|
||||
... self.blech = 1
|
||||
...
|
||||
... def foo(self):
|
||||
... somethin = blech
|
||||
|
||||
>>> A().foo()
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1
|
||||
somethin = blech
|
||||
^^^^^
|
||||
NameError: name 'blech' is not defined. Did you mean: 'self.blech'?
|
||||
|
||||
|
||||
* Improve the :exc:`SyntaxError` error message when the user types ``import x
|
||||
from y`` instead of ``from y import x``. Contributed by Pablo Galindo in :gh:`98931`.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue