Skip to content

Commit 7e9e587

Browse files
committed
Use full typeshed stubs in namedtuple test
1 parent 91a477d commit 7e9e587

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

mypy/semanal.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,18 +2120,19 @@ def analyze_namedtuple_assign(self, s: AssignmentStmt) -> bool:
21202120
"""Check if s defines a namedtuple."""
21212121
if isinstance(s.rvalue, CallExpr) and isinstance(s.rvalue.analyzed, NamedTupleExpr):
21222122
return True # This is a valid and analyzed named tuple definition, nothing to do here.
2123-
if len(s.lvalues) != 1 or not (isinstance(s.lvalues[0], NameExpr) or
2124-
isinstance(s.lvalues[0], MemberExpr)):
2123+
if len(s.lvalues) != 1:
2124+
return False
2125+
if not isinstance(s.lvalues[0], NameExpr):
2126+
if isinstance(s.lvalues[0], MemberExpr):
2127+
self.fail("NamedTuple type as an attribute is not supported", s.lvalues[0])
2128+
return False
21252129
return False
21262130
lvalue = s.lvalues[0]
21272131
name = lvalue.name
21282132
is_named_tuple, info = self.named_tuple_analyzer.check_namedtuple(s.rvalue, name,
21292133
self.is_func_scope())
21302134
if not is_named_tuple:
21312135
return False
2132-
if isinstance(s.lvalues[0], MemberExpr):
2133-
self.fail("NamedTuple type as an attribute is not supported", s.lvalues[0])
2134-
return False
21352136
# Yes, it's a valid namedtuple, but defer if it is not ready.
21362137
if not info:
21372138
self.mark_incomplete(name, lvalue, becomes_typeinfo=True)

test-data/unit/check-namedtuple.test

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,3 @@ b = (1, 2)
853853
if not b:
854854
''() # E: "str" not callable
855855
[builtins fixtures/tuple.pyi]
856-
857-
[case testNamedTupleAtRunTime]
858-
from typing import NamedTuple
859-
860-
class A:
861-
def __init__(self) -> None:
862-
self.b = NamedTuple('x', [('s', str), ('n', int)]) # E: NamedTuple type as an attribute is not supported

test-data/unit/pythoneval.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,3 +1453,15 @@ def f_suppresses() -> int:
14531453
[out]
14541454
_testUnreachableWithStdlibContextManagersNoStrictOptional.py:9: error: Statement is unreachable
14551455
_testUnreachableWithStdlibContextManagersNoStrictOptional.py:15: error: Statement is unreachable
1456+
1457+
[case testNamedTupleAtRunTime]
1458+
from typing import NamedTuple
1459+
1460+
class A:
1461+
def __init__(self) -> None:
1462+
self.b = NamedTuple('x', [('s', str), ('n', int)])
1463+
1464+
reveal_type(A().b)
1465+
[out]
1466+
_testNamedTupleAtRunTime.py:5: error: NamedTuple type as an attribute is not supported
1467+
_testNamedTupleAtRunTime.py:7: note: Revealed type is 'typing.NamedTuple'

0 commit comments

Comments
 (0)