8000 ENH: Add annotations for `np.lib.type_check` · numpy/numpy@d3809f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit d3809f2

Browse files
Bas van BeekBvB93
Bas van Beek
authored andcommitted
ENH: Add annotations for np.lib.type_check
1 parent 7d8fada commit d3809f2

File tree

1 file changed

+231
-15
lines changed

1 file changed

+231
-15
lines changed

numpy/lib/type_check.pyi

Lines changed: 231 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,235 @@
1-
from typing import List
1+
import sys
2+
from typing import (
3+
Any,
4+
Container,
5+
Iterable,
6+
List,
7+
overload,
8+
Type,
9+
TypeVar,
10+
)
11+
12+
from numpy import (
13+
dtype,
14+
generic,
15+
bool_,
16+
floating,
17+
float64,
18+
complexfloating,
19+
integer,
20+
)
21+
22+
from numpy.typing import (
23+
ArrayLike,
24+
DTypeLike,
25+
NBitBase,
26+
NDArray,
27+
_64Bit,
28+
_SupportsDType,
29+
_ScalarLike_co,
30+
_NestedSequence,
31+
_SupportsArray,
32+
_DTypeLikeComplex,
33+
)
34+
35+
if sys.version_info >= (3, 8):
36+
from typing import Protocol, Literal as L
37+
else:
38+
from typing_extensions import Protocol, Literal as L
39+
40+
_T = TypeVar("_T")
41+
_T_co = TypeVar("_T_co", covariant=True)
42+
_SCT = TypeVar("_SCT", bound=generic)
43+
_NBit1 = TypeVar("_NBit1", bound=NBitBase)
44+
_NBit2 = TypeVar("_NBit2", bound=NBitBase)
45+
46+
_ArrayLike = _NestedSequence[_SupportsArray[dtype[_SCT]]]
47+
48+
class _SupportsReal(Protocol[_T_co]):
49+
@property
50+
def real(self) -> _T_co: ...
51+
52+
class _SupportsImag(Protocol[_T_co]):
53+
@property
54+
def imag(self) -> _T_co: ...
255

356
__all__: List[str]
457

