Skip to content

Commit 468dff2

Browse files
hoeflinggvanrossum
authored andcommitted
Add handling of field names tuple to stubgen (#4407)
Fixes #4406
1 parent 7de40fa commit 468dff2

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

mypy/stubgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ def process_namedtuple(self, lvalue: NameExpr, rvalue: CallExpr) -> None:
593593
name = repr(getattr(rvalue.args[0], 'value', '<ERROR>'))
594594
if isinstance(rvalue.args[1], StrExpr):
595595
items = repr(rvalue.args[1].value)
596-
elif isinstance(rvalue.args[1], ListExpr):
596+
elif isinstance(rvalue.args[1], (ListExpr, TupleExpr)):
597597
list_items = cast(List[StrExpr], rvalue.args[1].items)
598598
items = '[%s]' % ', '.join(repr(item.value) for item in list_items)
599599
else:

test-data/unit/stubgen.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,20 @@ _X = namedtuple('_X', ['a', 'b'])
569569

570570
class Y(_X): ...
571571

572+
[case testNamedtupleAltSyntaxFieldsTuples]
573+
from collections import namedtuple, x
574+
X = namedtuple('X', ())
575+
Y = namedtuple('Y', ('a',))
576+
Z = namedtuple('Z', ('a', 'b', 'c', 'd', 'e'))
577+
[out]
578+
from collections import namedtuple
579+
580+
X = namedtuple('X', [])
581+
582+
Y = namedtuple('Y', ['a'])
583+
584+
Z = namedtuple('Z', ['a', 'b', 'c', 'd', 'e'])
585+
572586
[case testArbitraryBaseClass]
573587
import x
574588
class D(x.C): ...

0 commit comments

Comments
 (0)