Skip to content

Commit 34dced7

Browse files
committed
🐴 special-case mypy false-positive operator error for inexact x inexact binop
1 parent 9e5c956 commit 34dced7

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

test/generated/scalar_ops_arithmetic.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# @generated 2025-03-28T20:53:33Z with tool/testgen.py
1+
# @generated 2025-03-30T19:51:31Z with tool/testgen.py
22
from typing_extensions import assert_type
33

44
import numpy as np
@@ -689,7 +689,7 @@ assert_type(fc + i, np.inexact)
689689
assert_type(fc + f, np.inexact)
690690
assert_type(fc + c, np.complexfloating)
691691
assert_type(fc + ui, np.inexact)
692-
assert_type(fc + fc, np.inexact)
692+
assert_type(fc + fc, np.inexact) # type: ignore[operator] # NOTE: mypy workaround
693693

694694
###
695695
# __[r]sub__
@@ -1331,7 +1331,7 @@ assert_type(fc - i, np.inexact)
13311331
assert_type(fc - f, np.inexact)
13321332
assert_type(fc - c, np.complexfloating)
13331333
assert_type(fc - ui, np.inexact)
1334-
assert_type(fc - fc, np.inexact)
1334+
assert_type(fc - fc, np.inexact) # type: ignore[operator] # NOTE: mypy workaround
13351335

13361336
###
13371337
# __[r]mul__
@@ -1968,7 +1968,7 @@ assert_type(fc * i, np.inexact)
19681968
assert_type(fc * f, np.inexact)
19691969
assert_type(fc * c, np.complexfloating)
19701970
assert_type(fc * ui, np.inexact)
1971-
assert_type(fc * fc, np.inexact)
1971+
assert_type(fc * fc, np.inexact) # type: ignore[operator] # NOTE: mypy workaround
19721972

19731973
###
19741974
# __[r]pow__
@@ -2565,7 +2565,7 @@ assert_type(fc**i, np.inexact)
25652565
assert_type(fc**f, np.inexact)
25662566
assert_type(fc**c, np.complexfloating)
25672567
assert_type(fc**ui, np.inexact)
2568-
assert_type(fc**fc, np.inexact)
2568+
assert_type(fc**fc, np.inexact) # type: ignore[operator] # NOTE: mypy workaround
25692569

25702570
###
25712571
# __[r]truediv__

test/generated/scalar_ops_modular.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# @generated 2025-03-28T20:53:34Z with tool/testgen.py
1+
# @generated 2025-03-30T19:51:31Z with tool/testgen.py
22
from typing_extensions import assert_type
33

44
import numpy as np
@@ -653,7 +653,7 @@ assert_type(fc // i, np.floating)
653653
assert_type(fc // f, np.floating)
654654
fc // c # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
655655
assert_type(fc // ui, np.floating)
656-
assert_type(fc // fc, np.floating)
656+
assert_type(fc // fc, np.floating) # type: ignore[operator] # NOTE: mypy workaround
657657

658658
###
659659
# __[r]mod__
@@ -1252,7 +1252,7 @@ assert_type(fc % i, np.floating)
12521252
assert_type(fc % f, np.floating)
12531253
fc % c # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
12541254
assert_type(fc % ui, np.floating)
1255-
assert_type(fc % fc, np.floating)
1255+
assert_type(fc % fc, np.floating) # type: ignore[operator] # NOTE: mypy workaround
12561256

12571257
###
12581258
# __[r]divmod__
@@ -1851,4 +1851,4 @@ assert_type(divmod(fc, i), tuple[np.floating, np.floating])
18511851
assert_type(divmod(fc, f), tuple[np.floating, np.floating])
18521852
divmod(fc, c) # type: ignore[operator] # pyright: ignore[reportArgumentType, reportCallIssue]
18531853
assert_type(divmod(fc, ui), tuple[np.floating, np.floating])
1854-
assert_type(divmod(fc, fc), tuple[np.floating, np.floating])
1854+
assert_type(divmod(fc, fc), tuple[np.floating, np.floating]) # type: ignore[operator] # NOTE: mypy workaround

tool/testgen.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -766,16 +766,19 @@ def _assert_stmt(self, op: str, lhs: str, rhs: str, /) -> str | None:
766766
stmt = _expr_assert_type(expr_eval, expr_type)
767767

768768
# workaround for mypy's lack of support for reflected binary ops like __radd__
769-
if (
770-
lhs == rhs == "BHILbhil"
771-
and op in self.OPS_ARITHMETIC | self.OPS_MODULAR
772-
and " / " not in op
773-
):
774-
stmt = " # ".join(( # noqa: FLY002
775-
stmt,
776-
"type: ignore[assert-type, operator]",
777-
"NOTE: mypy workaround",
778-
))
769+
if op in self.OPS_ARITHMETIC | self.OPS_MODULAR and " / " not in op:
770+
if lhs == rhs == "BHILbhil":
771+
stmt = " # ".join(( # noqa: FLY002
772+
stmt,
773+
"type: ignore[assert-type, operator]",
774+
"NOTE: mypy workaround",
775+
))
776+
elif lhs == rhs == "efdgFDG":
777+
stmt = " # ".join(( # noqa: FLY002
778+
stmt,
779+
"type: ignore[operator]",
780+
"NOTE: mypy workaround",
781+
))
779782

780783
return stmt
781784

0 commit comments

Comments
 (0)