_callable.pyi
1 """ 2 A module with various ``typing.Protocol`` subclasses that implement 3 the ``__call__`` magic method. 4 5 See the `Mypy documentation`_ on protocols for more details. 6 7 .. _`Mypy documentation`: https://mypy.readthedocs.io/en/stable/protocols.html#callback-protocols 8 9 """ 10 11 from __future__ import annotations 12 13 from typing import ( 14 TypeVar, 15 overload, 16 Any, 17 NoReturn, 18 Protocol, 19 ) 20 21 from numpy import ( 22 ndarray, 23 dtype, 24 generic, 25 bool_, 26 timedelta64, 27 number, 28 integer, 29 unsignedinteger, 30 signedinteger, 31 int8, 32 int_, 33 floating, 34 float64, 35 complexfloating, 36 complex128, 37 ) 38 from ._nbit import _NBitInt, _NBitDouble 39 from ._scalars import ( 40 _BoolLike_co, 41 _IntLike_co, 42 _FloatLike_co, 43 _NumberLike_co, 44 ) 45 from . import NBitBase 46 from ._generic_alias import NDArray 47 from ._nested_sequence import _NestedSequence 48 49 _T1 = TypeVar("_T1") 50 _T2 = TypeVar("_T2") 51 _T1_contra = TypeVar("_T1_contra", contravariant=True) 52 _T2_contra = TypeVar("_T2_contra", contravariant=True) 53 _2Tuple = tuple[_T1, _T1] 54 55 _NBit1 = TypeVar("_NBit1", bound=NBitBase) 56 _NBit2 = TypeVar("_NBit2", bound=NBitBase) 57 58 _IntType = TypeVar("_IntType", bound=integer) 59 _FloatType = TypeVar("_FloatType", bound=floating) 60 _NumberType = TypeVar("_NumberType", bound=number) 61 _NumberType_co = TypeVar("_NumberType_co", covariant=True, bound=number) 62 _GenericType_co = TypeVar("_GenericType_co", covariant=True, bound=generic) 63 64 class _BoolOp(Protocol[_GenericType_co]): 65 @overload 66 def __call__(self, other: _BoolLike_co, /) -> _GenericType_co: ... 67 @overload # platform dependent 68 def __call__(self, other: int, /) -> int_: ... 69 @overload 70 def __call__(self, other: float, /) -> float64: ... 71 @overload 72 def __call__(self, other: complex, /) -> complex128: ... 73 @overload 74 def __call__(self, other: _NumberType, /) -> _NumberType: ... 75 76 class _BoolBitOp(Protocol[_GenericType_co]): 77 @overload 78 def __call__(self, other: _BoolLike_co, /) -> _GenericType_co: ... 79 @overload # platform dependent 80 def __call__(self, other: int, /) -> int_: ... 81 @overload 82 def __call__(self, other: _IntType, /) -> _IntType: ... 83 84 class _BoolSub(Protocol): 85 # Note that `other: bool_` is absent here 86 @overload 87 def __call__(self, other: bool, /) -> NoReturn: ... 88 @overload # platform dependent 89 def __call__(self, other: int, /) -> int_: ... 90 @overload 91 def __call__(self, other: float, /) -> float64: ... 92 @overload 93 def __call__(self, other: complex, /) -> complex128: ... 94 @overload 95 def __call__(self, other: _NumberType, /) -> _NumberType: ... 96 97 class _BoolTrueDiv(Protocol): 98 @overload 99 def __call__(self, other: float | _IntLike_co, /) -> float64: ... 100 @overload 101 def __call__(self, other: complex, /) -> complex128: ... 102 @overload 103 def __call__(self, other: _NumberType, /) -> _NumberType: ... 104 105 class _BoolMod(Protocol): 106 @overload 107 def __call__(self, other: _BoolLike_co, /) -> int8: ... 108 @overload # platform dependent 109 def __call__(self, other: int, /) -> int_: ... 110 @overload 111 def __call__(self, other: float, /) -> float64: ... 112 @overload 113 def __call__(self, other: _IntType, /) -> _IntType: ... 114 @overload 115 def __call__(self, other: _FloatType, /) -> _FloatType: ... 116 117 class _BoolDivMod(Protocol): 118 @overload 119 def __call__(self, other: _BoolLike_co, /) -> _2Tuple[int8]: ... 120 @overload # platform dependent 121 def __call__(self, other: int, /) -> _2Tuple[int_]: ... 122 @overload 123 def __call__(self, other: float, /) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... 124 @overload 125 def __call__(self, other: _IntType, /) -> _2Tuple[_IntType]: ... 126 @overload 127 def __call__(self, other: _FloatType, /) -> _2Tuple[_FloatType]: ... 128 129 class _TD64Div(Protocol[_NumberType_co]): 130 @overload 131 def __call__(self, other: timedelta64, /) -> _NumberType_co: ... 132 @overload 133 def __call__(self, other: _BoolLike_co, /) -> NoReturn: ... 134 @overload 135 def __call__(self, other: _FloatLike_co, /) -> timedelta64: ... 136 137 class _IntTrueDiv(Protocol[_NBit1]): 138 @overload 139 def __call__(self, other: bool, /) -> floating[_NBit1]: ... 140 @overload 141 def __call__(self, other: int, /) -> floating[_NBit1 | _NBitInt]: ... 142 @overload 143 def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... 144 @overload 145 def __call__( 146 self, other: complex, /, 147 ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... 148 @overload 149 def __call__(self, other: integer[_NBit2], /) -> floating[_NBit1 | _NBit2]: ... 150 151 class _UnsignedIntOp(Protocol[_NBit1]): 152 # NOTE: `uint64 + signedinteger -> float64` 153 @overload 154 def __call__(self, other: bool, /) -> unsignedinteger[_NBit1]: ... 155 @overload 156 def __call__( 157 self, other: int | signedinteger[Any], / 158 ) -> Any: ... 159 @overload 160 def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... 161 @overload 162 def __call__( 163 self, other: complex, /, 164 ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... 165 @overload 166 def __call__( 167 self, other: unsignedinteger[_NBit2], / 168 ) -> unsignedinteger[_NBit1 | _NBit2]: ... 169 170 class _UnsignedIntBitOp(Protocol[_NBit1]): 171 @overload 172 def __call__(self, other: bool, /) -> unsignedinteger[_NBit1]: ... 173 @overload 174 def __call__(self, other: int, /) -> signedinteger[Any]: ... 175 @overload 176 def __call__(self, other: signedinteger[Any], /) -> signedinteger[Any]: ... 177 @overload 178 def __call__( 179 self, other: unsignedinteger[_NBit2], / 180 ) -> unsignedinteger[_NBit1 | _NBit2]: ... 181 182 class _UnsignedIntMod(Protocol[_NBit1]): 183 @overload 184 def __call__(self, other: bool, /) -> unsignedinteger[_NBit1]: ... 185 @overload 186 def __call__( 187 self, other: int | signedinteger[Any], / 188 ) -> Any: ... 189 @overload 190 def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... 191 @overload 192 def __call__( 193 self, other: unsignedinteger[_NBit2], / 194 ) -> unsignedinteger[_NBit1 | _NBit2]: ... 195 196 class _UnsignedIntDivMod(Protocol[_NBit1]): 197 @overload 198 def __call__(self, other: bool, /) -> _2Tuple[signedinteger[_NBit1]]: ... 199 @overload 200 def __call__( 201 self, other: int | signedinteger[Any], / 202 ) -> _2Tuple[Any]: ... 203 @overload 204 def __call__(self, other: float, /) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... 205 @overload 206 def __call__( 207 self, other: unsignedinteger[_NBit2], / 208 ) -> _2Tuple[unsignedinteger[_NBit1 | _NBit2]]: ... 209 210 class _SignedIntOp(Protocol[_NBit1]): 211 @overload 212 def __call__(self, other: bool, /) -> signedinteger[_NBit1]: ... 213 @overload 214 def __call__(self, other: int, /) -> signedinteger[_NBit1 | _NBitInt]: ... 215 @overload 216 def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... 217 @overload 218 def __call__( 219 self, other: complex, /, 220 ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... 221 @overload 222 def __call__( 223 self, other: signedinteger[_NBit2], /, 224 ) -> signedinteger[_NBit1 | _NBit2]: ... 225 226 class _SignedIntBitOp(Protocol[_NBit1]): 227 @overload 228 def __call__(self, other: bool, /) -> signedinteger[_NBit1]: ... 229 @overload 230 def __call__(self, other: int, /) -> signedinteger[_NBit1 | _NBitInt]: ... 231 @overload 232 def __call__( 233 self, other: signedinteger[_NBit2], /, 234 ) -> signedinteger[_NBit1 | _NBit2]: ... 235 236 class _SignedIntMod(Protocol[_NBit1]): 237 @overload 238 def __call__(self, other: bool, /) -> signedinteger[_NBit1]: ... 239 @overload 240 def __call__(self, other: int, /) -> signedinteger[_NBit1 | _NBitInt]: ... 241 @overload 242 def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... 243 @overload 244 def __call__( 245 self, other: signedinteger[_NBit2], /, 246 ) -> signedinteger[_NBit1 | _NBit2]: ... 247 248 class _SignedIntDivMod(Protocol[_NBit1]): 249 @overload 250 def __call__(self, other: bool, /) -> _2Tuple[signedinteger[_NBit1]]: ... 251 @overload 252 def __call__(self, other: int, /) -> _2Tuple[signedinteger[_NBit1 | _NBitInt]]: ... 253 @overload 254 def __call__(self, other: float, /) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... 255 @overload 256 def __call__( 257 self, other: signedinteger[_NBit2], /, 258 ) -> _2Tuple[signedinteger[_NBit1 | _NBit2]]: ... 259 260 class _FloatOp(Protocol[_NBit1]): 261 @overload 262 def __call__(self, other: bool, /) -> floating[_NBit1]: ... 263 @overload 264 def __call__(self, other: int, /) -> floating[_NBit1 | _NBitInt]: ... 265 @overload 266 def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... 267 @overload 268 def __call__( 269 self, other: complex, /, 270 ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... 271 @overload 272 def __call__( 273 self, other: integer[_NBit2] | floating[_NBit2], / 274 ) -> floating[_NBit1 | _NBit2]: ... 275 276 class _FloatMod(Protocol[_NBit1]): 277 @overload 278 def __call__(self, other: bool, /) -> floating[_NBit1]: ... 279 @overload 280 def __call__(self, other: int, /) -> floating[_NBit1 | _NBitInt]: ... 281 @overload 282 def __call__(self, other: float, /) -> floating[_NBit1 | _NBitDouble]: ... 283 @overload 284 def __call__( 285 self, other: integer[_NBit2] | floating[_NBit2], / 286 ) -> floating[_NBit1 | _NBit2]: ... 287 288 class _FloatDivMod(Protocol[_NBit1]): 289 @overload 290 def __call__(self, other: bool, /) -> _2Tuple[floating[_NBit1]]: ... 291 @overload 292 def __call__(self, other: int, /) -> _2Tuple[floating[_NBit1 | _NBitInt]]: ... 293 @overload 294 def __call__(self, other: float, /) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ... 295 @overload 296 def __call__( 297 self, other: integer[_NBit2] | floating[_NBit2], / 298 ) -> _2Tuple[floating[_NBit1 | _NBit2]]: ... 299 300 class _ComplexOp(Protocol[_NBit1]): 301 @overload 302 def __call__(self, other: bool, /) -> complexfloating[_NBit1, _NBit1]: ... 303 @overload 304 def __call__(self, other: int, /) -> complexfloating[_NBit1 | _NBitInt, _NBit1 | _NBitInt]: ... 305 @overload 306 def __call__( 307 self, other: complex, /, 308 ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ... 309 @overload 310 def __call__( 311 self, 312 other: ( 313 integer[_NBit2] 314 | floating[_NBit2] 315 | complexfloating[_NBit2, _NBit2] 316 ), /, 317 ) -> complexfloating[_NBit1 | _NBit2, _NBit1 | _NBit2]: ... 318 319 class _NumberOp(Protocol): 320 def __call__(self, other: _NumberLike_co, /) -> Any: ... 321 322 class _SupportsLT(Protocol): 323 def __lt__(self, other: Any, /) -> object: ... 324 325 class _SupportsGT(Protocol): 326 def __gt__(self, other: Any, /) -> object: ... 327 328 class _ComparisonOp(Protocol[_T1_contra, _T2_contra]): 329 @overload 330 def __call__(self, other: _T1_contra, /) -> bool_: ... 331 @overload 332 def __call__(self, other: _T2_contra, /) -> NDArray[bool_]: ... 333 @overload 334 def __call__( 335 self, 336 other: _SupportsLT | _SupportsGT | _NestedSequence[_SupportsLT | _SupportsGT], 337 /, 338 ) -> Any: ...