Skip to content

Commit a0b1d40

Browse files
bpo-44655: Don't include suggestions for attributes that are the same as the missing one (pythonGH-27197) (pythonGH-27198)
(cherry picked from commit 6714dec) Co-authored-by: Pablo Galindo Salgado <[email protected]>
1 parent 42a5514 commit a0b1d40

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Lib/test/test_exceptions.py

Lines changed: 12 additions & 0 deletions
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
Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ calculate_suggestions(PyObject *dir,
149149
if (item_str == NULL) {
150150
return NULL;
151151
}
152+
if (PyUnicode_CompareWithASCIIString(name, item_str) == 0) {
153+
continue;
154+
}
152155
// No more than 1/3 of the involved characters should need changed.
153156
Py_ssize_t max_distance = (name_size + item_size + 3) * MOVE_COST / 6;
154157
// Don't take matches we've already beaten.

0 commit comments

Comments
 (0)