Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import mypy.util
import mypy.version
from mypy.build import build
from mypy.constant_fold import constant_fold_expr
from mypy.errors import CompileError, Errors
from mypy.find_sources import InvalidSourceList, create_source_list
from mypy.modulefinder import (
Expand Down Expand Up @@ -1021,14 +1022,16 @@ def is_typed_namedtuple(self, expr: CallExpr) -> bool:
def _get_namedtuple_fields(self, call: CallExpr) -> list[tuple[str, str]] | None:
if self.is_namedtuple(call):
fields_arg = call.args[1]
if isinstance(fields_arg, StrExpr):
field_names = fields_arg.value.replace(",", " ").split()
folded = constant_fold_expr(fields_arg, "<unused>")
if isinstance(folded, str):
field_names = folded.replace(",", " ").split()
elif isinstance(fields_arg, (ListExpr, TupleExpr)):
field_names = []
for field in fields_arg.items:
if not isinstance(field, StrExpr):
folded = constant_fold_expr(field, "<unused>")
if not isinstance(folded, str):
return None
field_names.append(field.value)
field_names.append(folded)
else:
return None # Invalid namedtuple fields type
if field_names:
Expand Down