Skip to content

Commit fd74e0f

Browse files
committed
🐴 special-case mypy false-positive operator error for inexact / inexact -> inexact
1 parent c32b715 commit fd74e0f

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

test/generated/scalar_ops_arithmetic.pyi

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

44
import numpy as np
@@ -661,7 +661,7 @@ assert_type(ui + u, np.integer | np.float64)
661661
assert_type(ui + i, np.signedinteger | np.float64)
662662
assert_type(ui + f, np.floating)
663663
assert_type(ui + c, np.complexfloating)
664-
assert_type(ui + ui, np.integer | np.float64) # type: ignore[assert-type, operator] # NOTE: mypy workaround
664+
assert_type(ui + ui, np.integer | np.float64) # type: ignore[assert-type, operator] # 🐴
665665
assert_type(ui + fc, np.inexact)
666666

667667
assert_type(fc + b_py, np.inexact)
@@ -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) # type: ignore[operator] # NOTE: mypy workaround
692+
assert_type(fc + fc, np.inexact) # type: ignore[operator] # 🐴
693693

694694
###
695695
# __[r]sub__
@@ -1303,7 +1303,7 @@ assert_type(ui - u, np.integer | np.float64)
13031303
assert_type(ui - i, np.signedinteger | np.float64)
13041304
assert_type(ui - f, np.floating)
13051305
assert_type(ui - c, np.complexfloating)
1306-
assert_type(ui - ui, np.integer | np.float64) # type: ignore[assert-type, operator] # NOTE: mypy workaround
1306+
assert_type(ui - ui, np.integer | np.float64) # type: ignore[assert-type, operator] # 🐴
13071307
assert_type(ui - fc, np.inexact)
13081308

13091309
assert_type(fc - b_py, np.inexact)
@@ -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) # type: ignore[operator] # NOTE: mypy workaround
1334+
assert_type(fc - fc, np.inexact) # type: ignore[operator] # 🐴
13351335

13361336
###
13371337
# __[r]mul__
@@ -1939,7 +1939,7 @@ assert_type(ui * u, np.integer | np.float64)
19391939
assert_type(ui * i, np.signedinteger | np.float64)
19401940
assert_type(ui * f, np.floating)
19411941
assert_type(ui * c, np.complexfloating)
1942-
assert_type(ui * ui, np.integer | np.float64) # type: ignore[assert-type, operator] # NOTE: mypy workaround
1942+
assert_type(ui * ui, np.integer | np.float64) # type: ignore[assert-type, operator] # 🐴
19431943
assert_type(ui * fc, np.inexact)
19441944

19451945
assert_type(fc * b_py, np.inexact)
@@ -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) # type: ignore[operator] # NOTE: mypy workaround
1971+
assert_type(fc * fc, np.inexact) # type: ignore[operator] # 🐴
19721972

19731973
###
19741974
# __[r]pow__
@@ -2537,7 +2537,7 @@ assert_type(ui**u, np.integer | np.float64)
25372537
assert_type(ui**i, np.signedinteger | np.float64)
25382538
assert_type(ui**f, np.floating)
25392539
assert_type(ui**c, np.complexfloating)
2540-
assert_type(ui**ui, np.integer | np.float64) # type: ignore[assert-type, operator] # NOTE: mypy workaround
2540+
assert_type(ui**ui, np.integer | np.float64) # type: ignore[assert-type, operator] # 🐴
25412541
assert_type(ui**fc, np.inexact)
25422542

25432543
assert_type(fc**b_py, np.inexact)
@@ -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) # type: ignore[operator] # NOTE: mypy workaround
2568+
assert_type(fc**fc, np.inexact) # type: ignore[operator] # 🐴
25692569

25702570
###
25712571
# __[r]truediv__
@@ -3183,4 +3183,4 @@ assert_type(fc / i, np.inexact)
31833183
assert_type(fc / f, np.inexact)
31843184
assert_type(fc / c, np.complexfloating)
31853185
assert_type(fc / ui, np.inexact)
3186-
assert_type(fc / fc, np.inexact)
3186+
assert_type(fc / fc, np.inexact) # type: ignore[operator] # 🐴

