Skip to content

bpo-44655: Don't include suggestions for attributes that are the same as the missing one #27197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions Python/suggestions.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ calculate_suggestions(PyObject *dir,
if (item_str == NULL) {
return NULL;
}
if (PyUnicode_CompareWithASCIIString(name, item_str) == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can only be used if the second argument is ASCII string. item_str is UTF-8 string. The behavior in such case is not defined.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #106025

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.
Expand Down