Skip to content

Commit 3bef7b8

Browse files
authored
stubgen: Handle commas in namedtuple field definition (#10828)
1 parent 5e83bd6 commit 3bef7b8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

mypy/stubgen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ def process_namedtuple(self, lvalue: NameExpr, rvalue: CallExpr) -> None:
926926
if self._state != EMPTY:
927927
self.add('\n')
928928
if isinstance(rvalue.args[1], StrExpr):
929-
items = rvalue.args[1].value.split(" ")
929+
items = rvalue.args[1].value.replace(',', ' ').split()
930930
elif isinstance(rvalue.args[1], (ListExpr, TupleExpr)):
931931
list_items = cast(List[StrExpr], rvalue.args[1].items)
932932
items = [item.value for item in list_items]

test-data/unit/stubgen.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,28 @@ xx
590590
[out]
591591
from typing import Any, NamedTuple
592592

593+
class X(NamedTuple):
594+
a: Any
595+
b: Any
596+
597+
[case testNamedtupleAltSyntaxUsingComma]
598+
from collections import namedtuple, xx
599+
X = namedtuple('X', 'a, b')
600+
xx
601+
[out]
602+
from typing import Any, NamedTuple
603+
604+
class X(NamedTuple):
605+
a: Any
606+
b: Any
607+
608+
[case testNamedtupleAltSyntaxUsingMultipleCommas]
609+
from collections import namedtuple, xx
610+
X = namedtuple('X', 'a,, b')
611+
xx
612+
[out]
613+
from typing import Any, NamedTuple
614+
593615
class X(NamedTuple):
594616
a: Any
595617
b: Any

0 commit comments

Comments
 (0)