summaryrefslogtreecommitdiff
path: root/numpy/typing/_callable.py
blob: 54f9b142576d7c0fd6cdabac2dc02ad9115d8352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
"""
A module with various ``typing.Protocol`` subclasses that implement
the ``__call__`` magic method.

See the `Mypy documentation`_ on protocols for more details.

.. _`Mypy documentation`: https://mypy.readthedocs.io/en/stable/protocols.html#callback-protocols

"""

from __future__ import annotations

import sys
from typing import (
    Union,
    TypeVar,
    overload,
    Any,
    Tuple,
    NoReturn,
    TYPE_CHECKING,
)

from numpy import (
    ndarray,
    dtype,
    generic,
    bool_,
    timedelta64,
    number,
    integer,
    unsignedinteger,
    signedinteger,
    int8,
    int_,
    floating,
    float64,
    complexfloating,
    complex128,
)
from ._nbit import _NBitInt, _NBitDouble
from ._scalars import (
    _BoolLike_co,
    _IntLike_co,
    _FloatLike_co,
    _NumberLike_co,
)
from . import NBitBase, _HAS_TYPING_EXTENSIONS
from ._generic_alias import NDArray

if sys.version_info >= (3, 8):
    from typing import Protocol
elif _HAS_TYPING_EXTENSIONS:
    from typing_extensions import Protocol

