Skip to content

Commit 31d0bd2

Browse files
committed
fix filtering names bug and be more strict on name collection
1 parent 115b831 commit 31d0bd2

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

mypy/semanal.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7475,12 +7475,16 @@ def name_not_defined(self, name: str, ctx: Context, namespace: str | None = None
74757475
self.record_incomplete_ref()
74767476
return
74777477
message = f'Name "{name}" is not defined'
7478-
# Collect all names in scope to suggest similar alternatives
7479-
alternatives = self._get_names_in_scope()
7480-
alternatives.discard(name)
7481-
matches = best_matches(name, alternatives, n=3)
7482-
if matches:
7483-
message += f"; did you mean {pretty_seq(matches, 'or')}?"
7478+
if (
7479+
"." not in name
7480+
and not (name.startswith("__") and name.endswith("__"))
7481+
and f"builtins.{name}" not in SUGGESTED_TEST_FIXTURES
7482+
):
7483+
alternatives = self._get_names_in_scope()
7484+
alternatives.discard(name)
7485+
matches = best_matches(name, alternatives, n=3)
7486+
if matches:
7487+
message += f"; did you mean {pretty_seq(matches, 'or')}?"
74847488
self.fail(message, ctx, code=codes.NAME_DEFINED)
74857489

74867490
if f"builtins.{name}" in SUGGESTED_TEST_FIXTURES:
@@ -7532,8 +7536,8 @@ def _get_names_in_scope(self) -> set[str]:
75327536
if not (len(builtin_name) > 1 and builtin_name[0] == "_" and builtin_name[1] != "_"):
75337537
names.add(builtin_name)
75347538

7535-
# Filter out internal/dunder names that aren't useful for suggestions and might introduce noise
7536-
return {n for n in names if not n.startswith("__") or n.endswith("__")}
7539+
# Filter out internal/dunder names that aren't useful as suggestions
7540+
return {n for n in names if not n.startswith("__")}
75377541

75387542
def already_defined(
75397543
self, name: str, ctx: Context, original_ctx: SymbolTableNode | SymbolNode | None, noun: str

test-data/unit/pythoneval.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ def f(x: _T) -> None: pass
608608
s: FrozenSet
609609
[out]
610610
_program.py:2: error: Name "_T" is not defined
611-
_program.py:3: error: Name "FrozenSet" is not defined
611+
_program.py:3: error: Name "FrozenSet" is not defined; did you mean "frozenset"?
612612

613613
[case testVarArgsFunctionSubtyping]
614614
import typing

0 commit comments

Comments
 (0)