5-
def mintypecode(typechars, typeset=..., default=...): ...
6-
def asfarray(a, dtype = ...): ...
7-
def real(val): ...
8-
def imag(val): ...
9-
def iscomplex(x): ...
10-
def isreal(x): ...
11-
def iscomplexobj(x): ...
12-
def isrealobj(x): ...
13-
def nan_to_num(x, copy=..., nan=..., posinf=..., neginf=...): ...
14-
def real_if_close(a, tol=...): ...
15-
def typename(char): ...
16-
def common_type(*arrays): ...
17-
18-
# NOTE: Deprecated
58+
def mintypecode(
59+
typechars: Iterable[str | ArrayLike],
60+
typeset: Container[str] = ...,
61+
default: str = ...,
62+
) -> str: ...
63+
64+
# `asfarray` ignores dtypes if they're not inexact
65+
66+
@overload
67+
def asfarray(
68+
a: object,
69+
dtype: None | Type[float] = ...,
70+
) -> NDArray[float64]: ...
71+
@overload
72+
def asfarray( # type: ignore[misc]
73+
a: Any,
74+
dtype: _DTypeLikeComplex,
75+
) -> NDArray[complexfloating[Any, Any]]: ...
76+
@overload
77+
def asfarray(
78+
a: Any,
79+
dtype: DTypeLike,
80+
) -> NDArray[floating[Any]]: ...
81+
82+
@overload
83+
def real(val: _SupportsReal[_T]) -> _T: ...
84+
@overload
85+
def real(val: ArrayLike) -> NDArray[Any]: ...
86+
87+
@overload
88+
def imag(val: _SupportsImag[_T]) -> _T: ...
89+
@overload
90+
def imag(val: ArrayLike) -> NDArray[Any]: ...
91+
92+
@overload
93+
def iscomplex(x: _ScalarLike_co) -> bool_: ... # type: ignore[misc]
94+
@overload
95+
def iscomplex(x: ArrayLike) -> NDArray[bool_]: ...
96+
97+
@overload
98+
def isreal(x: _ScalarLike_co) -> bool_: ... # type: ignore[misc]
99+
@overload
100+
def isreal(x: ArrayLike) -> NDArray[bool_]: ...
101+
102+
def iscomplexobj(x: _SupportsDType[dtype[Any]] | ArrayLike) -> bool: ...
103+
104+
def isrealobj(x: _SupportsDType[dtype[Any]] | ArrayLike) -> bool: ...
105+
106+
@overload
107+
def nan_to_num( # type: ignore[misc]
108+
x: _SCT,
109+
copy: bool = ...,
110+
nan: float = ...,
111+
posinf: None | float = ...,
112+
neginf: None | float = ...,
113+
) -> _SCT: ...
114+
@overload
115+
def nan_to_num(
116+
x: _ScalarLike_co,
117+
copy: bool = ...,
118+
nan: float = ...,
119+
posinf: None | float = ...,
120+
neginf: None | float = ...,
121+
) -> Any: ...
122+
@overload
123+
def nan_to_num(
124+
x: _ArrayLike[_SCT],
125+
copy: bool = ...,
126+
nan: float = ...,
127+
posinf: None | float = ...,
128+
neginf: None | float = ...,
129+
) -> NDArray[_SCT]: ...
130+
@overload
131+
def nan_to_num(
132+
x: ArrayLike,
133+
copy: bool = ...,
134+
nan: float = ...,
135+
posinf: None | float = ...,
136+
neginf: None | float = ...,
137+
) -> NDArray[Any]: ...
138+
139+
# If one passes a complex array to `real_if_close`, then one is reasonably
140+
# expected to verify the output dtype (so we can return an unsafe union here)
141+
142+
@overload
143+
def real_if_close( # type: ignore[misc]
144+
a: _ArrayLike[complexfloating[_NBit1, _NBit1]],
145+
tol: float = ...,
146+
) -> NDArray[floating[_NBit1]] | NDArray[complexfloating[_NBit1, _NBit1]]: ...
147+
@overload
148+
def real_if_close(
149+
a: _ArrayLike[_SCT],
150+
tol: float = ...,
151+
) -> NDArray[_SCT]: ...
152+
@overload
153+
def real_if_close(
154+
a: ArrayLike,
155+
tol: float = ...,
156+
) -> NDArray[Any]: ...
157+
158+
# NOTE: deprecated
19159
# def asscalar(a): ...
160+
161+
@overload
162+
def typename(char: L['S1']) -> L['character']: ...
163+
@overload
164+
def typename(char: L['?']) -> L['bool']: ...
165+
@overload
166+
def typename(char: L['b']) -> L['signed char']: ...
167+
@overload
168+
def typename(char: L['B']) -> L['unsigned char']: ...
169+
@overload
170+
def typename(char: L['h']) -> L['short']: ...
171+
@overload
172+
def typename(char: L['H']) -> L['unsigned short']: ...
173+
@overload
174+
def typename(char: L['i']) -> L['integer']: ...
175+
@overload
176+
def typename(char: L['I']) -> L['unsigned integer']: ...
177+
@overload
178+
def typename(char: L['l']) -> L['long integer']: ...
179+
@overload
180+
def typename(char: L['L']) -> L['unsigned long integer']: ...
181+
@overload
182+
def typename(char: L['q']) -> L['long long integer']: ...
183+
@overload
184+
def typename(char: L['Q']) -> L['unsigned long long integer']: ...
185+
@overload
186+
def typename(char: L['f']) -> L['single precision']: ...
187+
@overload
188+
def typename(char: L['d']) -> L['double precision']: ...
189+
@overload
190+
def typename(char: L['g']) -> L['long precision']: ...
191+
@overload
192+
def typename(char: L['F']) -> L['complex single precision']: ...
193+
@overload
194+
def typename(char: L['D']) -> L['complex double precision']: ...
195+
@overload
196+
def typename(char: L['G']) -> L['complex long double precision']: ...
197+
@overload
198+
def typename(char: L['S']) -> L['string']: ...
199+
@overload
200+
def typename(char: L['U']) -> L['unicode']: ...
201+
@overload
202+
def typename(char: L['V']) -> L['void']: ...
203+
@overload
204+
def typename(char: L['O']) -> L['object']: ...
205+
206+
@overload
207+
def common_type( # type: ignore[misc]
208+
*arrays: _SupportsDType[dtype[
209+
integer[Any]
210+
]]
211+
) -> Type[floating[_64Bit]]: ...
212+
@overload
213+
def common_type( # type: ignore[misc]
214+
*arrays: _SupportsDType[dtype[
215+
floating[_NBit1]
216+
]]
217+
) -> Type[floating[_NBit1]]: ...
218+
@overload
219+
def common_type( # type: ignore[misc]
220+
*arrays: _SupportsDType[dtype[
221+
integer[Any] | floating[_NBit1]
222+
]]
223+
) -> Type[floating[_NBit1 | _64Bit]]: ...
224+
@overload
225+
def common_type( # type: ignore[misc]
226+
*arrays: _SupportsDType[dtype[
227+
floating[_NBit1] | complexfloating[_NBit2, _NBit2]
228+
]]
229+
) -> Type[complexfloating[_NBit1 | _NBit2, _NBit1 | _NBit2]]: ...
230+
@overload
231+
def common_type(
232+
*arrays: _SupportsDType[dtype[
233+
integer[Any] | floating[_NBit1] | complexfloating[_NBit2, _NBit2]
234+
]]
235+
) -> Type[complexfloating[_64Bit | _NBit1 | _NBit2, _64Bit | _NBit1 | _NBit2]]: ...

0 commit comments

Comments
 (0)
0