Skip to content

Commit be8b79f

Browse files
committed
Add test for the weird NamedTuple behavior
1 parent 59c3add commit be8b79f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

mypy/semanal_namedtuple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,14 +658,14 @@ def save_namedtuple_body(self, named_tuple_info: TypeInfo) -> Iterator[None]:
658658
# Keep user-defined methods as is.
659659
continue
660660
elif isinstance(sym.node, Var):
661-
# NamedTuple fields are initialized in the generated __init__.
661+
# Don't track the initialization status of the mangled attributes.
662662
sym.node.is_uninitialized = False
663663
# Keep existing (user-provided) definitions under mangled names, so they
664664
# get semantically analyzed.
665665
r_key = get_unique_redefinition_name(key, named_tuple_info.names)
666666
named_tuple_info.names[r_key] = sym
667667
if isinstance(value.node, Var):
668-
# NamedTuple fields are initialized in the generated __init__.
668+
# When a NamedTuple is analyzed multiple times, we have to reset this again.
669669
value.node.is_uninitialized = False
670670
named_tuple_info.names[key] = value
671671

test-data/unit/check-warnings.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,17 @@ class Employee(NamedTuple):
375375
Employee("foo")
376376
[builtins fixtures/tuple.pyi]
377377

378+
[case testUninitializedAttributeNamedTupleRunTwice]
379+
# flags: --warn-uninitialized-attributes
380+
from typing import NamedTuple
381+
382+
NT: "N" # Force mypy to analyze this twice.
383+
class N(NamedTuple):
384+
x: int
385+
386+
NT = N(1)
387+
[builtins fixtures/tuple.pyi]
388+
378389
[case testUninitializedAttributeTypedDict]
379390
# flags: --warn-uninitialized-attributes
380391
from mypy_extensions import TypedDict

0 commit comments

Comments
 (0)