Skip to content

Commit

Permalink
stubgen: Handle commas in namedtuple field definition (python#10828)
Browse files Browse the repository at this point in the history
  • Loading branch information
ismeta authored Jul 16, 2021
1 parent 5e83bd6 commit 3bef7b8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ def process_namedtuple(self, lvalue: NameExpr, rvalue: CallExpr) -> None:
if self._state != EMPTY:
self.add('\n')
if isinstance(rvalue.args[1], StrExpr):
items = rvalue.args[1].value.split(" ")
items = rvalue.args[1].value.replace(',', ' ').split()
elif isinstance(rvalue.args[1], (ListExpr, TupleExpr)):
list_items = cast(List[StrExpr], rvalue.args[1].items)
items = [item.value for item in list_items]
Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,28 @@ xx
[out]
from typing import Any, NamedTuple

class X(NamedTuple):
a: Any
b: Any

[case testNamedtupleAltSyntaxUsingComma]
from collections import namedtuple, xx
X = namedtuple('X', 'a, b')
xx
[out]
from typing import Any, NamedTuple

class X(NamedTuple):
a: Any
b: Any

[case testNamedtupleAltSyntaxUsingMultipleCommas]
from collections import namedtuple, xx
X = namedtuple('X', 'a,, b')
xx
[out]
from typing import Any, NamedTuple

class X(NamedTuple):
a: Any
b: Any
Expand Down

0 comments on commit 3bef7b8

Please sign in to comment.