test/generated/scalar_ops_modular.pyi

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

44
import numpy as np
@@ -625,7 +625,7 @@ assert_type(ui // u, np.integer | np.float64)
625625
assert_type(ui // i, np.signedinteger | np.float64)
626626
assert_type(ui // f, np.floating)
627627
ui // c # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
628-
assert_type(ui // ui, np.integer | np.float64) # type: ignore[assert-type, operator] # NOTE: mypy workaround
628+
assert_type(ui // ui, np.integer | np.float64) # type: ignore[assert-type, operator] # 🐴
629629
assert_type(ui // fc, np.floating)
630630

631631
assert_type(fc // b_py, np.floating)
@@ -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) # type: ignore[operator] # NOTE: mypy workaround
656+
assert_type(fc // fc, np.floating) # type: ignore[operator] # 🐴
657657

658658
###
659659
# __[r]mod__
@@ -1224,7 +1224,7 @@ assert_type(ui % u, np.integer | np.float64)
12241224
assert_type(ui % i, np.signedinteger | np.float64)
12251225
assert_type(ui % f, np.floating)
12261226
ui % c # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
1227-
assert_type(ui % ui, np.integer | np.float64) # type: ignore[assert-type, operator] # NOTE: mypy workaround
1227+
assert_type(ui % ui, np.integer | np.float64) # type: ignore[assert-type, operator] # 🐴
12281228
assert_type(ui % fc, np.floating)
12291229

12301230
assert_type(fc % b_py, np.floating)
@@ -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) # type: ignore[operator] # NOTE: mypy workaround
1255+
assert_type(fc % fc, np.floating) # type: ignore[operator] # 🐴
12561256

12571257
###
12581258
# __[r]divmod__
@@ -1823,7 +1823,7 @@ assert_type(divmod(ui, u), tuple[np.integer | np.float64, np.integer | np.float6
18231823
assert_type(divmod(ui, i), tuple[np.signedinteger | np.float64, np.signedinteger | np.float64])
18241824
assert_type(divmod(ui, f), tuple[np.floating, np.floating])
18251825
divmod(ui, c) # type: ignore[operator] # pyright: ignore[reportArgumentType, reportCallIssue]
1826-
assert_type(divmod(ui, ui), tuple[np.integer | np.float64, np.integer | np.float64]) # type: ignore[assert-type, operator] # NOTE: mypy workaround
1826+
assert_type(divmod(ui, ui), tuple[np.integer | np.float64, np.integer | np.float64]) # type: ignore[assert-type, operator] # 🐴
18271827
assert_type(divmod(ui, fc), tuple[np.floating, np.floating])
18281828

18291829
assert_type(divmod(fc, b_py), tuple[np.floating, np.floating])
@@ -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]) # type: ignore[operator] # NOTE: mypy workaround
1854+
assert_type(divmod(fc, fc), tuple[np.floating, np.floating]) # type: ignore[operator] # 🐴

tool/testgen.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -766,19 +766,20 @@ 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 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-
))
769+
if (
770+
op in self.OPS_ARITHMETIC | self.OPS_MODULAR
771+
and lhs == rhs
772+
and (abstract_arg := self.ABSTRACT_TYPES.get(self.NAMES[lhs]))
773+
):
774+
if abstract_arg == "integer" and " / " not in op:
775+
mypy_ignore = "assert-type, operator"
776+
elif abstract_arg == "inexact":
777+
mypy_ignore = "operator"
778+
else:
779+
mypy_ignore = ""
780+
781+
if mypy_ignore:
782+
stmt = " # ".join((stmt, f"type: ignore[{mypy_ignore}]", "🐴"))
782783

783784
return stmt
784785

0 commit comments

Comments
 (0)