Skip to content

Commit

Permalink
Fix crash on NamedTuple with method and error in function (#17498)
Browse files Browse the repository at this point in the history
Fixes #16814

This one is tricky and may expose some other bugs. But IMO this is
strictly correct thing to do.
  • Loading branch information
ilevkivskyi authored Jul 6, 2024
1 parent e5b3b56 commit 1acdfd0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mypy/semanal_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ def process_top_level_function(
deferred, incomplete, progress = semantic_analyze_target(
target, module, state, node, active_type, final_iteration, patches
)
if not incomplete:
state.manager.incomplete_namespaces.discard(module)
if final_iteration:
assert not deferred, "Must not defer during final iteration"
if not progress:
Expand Down
24 changes: 24 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1423,3 +1423,27 @@ class Foo(typing.NamedTuple):
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]

[case testNameErrorInNamedTupleNestedInFunction1]
from typing import NamedTuple

def bar() -> None:
class MyNamedTuple(NamedTuple):
a: int
def foo(self) -> None:
...
int_set: Set[int] # E: Name "Set" is not defined \
# N: Did you forget to import it from "typing"? (Suggestion: "from typing import Set")
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]

[case testNameErrorInNamedTupleNestedInFunction2]
from typing import NamedTuple

def bar() -> None:
class MyNamedTuple(NamedTuple):
a: int
def foo(self) -> None:
misspelled_var_name # E: Name "misspelled_var_name" is not defined
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]

0 comments on commit 1acdfd0

Please sign in to comment.