Skip to content

Commit 3facfba

Browse files
committed
[mypyc] feat: support constant folding in ASTStubGenerator._get_namedtuple_fields
1 parent 139071c commit 3facfba

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

mypy/stubgen.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import mypy.util
5858
import mypy.version
5959
from mypy.build import build
60+
from mypy.constant_fold import constant_fold_expr
6061
from mypy.errors import CompileError, Errors
6162
from mypy.find_sources import InvalidSourceList, create_source_list
6263
from mypy.modulefinder import (
@@ -1021,14 +1022,16 @@ def is_typed_namedtuple(self, expr: CallExpr) -> bool:
10211022
def _get_namedtuple_fields(self, call: CallExpr) -> list[tuple[str, str]] | None:
10221023
if self.is_namedtuple(call):
10231024
fields_arg = call.args[1]
1024-
if isinstance(fields_arg, StrExpr):
1025-
field_names = fields_arg.value.replace(",", " ").split()
1025+
folded = constant_fold_expr(fields_arg, "<unused>")
1026+
if isinstance(folded, str):
1027+
field_names = folded.replace(",", " ").split()
10261028
elif isinstance(fields_arg, (ListExpr, TupleExpr)):
10271029
field_names = []
10281030
for field in fields_arg.items:
1029-
if not isinstance(field, StrExpr):
1031+
folded = constant_fold_expr(field, "<unused>")
1032+
if not isinstance(folded, str):
10301033
return None
1031-
field_names.append(field.value)
1034+
field_names.append(folded)
10321035
else:
10331036
return None # Invalid namedtuple fields type
10341037
if field_names:

0 commit comments

Comments
 (0)