Skip to content

Commit 1acdfd0

Browse files
authored
Fix crash on NamedTuple with method and error in function (#17498)
Fixes #16814 This one is tricky and may expose some other bugs. But IMO this is strictly correct thing to do.
1 parent e5b3b56 commit 1acdfd0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

mypy/semanal_main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ def process_top_level_function(
291291
deferred, incomplete, progress = semantic_analyze_target(
292292
target, module, state, node, active_type, final_iteration, patches
293293
)
294+
if not incomplete:
295+
state.manager.incomplete_namespaces.discard(module)
294296
if final_iteration:
295297
assert not deferred, "Must not defer during final iteration"
296298
if not progress:

test-data/unit/check-namedtuple.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,3 +1423,27 @@ class Foo(typing.NamedTuple):
14231423
reveal_type(x) # N: Revealed type is "builtins.int"
14241424
[builtins fixtures/tuple.pyi]
14251425
[typing fixtures/typing-namedtuple.pyi]
1426+
1427+
[case testNameErrorInNamedTupleNestedInFunction1]
1428+
from typing import NamedTuple
1429+
1430+
def bar() -> None:
1431+
class MyNamedTuple(NamedTuple):
1432+
a: int
1433+
def foo(self) -> None:
1434+
...
1435+
int_set: Set[int] # E: Name "Set" is not defined \
1436+
# N: Did you forget to import it from "typing"? (Suggestion: "from typing import Set")
1437+
[builtins fixtures/tuple.pyi]
1438+
[typing fixtures/typing-namedtuple.pyi]
1439+
1440+
[case testNameErrorInNamedTupleNestedInFunction2]
1441+
from typing import NamedTuple
1442+
1443+
def bar() -> None:
1444+
class MyNamedTuple(NamedTuple):
1445+
a: int
1446+
def foo(self) -> None:
1447+
misspelled_var_name # E: Name "misspelled_var_name" is not defined
1448+
[builtins fixtures/tuple.pyi]
1449+
[typing fixtures/typing-namedtuple.pyi]

0 commit comments

Comments
 (0)