Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions mypy/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
NamedTupleExpr,
NameExpr,
PassStmt,
PlaceholderNode,
RefExpr,
Statement,
StrExpr,
Expand Down Expand Up @@ -697,10 +698,14 @@ def save_namedtuple_body(self, named_tuple_info: TypeInfo) -> Iterator[None]:
if isinstance(sym.node, (FuncBase, Decorator)) and not sym.plugin_generated:
# Keep user-defined methods as is.
continue
# 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
# Do not retain placeholders - we'll get back here if they cease to
# be placeholders later. If we keep placeholders alive, they may never
# be reached again, making it to cacheable symtable.
if not isinstance(sym.node, PlaceholderNode):
# 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
named_tuple_info.names[key] = value

# Helpers
Expand Down
25 changes: 25 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1530,3 +1530,28 @@ class Base:
names = [name for name in namespace if fail] # E: Name "fail" is not defined
self.n = namedtuple("n", names) # E: NamedTuple type as an attribute is not supported
[builtins fixtures/tuple.pyi]

[case testNamedTupleDefaultValueDefer]
# flags: --debug-serialize
from typing import NamedTuple

class NT(NamedTuple):
foo: int = UNDEFINED # E: Name "UNDEFINED" is not defined
[builtins fixtures/tuple.pyi]

[case testNamedTupleDefaultValueDefer2]
# flags: --debug-serialize
from typing import NamedTuple

class NT(NamedTuple):
foo: int = DEFERRED_INT

class NT2(NamedTuple):
foo: int = DEFERRED_STR # E: Incompatible types in assignment (expression has type "str", variable has type "int")

from foo import DEFERRED_INT, DEFERRED_STR

[file foo.py]
DEFERRED_INT = 1
DEFERRED_STR = "a"
[builtins fixtures/tuple.pyi]
Loading