Skip to content

Commit

Permalink
Add test for the weird NamedTuple behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
tmke8 committed Sep 20, 2023
1 parent f01b176 commit 560f365
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mypy/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,14 +653,14 @@ def save_namedtuple_body(self, named_tuple_info: TypeInfo) -> Iterator[None]:
# Keep user-defined methods as is.
continue
elif isinstance(sym.node, Var):
# NamedTuple fields are initialized in the generated __init__.
# Don't track the initialization status of the mangled attributes.
sym.node.is_uninitialized = False
# Keep existing (user-provided) definitions under mangled names, so they
# get semantically analyzed.
r_key = get_unique_redefinition_name(key, named_tuple_info.names)
named_tuple_info.names[r_key] = sym
if isinstance(value.node, Var):
# NamedTuple fields are initialized in the generated __init__.
# When a NamedTuple is analyzed multiple times, we have to reset this again.
value.node.is_uninitialized = False
named_tuple_info.names[key] = value

Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-warnings.test
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,17 @@ class Employee(NamedTuple):
Employee("foo")
[builtins fixtures/tuple.pyi]

[case testUninitializedAttributeNamedTupleRunTwice]
# flags: --warn-uninitialized-attributes
from typing import NamedTuple

NT: "N" # Force mypy to analyze this twice.
class N(NamedTuple):
x: int

NT = N(1)
[builtins fixtures/tuple.pyi]

[case testUninitializedAttributeTypedDict]
# flags: --warn-uninitialized-attributes
from mypy_extensions import TypedDict
Expand Down

0 comments on commit 560f365

Please sign in to comment.