[Python-checkins] bpo-44655: Don't include suggestions for attributes that are the same as the missing one (GH-27197) (GH-27198)

pablogsal webhook-mailer at python.org
Fri Jul 16 17:16:21 EDT 2021


https://github.com/python/cpython/commit/a0b1d401db52391d13479c53ee3880e6640df98c
commit: a0b1d401db52391d13479c53ee3880e6640df98c
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-07-16T22:16:08+01:00
summary:

bpo-44655: Don't include suggestions for attributes that are the same as the missing one (GH-27197) (GH-27198)

(cherry picked from commit 6714dec5e104bdee4a0ed4d9966de27d1bfa1e3d)

Co-authored-by: Pablo Galindo Salgado <Pablogsal at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2021-07-16-20-25-37.bpo-44655.I3wRjL.rst
M Lib/test/test_exceptions.py
M Python/suggestions.c

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index f92637f9930bf..a2bc62b72eed6 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -1916,6 +1916,18 @@ def __getattr__(self, attr):
 
             self.assertIn("blech", err.getvalue())
 
+    def test_getattr_suggestions_for_same_name(self):
+        class A:
+            def __dir__(self):
+                return ['blech']
+        try:
+            A().blech
+        except AttributeError as exc:
+            with support.captured_stderr() as err:
+                sys.__excepthook__(*sys.exc_info())
+
+        self.assertNotIn("Did you mean", err.getvalue())
+
     def test_attribute_error_with_failing_dict(self):
         class T:
             bluch = 1
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-16-20-25-37.bpo-44655.I3wRjL.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-16-20-25-37.bpo-44655.I3wRjL.rst
new file mode 100644
index 0000000000000..4ea4a6d085979
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-16-20-25-37.bpo-44655.I3wRjL.rst	
@@ -0,0 +1,2 @@
+Don't include a missing attribute with the same name as the failing one when
+offering suggestions for missing attributes. Patch by Pablo Galindo
diff --git a/Python/suggestions.c b/Python/suggestions.c
index 6fb01f10cd37c..7fd62fbfabf64 100644
--- a/Python/suggestions.c
+++ b/Python/suggestions.c
@@ -149,6 +149,9 @@ calculate_suggestions(PyObject *dir,
         if (item_str == NULL) {
             return NULL;
         }
+        if (PyUnicode_CompareWithASCIIString(name, item_str) == 0) {
+            continue;
+        }
         // No more than 1/3 of the involved characters should need changed.
         Py_ssize_t max_distance = (name_size + item_size + 3) * MOVE_COST / 6;
         // Don't take matches we've already beaten.



More information about the Python-checkins mailing list