Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mypy/constant_fold.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from mypy.nodes import (
ComplexExpr,
ConditionalExpr,
Expression,
FloatExpr,
IntExpr,
Expand Down Expand Up @@ -73,6 +74,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


Expand Down
6 changes: 6 additions & 0 deletions mypyc/irbuild/constant_fold.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from mypy.nodes import (
BytesExpr,
ComplexExpr,
ConditionalExpr,
Expression,
FloatExpr,
IntExpr,
Expand Down Expand Up @@ -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, value_expr)
return None


Expand Down