diff --git a/mypy/semanal_namedtuple.py b/mypy/semanal_namedtuple.py index 07863dea2efb..109ec17cbc89 100644 --- a/mypy/semanal_namedtuple.py +++ b/mypy/semanal_namedtuple.py @@ -186,11 +186,16 @@ def check_namedtuple(self, # Error. Construct dummy return value. if var_name: name = var_name + if is_func_scope: + name += '@' + str(call.line) else: - name = 'namedtuple@' + str(call.line) + name = var_name = 'namedtuple@' + str(call.line) info = self.build_namedtuple_typeinfo(name, [], [], {}, node.line) - self.store_namedtuple_info(info, name, call, is_typed) - return name, info + self.store_namedtuple_info(info, var_name, call, is_typed) + if name != var_name or is_func_scope: + # NOTE: we skip local namespaces since they are not serialized. + self.api.add_symbol_skip_local(name, info) + return var_name, info if not ok: # This is a valid named tuple but some types are not ready. return typename, None diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test index c604b386691b..79fa1c92c52e 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -5711,3 +5711,32 @@ class C: [builtins fixtures/dict.pyi] [out2] tmp/a.py:2: error: "object" has no attribute "xyz" + +[case testIncrementalInvalidNamedTupleInUnannotatedFunction] +import a + +[file a.py] +import b + +[file a.py.2] +import b # f + +[file b.py] +from typing import NamedTuple + +def toplevel(fields): + TupleType = NamedTuple("TupleType", fields) + class InheritFromTuple(TupleType): + pass + NT2 = NamedTuple("bad", [('x', int)]) + nt2: NT2 = NT2(x=1) + +class C: + def method(self, fields): + TupleType = NamedTuple("TupleType", fields) + class InheritFromTuple(TupleType): + pass + NT2 = NamedTuple("bad", [('x', int)]) + nt2: NT2 = NT2(x=1) + +[builtins fixtures/tuple.pyi] diff --git a/test-data/unit/check-namedtuple.test b/test-data/unit/check-namedtuple.test index b95cc96f8115..c6f1fe3b1d04 100644 --- a/test-data/unit/check-namedtuple.test +++ b/test-data/unit/check-namedtuple.test @@ -1120,3 +1120,17 @@ def bar1(c: C1) -> None: reveal_type(c) # N: Revealed type is "Tuple[builtins.int, fallback=__main__.C1]" [builtins fixtures/tuple.pyi] [typing fixtures/typing-namedtuple.pyi] + +[case testInvalidNamedTupleWithinFunction] +from collections import namedtuple + +def f(fields) -> None: + TupleType = namedtuple("TupleType", fields) \ + # E: List or tuple literal expected as the second argument to "namedtuple()" + class InheritFromTuple(TupleType): + pass + t: TupleType + it: InheritFromTuple + NT2 = namedtuple("bad", "x") # E: First argument to namedtuple() should be "NT2", not "bad" + nt2: NT2 = NT2(x=1) +[builtins fixtures/tuple.pyi]