Skip to content

Commit 056994c

Browse files
extend for TupleType comprised of Literals
1 parent b84bfbd commit 056994c

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

mypyc/irbuild/constant_fold.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,20 @@ def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue |
8282
# builtins.str methods
8383
if isinstance(folded_callee, str):
8484
# str.join
85-
if (
86-
callee.name == "join"
87-
and len(args := expr.args) == 1
85+
if callee.name == "join" and len(args := expr.args) == 1:
8886
# TODO extend this to work with rtuples comprised of known literal values
89-
and isinstance(arg := args[0], (ListExpr, TupleExpr))
90-
):
91-
folded_items = constant_fold_container_expr(builder, arg)
92-
if folded_items is not None and all(
93-
isinstance(item, str) for item in folded_items
87+
arg = args[0]
88+
if isinstance(arg, (ListExpr, TupleExpr)):
89+
folded_items = constant_fold_container_expr(builder, arg)
90+
if folded_items is not None and all(
91+
isinstance(item, str) for item in folded_items
92+
):
93+
return folded_callee.join(folded_items) # type: ignore [arg-type]
94+
expr_type = builder.types[arg]
95+
if isinstance(expr_type, TupleType) and all(
96+
isinstance(i, LiteralType) and isinstance(i.value, str) for i in expr_type.items
9497
):
95-
return folded_callee.join(folded_items) # type: ignore [arg-type]
98+
return folded_callee.join(i.value for i in expr_type.items)
9699

97100
# builtins.bytes methods
98101
elif isinstance(folded_callee, bytes):

0 commit comments

Comments
 (0)