Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-143962: Improve name suggestions for not normalized names#144154

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall 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
46 changes: 46 additions & 0 deletionsLib/test/test_traceback.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4250,6 +4250,24 @@ def __dir__(self):
actual = self.get_suggestion(A(), 'blech')
self.assertNotIn("Did you mean", actual)

def test_suggestions_not_normalized(self):
class A:
analization = None
fiⁿₐˡᵢᶻₐᵗᵢᵒₙ = None

suggestion = self.get_suggestion(A(), 'fiⁿₐˡᵢᶻₐᵗᵢᵒₙ')
self.assertIn("'finalization'", suggestion)
self.assertNotIn("analization", suggestion)

class B:
attr_a = None
attr_µ = None # attr_\xb5

suggestion = self.get_suggestion(B(), 'attr_\xb5')
self.assertIn("'attr_\u03bc'", suggestion)
self.assertIn(r"'attr_\u03bc'", suggestion)
self.assertNotIn("attr_a", suggestion)


class GetattrSuggestionTests(BaseSuggestionTests):
def test_suggestions_no_args(self):
Expand DownExpand Up@@ -4872,6 +4890,34 @@ def foo(self):
actual = self.get_suggestion(instance.foo)
self.assertIn("self.blech", actual)

def test_name_error_with_instance_not_normalized(self):
class A:
def __init__(self):
self.fiⁿₐˡᵢᶻₐᵗᵢᵒₙ = None
def foo(self):
analization = 1
x = fiⁿₐˡᵢᶻₐᵗᵢᵒₙ

instance = A()
actual = self.get_suggestion(instance.foo)
self.assertIn("self.finalization", actual)
self.assertNotIn("fiⁿₐˡᵢᶻₐᵗᵢᵒₙ", actual)
self.assertNotIn("analization", actual)

class B:
def __init__(self):
self.attr_µ = None # attr_\xb5
def foo(self):
attr_a = 1
x = attr_µ # attr_\xb5

instance = B()
actual = self.get_suggestion(instance.foo)
self.assertIn("self.attr_\u03bc", actual)
self.assertIn(r"self.attr_\u03bc", actual)
self.assertNotIn("attr_\xb5", actual)
self.assertNotIn("attr_a", actual)

def test_unbound_local_error_with_instance(self):
class A:
def __init__(self):
Expand Down
21 changes: 19 additions & 2 deletionsLib/traceback.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1111,7 +1111,10 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
wrong_name=getattr(exc_value,"name_from",None)
suggestion=_compute_suggestion_error(exc_value,exc_traceback,wrong_name)
ifsuggestion:
self._str+=f". Did you mean: '{suggestion}'?"
ifsuggestion.isascii():
self._str+=f". Did you mean: '{suggestion}'?"
else:
self._str+=f". Did you mean: '{suggestion}' ({suggestion!a})?"
elifexc_typeandissubclass(exc_type,ModuleNotFoundError):
module_name=getattr(exc_value,"name",None)
ifmodule_nameinsys.stdlib_module_names:
Expand All@@ -1129,7 +1132,10 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
wrong_name=getattr(exc_value,"name",None)
suggestion=_compute_suggestion_error(exc_value,exc_traceback,wrong_name)
ifsuggestion:
self._str+=f". Did you mean: '{suggestion}'?"
ifsuggestion.isascii():
self._str+=f". Did you mean: '{suggestion}'?"
else:
self._str+=f". Did you mean: '{suggestion}' ({suggestion!a})?"
ifissubclass(exc_type,NameError):
wrong_name=getattr(exc_value,"name",None)
ifwrong_nameisnotNoneandwrong_nameinsys.stdlib_module_names:
Expand DownExpand Up@@ -1654,6 +1660,13 @@ def _check_for_nested_attribute(obj, wrong_name, attrs):
def_compute_suggestion_error(exc_value,tb,wrong_name):
ifwrong_nameisNoneornotisinstance(wrong_name,str):
returnNone
not_normalized=False
ifnotwrong_name.isascii():
fromunicodedataimportnormalize
normalized_name=normalize('NFKC',wrong_name)
ifnormalized_name!=wrong_name:
not_normalized=True
wrong_name=normalized_name
ifisinstance(exc_value,AttributeError):
obj=exc_value.obj
try:
Expand DownExpand Up@@ -1699,6 +1712,8 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
+list(frame.f_builtins)
)
d= [xforxindifisinstance(x,str)]
ifnot_normalizedandwrong_nameind:
returnwrong_name

# Check first if we are in a method and the instance
# has the wrong name as attribute
Expand All@@ -1711,6 +1726,8 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
ifhas_wrong_name:
returnf"self.{wrong_name}"

ifnot_normalizedandwrong_nameind:
returnwrong_name
try:
import_suggestions
exceptImportError:
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
Name suggestion for not normalized name suggests now the normalized name or
the closest name to the normalized name. If the suggested name is not ASCII,
include also its ASCII representation.
Loading

[8]ページ先頭

©2009-2026 Movatter.jp