Skip to content

Commit 0a55800

Browse files
[wip] [mypyc] feat: quasi-constant folding for DictExpr and TupleExpr
1 parent 43364c1 commit 0a55800

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

mypyc/irbuild/constant_fold.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
from mypyc.irbuild.util import bytes_from_str
3131

3232
# All possible result types of constant folding
33-
ConstantValue = Union[int, float, complex, str, bytes]
34-
CONST_TYPES: Final = (int, float, complex, str, bytes)
33+
ConstantValue = Union[int, float, complex, str, bytes, tuple]
34+
CONST_TYPES: Final = (int, float, complex, str, bytes, tuple)
3535

3636

3737
def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue | None:
@@ -72,6 +72,10 @@ def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue |
7272
value = constant_fold_expr(builder, expr.expr)
7373
if value is not None and not isinstance(value, bytes):
7474
return constant_fold_unary_op(expr.op, value)
75+
elif isinstance(expr, TupleExpr):
76+
folded = tuple(constant_fold_expr(item) for item in expr.items)
77+
if None not in folded:
78+
return folded
7579
return None
7680

7781

0 commit comments

Comments
 (0)