Skip to content

Commit 51fdd2d

Browse files
Update constant_fold.py
1 parent 970cd4f commit 51fdd2d

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

mypy/constant_fold.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
ComplexExpr,
1212
Expression,
1313
FloatExpr,
14+
IndexExpr,
1415
IntExpr,
1516
NameExpr,
1617
OpExpr,
@@ -73,6 +74,15 @@ def constant_fold_expr(expr: Expression, cur_mod_id: str) -> ConstantValue | Non
7374
value = constant_fold_expr(expr.expr, cur_mod_id)
7475
if value is not None:
7576
return constant_fold_unary_op(expr.op, value)
77+
elif isinstance(expr, IndexExpr):
78+
base = constant_fold_expr(expr.base, cur_mod_id)
79+
if base is not None:
80+
index = constant_fold_expr(expr.index, cur_mod_id)
81+
if index is not None:
82+
try:
83+
return base[index]
84+
except Exception:
85+
return None
7686
return None
7787

7888

0 commit comments

Comments
 (0)