@@ -1351,8 +1351,8 @@ def check_namedtuple(self, node: Node) -> TypeInfo:
1351
1351
fullname = callee .fullname
1352
1352
if fullname not in ('collections.namedtuple' , 'typing.NamedTuple' ):
1353
1353
return None
1354
- items , types = self .parse_namedtuple_args (call , fullname )
1355
- if not items :
1354
+ items , types , ok = self .parse_namedtuple_args (call , fullname )
1355
+ if not ok :
1356
1356
# Error. Construct dummy return value.
1357
1357
return self .build_namedtuple_typeinfo ('namedtuple' , [], [])
1358
1358
else :
@@ -1362,7 +1362,7 @@ def check_namedtuple(self, node: Node) -> TypeInfo:
1362
1362
return info
1363
1363
1364
1364
def parse_namedtuple_args (self , call : CallExpr ,
1365
- fullname : str ) -> Tuple [List [str ], List [Type ]]:
1365
+ fullname : str ) -> Tuple [List [str ], List [Type ], bool ]:
1366
1366
# TODO Share code with check_argument_count in checkexpr.py?
1367
1367
args = call .args
1368
1368
if len (args ) < 2 :
@@ -1375,6 +1375,7 @@ def parse_namedtuple_args(self, call: CallExpr,
1375
1375
return self .fail_namedtuple_arg (
1376
1376
"namedtuple() expects a string literal as the first argument" , call )
1377
1377
types = [] # type: List[Type]
1378
+ ok = True
1378
1379
if not isinstance (args [1 ], ListExpr ):
1379
1380
if fullname == 'collections.namedtuple' and isinstance (args [1 ], StrExpr ):
1380
1381
str_expr = cast (StrExpr , args [1 ])
@@ -1392,13 +1393,13 @@ def parse_namedtuple_args(self, call: CallExpr,
1392
1393
items = [cast (StrExpr , item ).value for item in listexpr .items ]
1393
1394
else :
1394
1395
# The fields argument contains (name, type) tuples.
1395
- items , types = self .parse_namedtuple_fields_with_types (listexpr .items , call )
1396
+ items , types , ok = self .parse_namedtuple_fields_with_types (listexpr .items , call )
1396
1397
if not types :
1397
1398
types = [AnyType () for _ in items ]
1398
- return items , types
1399
+ return items , types , ok
1399
1400
1400
1401
def parse_namedtuple_fields_with_types (self , nodes : List [Node ],
1401
- context : Context ) -> Tuple [List [str ], List [Type ]]:
1402
+ context : Context ) -> Tuple [List [str ], List [Type ], bool ]:
1402
1403
items = [] # type: List[str]
1403
1404
types = [] # type: List[Type]
1404
1405
for item in nodes :
@@ -1418,11 +1419,12 @@ def parse_namedtuple_fields_with_types(self, nodes: List[Node],
1418
1419
types .append (self .anal_type (type ))
1419
1420
else :
1420
1421
return self .fail_namedtuple_arg ("Tuple expected as NamedTuple() field" , item )
1421
- return items , types
1422
+ return items , types , True
1422
1423
1423
- def fail_namedtuple_arg (self , message : str , context : Context ) -> Tuple [List [str ], List [Type ]]:
1424
+ def fail_namedtuple_arg (self , message : str ,
1425
+ context : Context ) -> Tuple [List [str ], List [Type ], bool ]:
1424
1426
self .fail (message , context )
1425
- return [], []
1427
+ return [], [], False
1426
1428
1427
1429
def build_namedtuple_typeinfo (self , name : str , items : List [str ],
1428
1430
types : List [Type ]) -> TypeInfo :
0 commit comments