Skip to content

Commit 7427511

Browse files
committed
Fix namedtuple crash in unannotated function
Fixes #11121.
1 parent 18a5107 commit 7427511

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

mypy/semanal_namedtuple.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ def check_namedtuple(self,
186186
# Error. Construct dummy return value.
187187
if var_name:
188188
name = var_name
189+
if is_func_scope:
190+
name += '@' + str(call.line)
189191
else:
190192
name = 'namedtuple@' + str(call.line)
191193
info = self.build_namedtuple_typeinfo(name, [], [], {}, node.line)

test-data/unit/check-incremental.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5711,3 +5711,24 @@ class C:
57115711
[builtins fixtures/dict.pyi]
57125712
[out2]
57135713
tmp/a.py:2: error: "object" has no attribute "xyz"
5714+
5715+
[case testIncrementalInvalidNamedTupleInUnannotatedFunction]
5716+
import a
5717+
5718+
[file a.py]
5719+
import b
5720+
5721+
[file a.py.2]
5722+
import b # f
5723+
5724+
[file b.py]
5725+
from typing import NamedTuple
5726+
5727+
def crash(fields):
5728+
TupleType = NamedTuple("TupleType", fields)
5729+
class InheritFromTuple(TupleType):
5730+
pass
5731+
NT2 = NamedTuple("bad", [('x', int)])
5732+
nt2: NT2 = NT2(x=1)
5733+
5734+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)