Skip to content

Commit ba8db50

Browse files
authored
Merge pull request #509 from numpy/ndarray-arithmetic-binop-fixes
2 parents b9edbf0 + 67da6f4 commit ba8db50

File tree

19 files changed

+5129
-837
lines changed

19 files changed

+5129
-837
lines changed

src/_numtype/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from typing_extensions import Never, Protocol, TypeAliasType, TypeVar, Unpack
1111
import numpy as np
1212
from numpy._typing import _NestedSequence
1313

14+
from . import op as op
1415
from ._dtype import (
1516
ToDType as ToDType,
1617
ToDTypeBool as ToDTypeBool,

src/_numtype/_nep50.pyi

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ from typing import Any, Protocol, TypeAlias, type_check_only
66
from typing_extensions import TypeAliasType, TypeVar
77

88
import numpy as np
9+
from numpy._typing import _NestedSequence
910

1011
__all__ = [
1112
"Casts",
@@ -22,6 +23,7 @@ __all__ = [
2223

2324
###
2425

26+
_LikeT = TypeVar("_LikeT")
2527
_LikeT_co = TypeVar("_LikeT_co", covariant=True)
2628

2729
_BuitinT = TypeVar("_BuitinT")
@@ -125,14 +127,16 @@ class _LikeScalar(Protocol[_LikeT_co]):
125127

126128
###
127129

130+
_SequenceND: TypeAlias = _LikeT | _NestedSequence[_LikeT]
131+
128132
Casts = TypeAliasType(
129133
"Casts",
130-
_LikeNumeric[_CanNEP50[_ScalarOutT, Any, Any], _ShapeT],
134+
_SequenceND[_LikeNumeric[_CanNEP50[_ScalarOutT, Any, Any], _ShapeT]],
131135
type_params=(_ScalarOutT, _ShapeT),
132136
)
133137
CastsArray = TypeAliasType(
134138
"CastsArray",
135-
_LikeArray[_CanNEP50[_ScalarOutT, Any, Any], _ShapeT],
139+
_SequenceND[_LikeArray[_CanNEP50[_ScalarOutT, Any, Any], _ShapeT]],
136140
type_params=(_ScalarOutT, _ShapeT),
137141
)
138142
CastsScalar = TypeAliasType(
@@ -154,12 +158,12 @@ _CastWith: TypeAlias = (
154158
)
155159
CastsWith = TypeAliasType(
156160
"CastsWith",
157-
_LikeNumeric[_CastWith[_ScalarInT, _ScalarOutT], _ShapeT],
161+
_SequenceND[_LikeNumeric[_CastWith[_ScalarInT, _ScalarOutT], _ShapeT]],
158162
type_params=(_ScalarInT, _ScalarOutT, _ShapeT),
159163
)
160164
CastsWithArray = TypeAliasType(
161165
"CastsWithArray",
162-
_LikeArray[_CastWith[_ScalarInT, _ScalarOutT], _ShapeT],
166+
_SequenceND[_LikeArray[_CastWith[_ScalarInT, _ScalarOutT], _ShapeT]],
163167
type_params=(_ScalarInT, _ScalarOutT, _ShapeT),
164168
)
165169
CastsWithScalar = TypeAliasType(

src/_numtype/op.pyi

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
from typing import Any, Protocol
2+
from typing_extensions import TypeVar, override
3+
4+
__all__ = [ # noqa: RUF022
5+
"CanEq",
6+
"CanNe",
7+
"CanLt",
8+
"CanLe",
9+
"CanGt",
10+
"CanGe",
11+
"CanAdd",
12+
"CanRAdd",
13+
"CanSub",
14+
"CanRSub",
15+
"CanMul",
16+
"CanRMul",
17+
"CanMatmul",
18+
"CanRMatmul",
19+
"CanPow",
20+
"CanRPow",
21+
"CanTruediv",
22+
"CanRTruediv",
23+
"CanFloordiv",
24+
"CanRFloordiv",
25+
"CanMod",
26+
"CanRMod",
27+
"CanDivmod",
28+
"CanRDivmod",
29+
"CanLshift",
30+
"CanRLshift",
31+
"CanRshift",
32+
"CanRRshift",
33+
"CanAnd",
34+
"CanRAnd",
35+
"CanXor",
36+
"CanRXor",
37+
"CanOr",
38+
"CanROr",
39+
] # fmt:
40+
41+
###
42+
43+
_T_contra = TypeVar("_T_contra", contravariant=True, default=object)
44+
_T_co = TypeVar("_T_co", covariant=True, default=Any)
45+
46+
###
47+
48+
class CanEq(Protocol[_T_contra, _T_co]):
49+
@override
50+
def __eq__(self, x: _T_contra, /) -> _T_co: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
51+
52+
class CanNe(Protocol[_T_contra, _T_co]):
53+
@override
54+
def __ne__(self, x: _T_contra, /) -> _T_co: ... # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride]
55+
56+
class CanLt(Protocol[_T_contra, _T_co]):
57+
def __lt__(self, x: _T_contra, /) -> _T_co: ...
58+
59+
class CanLe(Protocol[_T_contra, _T_co]):
60+
def __le__(self, x: _T_contra, /) -> _T_co: ...
61+
62+
class CanGt(Protocol[_T_contra, _T_co]):
63+
def __gt__(self, x: _T_contra, /) -> _T_co: ...
64+
65+
class CanGe(Protocol[_T_contra, _T_co]):
66+
def __ge__(self, x: _T_contra, /) -> _T_co: ...
67+
68+
###
69+
70+
class CanAdd(Protocol[_T_contra, _T_co]):
71+
def __add__(self, x: _T_contra, /) -> _T_co: ...
72+
73+
class CanRAdd(Protocol[_T_contra, _T_co]):
74+
def __radd__(self, x: _T_contra, /) -> _T_co: ...
75+
76+
class CanSub(Protocol[_T_contra, _T_co]):
77+
def __sub__(self, x: _T_contra, /) -> _T_co: ...
78+
79+
class CanRSub(Protocol[_T_contra, _T_co]):
80+
def __rsub__(self, x: _T_contra, /) -> _T_co: ...
81+
82+
class CanMul(Protocol[_T_contra, _T_co]):
83+
def __mul__(self, x: _T_contra, /) -> _T_co: ...
84+
85+
class CanRMul(Protocol[_T_contra, _T_co]):
86+
def __rmul__(self, x: _T_contra, /) -> _T_co: ...
87+
88+
class CanMatmul(Protocol[_T_contra, _T_co]):
89+
def __matmul__(self, x: _T_contra, /) -> _T_co: ...
90+
91+
class CanRMatmul(Protocol[_T_contra, _T_co]):
92+
def __rmatmul__(self, x: _T_contra, /) -> _T_co: ...
93+
94+
class CanPow(Protocol[_T_contra, _T_co]):
95+
def __pow__(self, exp: _T_contra, /) -> _T_co: ...
96+
97+
class CanRPow(Protocol[_T_contra, _T_co]):
98+
def __rpow__(self, x: _T_contra, /) -> _T_co: ...
99+
100+
class CanTruediv(Protocol[_T_contra, _T_co]):
101+
def __truediv__(self, x: _T_contra, /) -> _T_co: ...
102+
103+
class CanRTruediv(Protocol[_T_contra, _T_co]):
104+
def __rtruediv__(self, x: _T_contra, /) -> _T_co: ...
105+
106+
###
107+
108+
class CanFloordiv(Protocol[_T_contra, _T_co]):
109+
def __floordiv__(self, x: _T_contra, /) -> _T_co: ...
110+
111+
class CanRFloordiv(Protocol[_T_contra, _T_co]):
112+
def __rfloordiv__(self, x: _T_contra, /) -> _T_co: ...
113+
114+
class CanMod(Protocol[_T_contra, _T_co]):
115+
def __mod__(self, x: _T_contra, /) -> _T_co: ...
116+
117+
class CanRMod(Protocol[_T_contra, _T_co]):
118+
def __rmod__(self, x: _T_contra, /) -> _T_co: ...
119+
120+
class CanDivmod(Protocol[_T_contra, _T_co]):
121+
def __divmod__(self, x: _T_contra, /) -> _T_co: ...
122+
123+
class CanRDivmod(Protocol[_T_contra, _T_co]):
124+
def __rdivmod__(self, x: _T_contra, /) -> _T_co: ...
125+
126+
###
127+
128+
class CanLshift(Protocol[_T_contra, _T_co]):
129+
def __lshift__(self, x: _T_contra, /) -> _T_co: ...
130+
131+
class CanRLshift(Protocol[_T_contra, _T_co]):
132+
def __rlshift__(self, x: _T_contra, /) -> _T_co: ...
133+
134+
class CanRshift(Protocol[_T_contra, _T_co]):
135+
def __rshift__(self, x: _T_contra, /) -> _T_co: ...
136+
137+
class CanRRshift(Protocol[_T_contra, _T_co]):
138+
def __rrshift__(self, x: _T_contra, /) -> _T_co: ...
139+
140+
class CanAnd(Protocol[_T_contra, _T_co]):
141+
def __and__(self, x: _T_contra, /) -> _T_co: ...
142+
143+
class CanRAnd(Protocol[_T_contra, _T_co]):
144+
def __rand__(self, x: _T_contra, /) -> _T_co: ...
145+
146+
class CanXor(Protocol[_T_contra, _T_co]):
147+
def __xor__(self, x: _T_contra, /) -> _T_co: ...
148+
149+
class CanRXor(Protocol[_T_contra, _T_co]):
150+
def __rxor__(self, x: _T_contra, /) -> _T_co: ...
151+
152+
class CanOr(Protocol[_T_contra, _T_co]):
153+
def __or__(self, x: _T_contra, /) -> _T_co: ...
154+
155+
class CanROr(Protocol[_T_contra, _T_co]):
156+
def __ror__(self, x: _T_contra, /) -> _T_co: ...

src/numpy-stubs/@test/generated/.ruff.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ line-length = 120
55
extend-ignore = [
66
"PTH", # flake8-use-pathlib
77
"B015", # flake8-bugbear useless-comparison
8+
"B018", # flake8-bugbear useless-expression
89
"PYI015", # flake8-pyi: assignment-default-in-stub
910
"PYI017", # flake8-pyi: complex-assignment-in-stub
1011
"SLF001", # flake8-self: private-member-access

src/numpy-stubs/@test/generated/ndarray_invert.pyi

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)