diff --git a/mypy/stubgen.py b/mypy/stubgen.py index 60fbd7f43c0f..75ba7ea473cf 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -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 ( @@ -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, "") + 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, "") + 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: