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

Function constant_fold_binary_int_op

mypy/constant_fold.py:114–147  ·  view source on GitHub ↗
(op: str, left: int, right: int)

Source from the content-addressed store, hash-verified

112
113
114def constant_fold_binary_int_op(op: str, left: int, right: int) -> int | float | None:
115 if op == "+":
116 return left + right
117 if op == "-":
118 return left - right
119 elif op == "*":
120 return left * right
121 elif op == "/":
122 if right != 0:
123 return left / right
124 elif op == "//":
125 if right != 0:
126 return left // right
127 elif op == "%":
128 if right != 0:
129 return left % right
130 elif op == "&":
131 return left & right
132 elif op == "|":
133 return left | right
134 elif op == "^":
135 return left ^ right
136 elif op == "<<":
137 if right >= 0:
138 return left << right
139 elif op == ">>":
140 if right >= 0:
141 return left >> right
142 elif op == "**":
143 if right >= 0:
144 ret = left**right
145 assert isinstance(ret, int)
146 return ret
147 return None
148
149
150def constant_fold_binary_float_op(op: str, left: int | float, right: int | float) -> 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…