Skip to content

Commit cc61c6d

Browse files
committed
TYP: fix stubtest errors in numpy.lib._twodim_base_impl
Ported from numpy/numtype#245 --- This fixes incorrect parameter names of `tril` and `triu`, and resolves a typing error in the signatures of `eye` and `tri`.
1 parent 82610b4 commit cc61c6d

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

numpy/lib/_twodim_base_impl.pyi

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ from typing import (
1010
import numpy as np
1111
from numpy import (
1212
generic,
13-
number,
1413
timedelta64,
1514
datetime64,
1615
int_,
@@ -56,14 +55,28 @@ __all__ = [
5655
"triu_indices_from",
5756
]
5857

58+
###
59+
5960
_T = TypeVar("_T")
6061
_SCT = TypeVar("_SCT", bound=generic)
62+
_SCT_complex = TypeVar("_SCT_complex", bound=np.complexfloating)
63+
_SCT_inexact = TypeVar("_SCT_inexact", bound=np.inexact)
64+
_SCT_number_co = TypeVar("_SCT_number_co", bound=_Number_co)
6165

6266
# The returned arrays dtype must be compatible with `np.equal`
63-
_MaskFunc: TypeAlias = Callable[
64-
[NDArray[int_], _T],
65-
NDArray[number[Any] | np.bool | timedelta64 | datetime64 | object_],
66-
]
67+
_MaskFunc: TypeAlias = Callable[[NDArray[int_], _T], NDArray[_Number_co | timedelta64 | datetime64 | object_]]
68+
69+
_Int_co: TypeAlias = np.integer | np.bool
70+
_Float_co: TypeAlias = np.floating | _Int_co
71+
_Number_co: TypeAlias = np.number | np.bool
72+
73+
_ArrayLike1D: TypeAlias = _SupportsArray[np.dtype[_SCT]] | Sequence[_SCT]
74+
_ArrayLike1DInt_co: TypeAlias = _SupportsArray[np.dtype[_Int_co]] | Sequence[int | _Int_co]
75+
_ArrayLike1DFloat_co: TypeAlias = _SupportsArray[np.dtype[_Float_co]] | Sequence[float | _Float_co]
76+
_ArrayLike2DFloat_co: TypeAlias = _SupportsArray[np.dtype[_Float_co]] | Sequence[_ArrayLike1DFloat_co]
77+
_ArrayLike1DNumber_co: TypeAlias = _SupportsArray[np.dtype[_Number_co]] | Sequence[complex | _Number_co]
78+
79+
###
6780

6881
@overload
6982
def fliplr(m: _ArrayLike[_SCT]) -> NDArray[_SCT]: ...
@@ -87,13 +100,24 @@ def eye(
87100
like: None | _SupportsArrayFunc = ...,
88101
) -> NDArray[float64]: ...
89102
@overload
103+
def eye(
104+
N: int,
105+
M: None | int,
106+
k: int,
107+
dtype: _DTypeLike[_SCT],
108+
order: _OrderCF = ...,
109+
*,
110+
device: None | L["cpu"] = ...,
111+
like: None | _SupportsArrayFunc = ...,
112+
) -> NDArray[_SCT]: ...
113+
@overload
90114
def eye(
91115
N: int,
92116
M: None | int = ...,
93117
k: int = ...,
94-
dtype: _DTypeLike[_SCT] = ...,
95-
order: _OrderCF = ...,
96118
*,
119+
dtype: _DTypeLike[_SCT],
120+
order: _OrderCF = ...,
97121
device: None | L["cpu"] = ...,
98122
like: None | _SupportsArrayFunc = ...,
99123
) -> NDArray[_SCT]: ...
@@ -129,12 +153,21 @@ def tri(
129153
like: None | _SupportsArrayFunc = ...
130154
) -> NDArray[float64]: ...
131155
@overload
156+
def tri(
157+
N: int,
158+
M: None | int,
159+
k: int,
160+
dtype: _DTypeLike[_SCT],
161+
*,
162+
like: None | _SupportsArrayFunc = ...
163+
) -> NDArray[_SCT]: ...
164+
@overload
132165
def tri(
133166
N: int,
134167
M: None | int = ...,
135168
k: int = ...,
136-
dtype: _DTypeLike[_SCT] = ...,
137169
*,
170+
dtype: _DTypeLike[_SCT],
138171
like: None | _SupportsArrayFunc = ...
139172
) -> NDArray[_SCT]: ...
140173
@overload
@@ -148,14 +181,14 @@ def tri(
148181
) -> NDArray[Any]: ...
149182

150183
@overload
151-
def tril(v: _ArrayLike[_SCT], k: int = ...) -> NDArray[_SCT]: ...
184+
def tril(m: _ArrayLike[_SCT], k: int = 0) -> NDArray[_SCT]: ...
152185
@overload
153-
def tril(v: ArrayLike, k: int = ...) -> NDArray[Any]: ...
186+
def tril(m: ArrayLike, k: int = 0) -> NDArray[Any]: ...
154187

155188
@overload
156-
def triu(v: _ArrayLike[_SCT], k: int = ...) -> NDArray[_SCT]: ...
189+
def triu(m: _ArrayLike[_SCT], k: int = 0) -> NDArray[_SCT]: ...
157190
@overload
158-
def triu(v: ArrayLike, k: int = ...) -> NDArray[Any]: ...
191+
def triu(m: ArrayLike, k: int = 0) -> NDArray[Any]: ...
159192

160193
@overload
161194
def vander( # type: ignore[misc]
@@ -182,37 +215,6 @@ def vander(
182215
increasing: bool = ...,
183216
) -> NDArray[object_]: ...
184217

185-
_Int_co: TypeAlias = np.integer[Any] | np.bool
186-
_Float_co: TypeAlias = np.floating[Any] | _Int_co
187-
_Number_co: TypeAlias = np.number[Any] | np.bool
188-
189-
_ArrayLike1D: TypeAlias = _SupportsArray[np.dtype[_SCT]] | Sequence[_SCT]
190-
_ArrayLike2D: TypeAlias = (
191-
_SupportsArray[np.dtype[_SCT]]
192-
| Sequence[_ArrayLike1D[_SCT]]
193-
)
194-
195-
_ArrayLike1DInt_co: TypeAlias = (
196-
_SupportsArray[np.dtype[_Int_co]]
197-
| Sequence[int | _Int_co]
198-
)
199-
_ArrayLike1DFloat_co: TypeAlias = (
200-
_SupportsArray[np.dtype[_Float_co]]
201-
| Sequence[float | int | _Float_co]
202-
)
203-
_ArrayLike2DFloat_co: TypeAlias = (
204-
_SupportsArray[np.dtype[_Float_co]]
205-
| Sequence[_ArrayLike1DFloat_co]
206-
)
207-
_ArrayLike1DNumber_co: TypeAlias = (
208-
_SupportsArray[np.dtype[_Number_co]]
209-
| Sequence[int | float | complex | _Number_co]
210-
)
211-
212-
_SCT_complex = TypeVar("_SCT_complex", bound=np.complexfloating[Any, Any])
213-
_SCT_inexact = TypeVar("_SCT_inexact", bound=np.inexact[Any])
214-
_SCT_number_co = TypeVar("_SCT_number_co", bound=_Number_co)
215-
216218
@overload
217219
def histogram2d(
218220
x: _ArrayLike1D[_SCT_complex],
@@ -343,7 +345,6 @@ def histogram2d(
343345
NDArray[_SCT_number_co | complex128 | float64],
344346
NDArray[_SCT_number_co | complex128 | float64],
345347
]: ...
346-
347348
@overload
348349
def histogram2d(
349350
x: _ArrayLike1DNumber_co,

0 commit comments

Comments
 (0)