MCPcopy Create free account
hub / github.com/python/mypy / constant_fold_binary_float_op

Function constant_fold_binary_float_op

mypy/constant_fold.py:150–177  ·  view source on GitHub ↗
(op: str, left: int | float, right: int | float)

Source from the content-addressed store, hash-verified

148
149
150def constant_fold_binary_float_op(op: str, left: int | float, right: int | float) -> float | None:
151 assert not (isinstance(left, int) and isinstance(right, int)), (op, left, right)
152 if op == "+":
153 return left + right
154 elif op == "-":
155 return left - right
156 elif op == "*":
157 return left * right
158 elif op == "/":
159 if right != 0:
160 return left / right
161 elif op == "//":
162 if right != 0:
163 return left // right
164 elif op == "%":
165 if right != 0:
166 return left % right
167 elif op == "**":
168 if (left < 0 and isinstance(right, int)) or left > 0:
169 try:
170 ret = left**right
171 except OverflowError:
172 return None
173 else:
174 assert isinstance(ret, float), ret
175 return ret
176
177 return None
178
179
180def constant_fold_unary_op(op: str, value: ConstantValue) -> int | float | None:

Callers 1

constant_fold_binary_opFunction · 0.85

Calls 1

isinstanceFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…