Skip to content

Commit 6714dec

Browse files
authored
bpo-44655: Don't include suggestions for attributes that are the same as the missing one (GH-27197)
1 parent c90c591 commit 6714dec

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Lib/test/test_exceptions.py

+12
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,18 @@ def __getattr__(self, attr):
19161916

19171917
self.assertIn("blech", err.getvalue())
19181918

1919+
def test_getattr_suggestions_for_same_name(self):
1920+
class A:
1921+
def __dir__(self):
1922+
return ['blech']
1923+
try:
1924+
A().blech
1925+
except AttributeError as exc:
1926+
with support.captured_stderr() as err:
1927+
sys.__excepthook__(*sys.exc_info())
1928+
1929+
self.assertNotIn("Did you mean", err.getvalue())
1930+
19191931
def test_attribute_error_with_failing_dict(self):
19201932
class T:
19211933
bluch = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Don't include a missing attribute with the same name as the failing one when
2+
offering suggestions for missing attributes. Patch by Pablo Galindo

Python/suggestions.c

+3
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ calculate_suggestions(PyObject *dir,
151151
if (item_str == NULL) {
152152
return NULL;
153153
}
154+
if (PyUnicode_CompareWithASCIIString(name, item_str) == 0) {
155+
continue;
156+
}
154157
// No more than 1/3 of the involved characters should need changed.
155158
Py_ssize_t max_distance = (name_size + item_size + 3) * MOVE_COST / 6;
156159
// Don't take matches we've already beaten.

0 commit comments

Comments
 (0)