Skip to content

Commit fad0915

Browse files
committed
handle tuples
1 parent 7cd9c62 commit fad0915

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

mypyc/irbuild/expression.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,17 +1023,25 @@ def dict_literal_values(
10231023
If all keys and values are deeply immutable and constant (including nested dicts as values),
10241024
return the Python dict value. Otherwise, return None.
10251025
"""
1026+
def constant_fold_expr_or_tuple(builder: IRBuilder, expr: Expression) -> ConstantValueTuple | None:
1027+
value = constant_fold_expr(builder, expr)
1028+
if value is not None:
1029+
return value
1030+
if not isinstance(expr, TupleExpr):
1031+
return None
1032+
folded = tuple(const for const in map(constant_fold_expr_or_tuple, expr.items) if const is not None)
1033+
return folded if len(folded) == len(expr.items) else None
1034+
10261035
result = {}
10271036
for key_expr, value_expr in items:
10281037
if key_expr is None:
10291038
# ** unpacking, not a literal
10301039
# TODO: if ** is unpacking a dict literal we can use that, we just need logic
10311040
return None
1032-
key = constant_fold_expr(builder, key_expr)
1041+
key = constant_fold_expr_or_tuple(builder, key_expr)
10331042
if key is None:
10341043
return None
1035-
# Recursively staticize dict values
1036-
value = constant_fold_expr(builder, value_expr)
1044+
value = constant_fold_expr_or_tuple(builder, value_expr)
10371045
if value is None:
10381046
return None
10391047
result[key] = value

0 commit comments

Comments
 (0)