diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2022-11-06 13:52:06 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-06 13:52:06 +0000 |
commit | 99e2e60cb25bfcf77ba1386d331cfa85864e6a65 (patch) | |
tree | 9aec45b4196922f64b17f82a52761514347c8d25 /Lib/traceback.py | |
parent | a0bc75e2fdd53680cb147881bcb3754bd56aa2fa (diff) | |
download | cpython-99e2e60cb25bfcf77ba1386d331cfa85864e6a65.tar.gz cpython-99e2e60cb25bfcf77ba1386d331cfa85864e6a65.zip |
gh-99139: Improve NameError error suggestion for instances (#99140)
Diffstat (limited to 'Lib/traceback.py')
-rw-r--r-- | Lib/traceback.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/Lib/traceback.py b/Lib/traceback.py index cf5f355ff04..8d518728fa1 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1037,6 +1037,16 @@ def _compute_suggestion_error(exc_value, tb, wrong_name): + list(frame.f_globals) + list(frame.f_builtins) ) + + # Check first if we are in a method and the instance + # has the wrong name as attribute + if 'self' in frame.f_locals: + self = frame.f_locals['self'] + if hasattr(self, wrong_name): + return f"self.{wrong_name}" + + # Compute closest match + if len(d) > _MAX_CANDIDATE_ITEMS: return None wrong_name_len = len(wrong_name) |