From 8d44ae0ed8ee1b4d00b71623c980c42a59cce1cd Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 2 Oct 2025 02:33:19 -0400 Subject: [PATCH 1/6] [mypyc] feat: add ConditionalExpr support to `constant_fold_expr` This PR adds ConditionalExpr support to `constant_fold_expr` --- mypy/constant_fold.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mypy/constant_fold.py b/mypy/constant_fold.py index 4582b2a7396d..e68c67830637 100644 --- a/mypy/constant_fold.py +++ b/mypy/constant_fold.py @@ -73,6 +73,11 @@ def constant_fold_expr(expr: Expression, cur_mod_id: str) -> ConstantValue | Non value = constant_fold_expr(expr.expr, cur_mod_id) if value is not None: return constant_fold_unary_op(expr.op, value) + elif isinstance(expr, ConditionalExpr): + cond = constant_fold_expr(expr.cond, cur_mod_id) + if cond is not None: + value_expr = expr.if_expr if cond else expr.else_expr + return constant_fold_expr(value_expr, cur_mod_id) return None From 0544fb17fc6bb18c058e37d9d8b85270ba4e59eb Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 2 Oct 2025 02:35:39 -0400 Subject: [PATCH 2/6] Update constant_fold.py --- mypyc/irbuild/constant_fold.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mypyc/irbuild/constant_fold.py b/mypyc/irbuild/constant_fold.py index 12a4b15dd40c..789eecbfeb26 100644 --- a/mypyc/irbuild/constant_fold.py +++ b/mypyc/irbuild/constant_fold.py @@ -16,6 +16,7 @@ from mypy.nodes import ( BytesExpr, ComplexExpr, + ConditionalExpr, Expression, FloatExpr, IntExpr, @@ -72,6 +73,11 @@ def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue | value = constant_fold_expr(builder, expr.expr) if value is not None and not isinstance(value, bytes): return constant_fold_unary_op(expr.op, value) + elif isinstance(expr, ConditionalExpr): + cond = constant_fold_expr(builder, expr.cond) + if cond is not None: + value_expr = expr.if_expr if cond else expr.else_expr + return constant_fold_expr(builder, expr.cond) return None From 0c5e974895b79e3e979328fe29b226bdab6c81f0 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 2 Oct 2025 02:39:29 -0400 Subject: [PATCH 3/6] fix missing import --- mypy/constant_fold.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mypy/constant_fold.py b/mypy/constant_fold.py index e68c67830637..7908260c4c3f 100644 --- a/mypy/constant_fold.py +++ b/mypy/constant_fold.py @@ -9,6 +9,7 @@ from mypy.nodes import ( ComplexExpr, + ConditionalExpr, Expression, FloatExpr, IntExpr, From c10cbb001e0dbe169cd391ff9d96ee3ee6a396ba Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 2 Oct 2025 02:43:38 -0400 Subject: [PATCH 4/6] Update constant_fold.py --- mypyc/irbuild/constant_fold.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypyc/irbuild/constant_fold.py b/mypyc/irbuild/constant_fold.py index 789eecbfeb26..102f9ccf0fbf 100644 --- a/mypyc/irbuild/constant_fold.py +++ b/mypyc/irbuild/constant_fold.py @@ -77,7 +77,7 @@ def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue | cond = constant_fold_expr(builder, expr.cond) if cond is not None: value_expr = expr.if_expr if cond else expr.else_expr - return constant_fold_expr(builder, expr.cond) + return constant_fold_expr(builder, value_expr) return None From 443c0a008b58a788d423d636b7c62b3017278c1e Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Thu, 2 Oct 2025 14:46:21 -0400 Subject: [PATCH 5/6] Update irbuild-constant-fold.test --- mypyc/test-data/irbuild-constant-fold.test | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mypyc/test-data/irbuild-constant-fold.test b/mypyc/test-data/irbuild-constant-fold.test index cd953c84c541..f5e041cf2ceb 100644 --- a/mypyc/test-data/irbuild-constant-fold.test +++ b/mypyc/test-data/irbuild-constant-fold.test @@ -478,3 +478,16 @@ L0: r3 = (-1.5+2j) neg_2 = r3 return 1 + +[case testConditionalConstantFolding] +from typing import Final + +constant: Final = 1 + +def f() -> None: + a = "t" if constant else "f" +[out] +def f(): + a :: str +L0: + a = "t" From 46ab58cf4bebf78b511bfe179aeb43fac9cef18f Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Fri, 3 Oct 2025 17:46:39 -0400 Subject: [PATCH 6/6] Update expression.py --- mypyc/irbuild/expression.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mypyc/irbuild/expression.py b/mypyc/irbuild/expression.py index 59ecc4ac2c5c..f239f3a1fee7 100644 --- a/mypyc/irbuild/expression.py +++ b/mypyc/irbuild/expression.py @@ -653,6 +653,7 @@ def try_gen_slice_op(builder: IRBuilder, base: Value, index: SliceExpr) -> Value return None +@folding_candidate def transform_conditional_expr(builder: IRBuilder, expr: ConditionalExpr) -> Value: if_body, else_body, next_block = BasicBlock(), BasicBlock(), BasicBlock()