type_check.pyi
1 from collections.abc import Container, Iterable 2 from typing import ( 3 Literal as L, 4 Any, 5 overload, 6 TypeVar, 7 Protocol, 8 ) 9 10 from numpy import ( 11 dtype, 12 generic, 13 bool_, 14 floating, 15 float64, 16 complexfloating, 17 integer, 18 ) 19 20 from numpy._typing import ( 21 ArrayLike, 22 DTypeLike, 23 NBitBase, 24 NDArray, 25 _64Bit, 26 _SupportsDType, 27 _ScalarLike_co, 28 _ArrayLike, 29 _DTypeLikeComplex, 30 ) 31 32 _T = TypeVar("_T") 33 _T_co = TypeVar("_T_co", covariant=True) 34 _SCT = TypeVar("_SCT", bound=generic) 35 _NBit1 = TypeVar("_NBit1", bound=NBitBase) 36 _NBit2 = TypeVar("_NBit2", bound=NBitBase) 37 38 class _SupportsReal(Protocol[_T_co]): 39 @property 40 def real(self) -> _T_co: ... 41 42 class _SupportsImag(Protocol[_T_co]): 43 @property 44 def imag(self) -> _T_co: ... 45 46 __all__: list[str] 47 48 def mintypecode( 49 typechars: Iterable[str | ArrayLike], 50 typeset: Container[str] = ..., 51 default: str = ..., 52 ) -> str: ... 53 54 # `asfarray` ignores dtypes if they're not inexact 55 56 @overload 57 def asfarray( 58 a: object, 59 dtype: None | type[float] = ..., 60 ) -> NDArray[float64]: ... 61 @overload 62 def asfarray( # type: ignore[misc] 63 a: Any, 64 dtype: _DTypeLikeComplex, 65 ) -> NDArray[complexfloating[Any, Any]]: ... 66 @overload 67 def asfarray( 68 a: Any, 69 dtype: DTypeLike, 70 ) -> NDArray[floating[Any]]: ... 71 72 @overload 73 def real(val: _SupportsReal[_T]) -> _T: ... 74 @overload 75 def real(val: ArrayLike) -> NDArray[Any]: ... 76 77 @overload 78 def imag(val: _SupportsImag[_T]) -> _T: ... 79 @overload 80 def imag(val: ArrayLike) -> NDArray[Any]: ... 81 82 @overload 83 def iscomplex(x: _ScalarLike_co) -> bool_: ... # type: ignore[misc] 84 @overload 85 def iscomplex(x: ArrayLike) -> NDArray[bool_]: ... 86 87 @overload 88 def isreal(x: _ScalarLike_co) -> bool_: ... # type: ignore[misc] 89 @overload 90 def isreal(x: ArrayLike) -> NDArray[bool_]: ... 91 92 def iscomplexobj(x: _SupportsDType[dtype[Any]] | ArrayLike) -> bool: ... 93 94 def isrealobj(x: _SupportsDType[dtype[Any]] | ArrayLike) -> bool: ... 95 96 @overload 97 def nan_to_num( # type: ignore[misc] 98 x: _SCT, 99 copy: bool = ..., 100 nan: float = ..., 101 posinf: None | float = ..., 102 neginf: None | float = ..., 103 ) -> _SCT: ... 104 @overload 105 def nan_to_num( 106 x: _ScalarLike_co, 107 copy: bool = ..., 108 nan: float = ..., 109 posinf: None | float = ..., 110 neginf: None | float = ..., 111 ) -> Any: ... 112 @overload 113 def nan_to_num( 114 x: _ArrayLike[_SCT], 115 copy: bool = ..., 116 nan: float = ..., 117 posinf: None | float = ..., 118 neginf: None | float = ..., 119 ) -> NDArray[_SCT]: ... 120 @overload 121 def nan_to_num( 122 x: ArrayLike, 123 copy: bool = ..., 124 nan: float = ..., 125 posinf: None | float = ..., 126 neginf: None | float = ..., 127 ) -> NDArray[Any]: ... 128 129 # If one passes a complex array to `real_if_close`, then one is reasonably 130 # expected to verify the output dtype (so we can return an unsafe union here) 131 132 @overload 133 def real_if_close( # type: ignore[misc] 134 a: _ArrayLike[complexfloating[_NBit1, _NBit1]], 135 tol: float = ..., 136 ) -> NDArray[floating[_NBit1]] | NDArray[complexfloating[_NBit1, _NBit1]]: ... 137 @overload 138 def real_if_close( 139 a: _ArrayLike[_SCT], 140 tol: float = ..., 141 ) -> NDArray[_SCT]: ... 142 @overload 143 def real_if_close( 144 a: ArrayLike, 145 tol: float = ..., 146 ) -> NDArray[Any]: ... 147 148 @overload 149 def typename(char: L['S1']) -> L['character']: ... 150 @overload 151 def typename(char: L['?']) -> L['bool']: ... 152 @overload 153 def typename(char: L['b']) -> L['signed char']: ... 154 @overload 155 def typename(char: L['B']) -> L['unsigned char']: ... 156 @overload 157 def typename(char: L['h']) -> L['short']: ... 158 @overload 159 def typename(char: L['H']) -> L['unsigned short']: ... 160 @overload 161 def typename(char: L['i']) -> L['integer']: ... 162 @overload 163 def typename(char: L['I']) -> L['unsigned integer']: ... 164 @overload 165 def typename(char: L['l']) -> L['long integer']: ... 166 @overload 167 def typename(char: L['L']) -> L['unsigned long integer']: ... 168 @overload 169 def typename(char: L['q']) -> L['long long integer']: ... 170 @overload 171 def typename(char: L['Q']) -> L['unsigned long long integer']: ... 172 @overload 173 def typename(char: L['f']) -> L['single precision']: ... 174 @overload 175 def typename(char: L['d']) -> L['double precision']: ... 176 @overload 177 def typename(char: L['g']) -> L['long precision']: ... 178 @overload 179 def typename(char: L['F']) -> L['complex single precision']: ... 180 @overload 181 def typename(char: L['D']) -> L['complex double precision']: ... 182 @overload 183 def typename(char: L['G']) -> L['complex long double precision']: ... 184 @overload 185 def typename(char: L['S']) -> L['string']: ... 186 @overload 187 def typename(char: L['U']) -> L['unicode']: ... 188 @overload 189 def typename(char: L['V']) -> L['void']: ... 190 @overload 191 def typename(char: L['O']) -> L['object']: ... 192 193 @overload 194 def common_type( # type: ignore[misc] 195 *arrays: _SupportsDType[dtype[ 196 integer[Any] 197 ]] 198 ) -> type[floating[_64Bit]]: ... 199 @overload 200 def common_type( # type: ignore[misc] 201 *arrays: _SupportsDType[dtype[ 202 floating[_NBit1] 203 ]] 204 ) -> type[floating[_NBit1]]: ... 205 @overload 206 def common_type( # type: ignore[misc] 207 *arrays: _SupportsDType[dtype[ 208 integer[Any] | floating[_NBit1] 209 ]] 210 ) -> type[floating[_NBit1 | _64Bit]]: ... 211 @overload 212 def common_type( # type: ignore[misc] 213 *arrays: _SupportsDType[dtype[ 214 floating[_NBit1] | complexfloating[_NBit2, _NBit2] 215 ]] 216 ) -> type[complexfloating[_NBit1 | _NBit2, _NBit1 | _NBit2]]: ... 217 @overload 218 def common_type( 219 *arrays: _SupportsDType[dtype[ 220 integer[Any] | floating[_NBit1] | complexfloating[_NBit2, _NBit2] 221 ]] 222 ) -> type[complexfloating[_64Bit | _NBit1 | _NBit2, _64Bit | _NBit1 | _NBit2]]: ...