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

Function constant_fold_binary_op

mypy/constant_fold.py:79–111  ·  view source on GitHub ↗
(
    op: str, left: ConstantValue, right: ConstantValue
)

Source from the content-addressed store, hash-verified

77
78
79def constant_fold_binary_op(
80 op: str, left: ConstantValue, right: ConstantValue
81) -> ConstantValue | None:
82 if isinstance(left, int) and isinstance(right, int):
83 return constant_fold_binary_int_op(op, left, right)
84
85 # Float and mixed int/float arithmetic.
86 if isinstance(left, float) and isinstance(right, float):
87 return constant_fold_binary_float_op(op, left, right)
88 elif isinstance(left, float) and isinstance(right, int):
89 return constant_fold_binary_float_op(op, left, right)
90 elif isinstance(left, int) and isinstance(right, float):
91 return constant_fold_binary_float_op(op, left, right)
92
93 # String concatenation and multiplication.
94 if op == "+" and isinstance(left, str) and isinstance(right, str):
95 return left + right
96 elif op == "*" and isinstance(left, str) and isinstance(right, int):
97 return left * right
98 elif op == "*" and isinstance(left, int) and isinstance(right, str):
99 return left * right
100
101 # Complex construction.
102 if op == "+" and isinstance(left, (int, float)) and isinstance(right, complex):
103 return left + right
104 elif op == "+" and isinstance(left, complex) and isinstance(right, (int, float)):
105 return left + right
106 elif op == "-" and isinstance(left, (int, float)) and isinstance(right, complex):
107 return left - right
108 elif op == "-" and isinstance(left, complex) and isinstance(right, (int, float)):
109 return left - right
110
111 return None
112
113
114def constant_fold_binary_int_op(op: str, left: int, right: int) -> int | float | None:

Callers 2

constant_fold_exprFunction · 0.85

Calls 3

isinstanceFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…