if TYPE_CHECKING or _HAS_TYPING_EXTENSIONS:
    _T1 = TypeVar("_T1")
    _T2 = TypeVar("_T2")
    _2Tuple = Tuple[_T1, _T1]

    _NBit1 = TypeVar("_NBit1", bound=NBitBase)
    _NBit2 = TypeVar("_NBit2", bound=NBitBase)

    _IntType = TypeVar("_IntType", bound=integer)
    _FloatType = TypeVar("_FloatType", bound=floating)
    _NumberType = TypeVar("_NumberType", bound=number)
    _NumberType_co = TypeVar("_NumberType_co", covariant=True, bound=number)
    _GenericType_co = TypeVar("_GenericType_co", covariant=True, bound=generic)

    class _BoolOp(Protocol[_GenericType_co]):
        @overload
        def __call__(self, __other: _BoolLike_co) -> _GenericType_co: ...
        @overload  # platform dependent
        def __call__(self, __other: int) -> int_: ...
        @overload
        def __call__(self, __other: float) -> float64: ...
        @overload
        def __call__(self, __other: complex) -> complex128: ...
        @overload
        def __call__(self, __other: _NumberType) -> _NumberType: ...

    class _BoolBitOp(Protocol[_GenericType_co]):
        @overload
        def __call__(self, __other: _BoolLike_co) -> _GenericType_co: ...
        @overload  # platform dependent
        def __call__(self, __other: int) -> int_: ...
        @overload
        def __call__(self, __other: _IntType) -> _IntType: ...

    class _BoolSub(Protocol):
        # Note that `__other: bool_` is absent here
        @overload
        def __call__(self, __other: bool) -> NoReturn: ...
        @overload  # platform dependent
        def __call__(self, __other: int) -> int_: ...
        @overload
        def __call__(self, __other: float) -> float64: ...
        @overload
        def __call__(self, __other: complex) -> complex128: ...
        @overload
        def __call__(self, __other: _NumberType) -> _NumberType: ...

    class _BoolTrueDiv(Protocol):
        @overload
        def __call__(self, __other: float | _IntLike_co) -> float64: ...
        @overload
        def __call__(self, __other: complex) -> complex128: ...
        @overload
        def __call__(self, __other: _NumberType) -> _NumberType: ...

    class _BoolMod(Protocol):
        @overload
        def __call__(self, __other: _BoolLike_co) -> int8: ...
        @overload  # platform dependent
        def __call__(self, __other: int) -> int_: ...
        @overload
        def __call__(self, __other: float) -> float64: ...
        @overload
        def __call__(self, __other: _IntType) -> _IntType: ...
        @overload
        def __call__(self, __other: _FloatType) -> _FloatType: ...

    class _BoolDivMod(Protocol):
        @overload
        def __call__(self, __other: _BoolLike_co) -> _2Tuple[int8]: ...
        @overload  # platform dependent
        def __call__(self, __other: int) -> _2Tuple[int_]: ...
        @overload
        def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ...
        @overload
        def __call__(self, __other: _IntType) -> _2Tuple[_IntType]: ...
        @overload
        def __call__(self, __other: _FloatType) -> _2Tuple[_FloatType]: ...

    class _TD64Div(Protocol[_NumberType_co]):
        @overload
        def __call__(self, __other: timedelta64) -> _NumberType_co: ...
        @overload
        def __call__(self, __other: _BoolLike_co) -> NoReturn: ...
        @overload
        def __call__(self, __other: _FloatLike_co) -> timedelta64: ...

    class _IntTrueDiv(Protocol[_NBit1]):
        @overload
        def __call__(self, __other: bool) -> floating[_NBit1]: ...
        @overload
        def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ...
        @overload
        def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
        @overload
        def __call__(
            self, __other: complex
        ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
        @overload
        def __call__(self, __other: integer[_NBit2]) -> floating[_NBit1 | _NBit2]: ...

    class _UnsignedIntOp(Protocol[_NBit1]):
        # NOTE: `uint64 + signedinteger -> float64`
        @overload
        def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ...
        @overload
        def __call__(
            self, __other: int | signedinteger[Any]
        ) -> Any: ...
        @overload
        def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
        @overload
        def __call__(
            self, __other: complex
        ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
        @overload
        def __call__(
            self, __other: unsignedinteger[_NBit2]
        ) -> unsignedinteger[_NBit1 | _NBit2]: ...

    class _UnsignedIntBitOp(Protocol[_NBit1]):
        @overload
        def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ...
        @overload
        def __call__(self, __other: int) -> signedinteger[Any]: ...
        @overload
        def __call__(self, __other: signedinteger[Any]) -> signedinteger[Any]: ...
        @overload
        def __call__(
            self, __other: unsignedinteger[_NBit2]
        ) -> unsignedinteger[_NBit1 | _NBit2]: ...

    class _UnsignedIntMod(Protocol[_NBit1]):
        @overload
        def __call__(self, __other: bool) -> unsignedinteger[_NBit1]: ...
        @overload
        def __call__(
            self, __other: int | signedinteger[Any]
        ) -> Any: ...
        @overload
        def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
        @overload
        def __call__(
            self, __other: unsignedinteger[_NBit2]
        ) -> unsignedinteger[_NBit1 | _NBit2]: ...

    class _UnsignedIntDivMod(Protocol[_NBit1]):
        @overload
        def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit1]]: ...
        @overload
        def __call__(
            self, __other: int | signedinteger[Any]
        ) -> _2Tuple[Any]: ...
        @overload
        def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ...
        @overload
        def __call__(
            self, __other: unsignedinteger[_NBit2]
        ) -> _2Tuple[unsignedinteger[_NBit1 | _NBit2]]: ...

    class _SignedIntOp(Protocol[_NBit1]):
        @overload
        def __call__(self, __other: bool) -> signedinteger[_NBit1]: ...
        @overload
        def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ...
        @overload
        def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
        @overload
        def __call__(
            self, __other: complex
        ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
        @overload
        def __call__(
            self, __other: signedinteger[_NBit2]
        ) -> signedinteger[_NBit1 | _NBit2]: ...

    class _SignedIntBitOp(Protocol[_NBit1]):
        @overload
        def __call__(self, __other: bool) -> signedinteger[_NBit1]: ...
        @overload
        def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ...
        @overload
        def __call__(
            self, __other: signedinteger[_NBit2]
        ) -> signedinteger[_NBit1 | _NBit2]: ...

    class _SignedIntMod(Protocol[_NBit1]):
        @overload
        def __call__(self, __other: bool) -> signedinteger[_NBit1]: ...
        @overload
        def __call__(self, __other: int) -> signedinteger[_NBit1 | _NBitInt]: ...
        @overload
        def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
        @overload
        def __call__(
            self, __other: signedinteger[_NBit2]
        ) -> signedinteger[_NBit1 | _NBit2]: ...

    class _SignedIntDivMod(Protocol[_NBit1]):
        @overload
        def __call__(self, __other: bool) -> _2Tuple[signedinteger[_NBit1]]: ...
        @overload
        def __call__(self, __other: int) -> _2Tuple[signedinteger[_NBit1 | _NBitInt]]: ...
        @overload
        def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ...
        @overload
        def __call__(
            self, __other: signedinteger[_NBit2]
        ) -> _2Tuple[signedinteger[_NBit1 | _NBit2]]: ...

    class _FloatOp(Protocol[_NBit1]):
        @overload
        def __call__(self, __other: bool) -> floating[_NBit1]: ...
        @overload
        def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ...
        @overload
        def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
        @overload
        def __call__(
            self, __other: complex
        ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
        @overload
        def __call__(
            self, __other: integer[_NBit2] | floating[_NBit2]
        ) -> floating[_NBit1 | _NBit2]: ...

    class _FloatMod(Protocol[_NBit1]):
        @overload
        def __call__(self, __other: bool) -> floating[_NBit1]: ...
        @overload
        def __call__(self, __other: int) -> floating[_NBit1 | _NBitInt]: ...
        @overload
        def __call__(self, __other: float) -> floating[_NBit1 | _NBitDouble]: ...
        @overload
        def __call__(
            self, __other: integer[_NBit2] | floating[_NBit2]
        ) -> floating[_NBit1 | _NBit2]: ...

    class _FloatDivMod(Protocol[_NBit1]):
        @overload
        def __call__(self, __other: bool) -> _2Tuple[floating[_NBit1]]: ...
        @overload
        def __call__(self, __other: int) -> _2Tuple[floating[_NBit1 | _NBitInt]]: ...
        @overload
        def __call__(self, __other: float) -> _2Tuple[floating[_NBit1 | _NBitDouble]]: ...
        @overload
        def __call__(
            self, __other: integer[_NBit2] | floating[_NBit2]
        ) -> _2Tuple[floating[_NBit1 | _NBit2]]: ...

    class _ComplexOp(Protocol[_NBit1]):
        @overload
        def __call__(self, __other: bool) -> complexfloating[_NBit1, _NBit1]: ...
        @overload
        def __call__(self, __other: int) -> complexfloating[_NBit1 | _NBitInt, _NBit1 | _NBitInt]: ...
        @overload
        def __call__(
            self, __other: complex
        ) -> complexfloating[_NBit1 | _NBitDouble, _NBit1 | _NBitDouble]: ...
        @overload
        def __call__(
            self,
            __other: Union[
                integer[_NBit2],
                floating[_NBit2],
                complexfloating[_NBit2, _NBit2],
            ]
        ) -> complexfloating[_NBit1 | _NBit2, _NBit1 | _NBit2]: ...

    class _NumberOp(Protocol):
        def __call__(self, __other: _NumberLike_co) -> Any: ...

    class _ComparisonOp(Protocol[_T1, _T2]):
        @overload
        def __call__(self, __other: _T1) -> bool_: ...
        @overload
        def __call__(self, __other: _T2) -> NDArray[bool_]: ...

else:
    _BoolOp = NotImplemented
    _BoolBitOp = NotImplemented
    _BoolSub = NotImplemented
    _BoolTrueDiv = NotImplemented
    _BoolMod = NotImplemented
    _BoolDivMod = NotImplemented
    _TD64Div = NotImplemented
    _IntTrueDiv = NotImplemented
    _UnsignedIntOp = NotImplemented
    _UnsignedIntBitOp = NotImplemented
    _UnsignedIntMod = NotImplemented
    _UnsignedIntDivMod = NotImplemented
    _SignedIntOp = NotImplemented
    _SignedIntBitOp = NotImplemented
    _SignedIntMod = NotImplemented
    _SignedIntDivMod = NotImplemented
    _FloatOp = NotImplemented
    _FloatMod = NotImplemented
    _FloatDivMod = NotImplemented
    _ComplexOp = NotImplemented
    _NumberOp = NotImplemented
    _ComparisonOp = NotImplemented