36
36
from typing import SupportsBytes
37
37
38
38
if sys .version_info >= (3 , 8 ):
39
- from typing import Literal
39
+ from typing import Literal , Protocol
40
40
else :
41
- from typing_extensions import Literal
41
+ from typing_extensions import Literal , Protocol
42
42
43
43
# TODO: remove when the full numpy namespace is defined
44
44
def __getattr__ (name : str ) -> Any : ...
@@ -52,7 +52,7 @@ _DtypeLikeNested = Any # TODO: wait for support for recursive types
52
52
53
53
# Anything that can be coerced into numpy.dtype.
54
54
# Reference: https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
55
- _DtypeLike = Union [
55
+ DtypeLike = Union [
56
56
dtype ,
57
57
# default data type (float64)
58
58
None ,
@@ -92,13 +92,17 @@ _DtypeLike = Union[
92
92
93
93
_NdArraySubClass = TypeVar ("_NdArraySubClass" , bound = ndarray )
94
94
95
- _ArrayLike = TypeVar ("_ArrayLike" )
95
+ class _SupportsArray (Protocol ):
96
+ @overload
97
+ def __array__ (self , __dtype : DtypeLike = ...) -> ndarray : ...
98
+ @overload
99
+ def __array__ (self , dtype : Optional [DtypeLike ] = ...) -> ndarray : ...
100
+
101
+ ArrayLike = Union [bool , int , float , complex , _SupportsArray , Sequence ]
96
102
97
103
class dtype :
98
104
names : Optional [Tuple [str , ...]]
99
- def __init__ (
100
- self , obj : _DtypeLike , align : bool = ..., copy : bool = ...
101
- ) -> None : ...
105
+ def __init__ (self , obj : DtypeLike , align : bool = ..., copy : bool = ...) -> None : ...
102
106
@property
103
107
def alignment (self ) -> int : ...
104
108
@property
@@ -217,6 +221,7 @@ class _ArrayOrScalarCommon(
217
221
def shape (self ) -> _Shape : ...
218
222
@property
219
223
def strides (self ) -> _Shape : ...
224
+ def __array__ (self , __dtype : Optional [DtypeLike ] = ...) -> ndarray : ...
220
225
def __int__ (self ) -> int : ...
221
226
def __float__ (self ) -> float : ...
222
227
def __complex__ (self ) -> complex : ...
@@ -299,7 +304,7 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
299
304
def __new__ (
300
305
cls ,
301
306
shape : Sequence [int ],
302
- dtype : Union [_DtypeLike , str ] = ...,
307
+ dtype : Union [DtypeLike , str ] = ...,
303
308
buffer : _BufferType = ...,
304
309
offset : int = ...,
305
310
strides : _ShapeLike = ...,
@@ -338,7 +343,7 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
338
343
def dumps (self ) -> bytes : ...
339
344
def astype (
340
345
self ,
341
- dtype : _DtypeLike ,
346
+ dtype : DtypeLike ,
342
347
order : str = ...,
343
348
casting : str = ...,
344
349
subok : bool = ...,
@@ -349,14 +354,14 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
349
354
@overload
350
355
def view (self , dtype : Type [_NdArraySubClass ]) -> _NdArraySubClass : ...
351
356
@overload
352
- def view (self , dtype : _DtypeLike = ...) -> ndarray : ...
357
+ def view (self , dtype : DtypeLike = ...) -> ndarray : ...
353
358
@overload
354
359
def view (
355
- self , dtype : _DtypeLike , type : Type [_NdArraySubClass ]
360
+ self , dtype : DtypeLike , type : Type [_NdArraySubClass ]
356
361
) -> _NdArraySubClass : ...
357
362
@overload
358
363
def view (self , * , type : Type [_NdArraySubClass ]) -> _NdArraySubClass : ...
359
- def getfield (self , dtype : Union [_DtypeLike , str ], offset : int = ...) -> ndarray : ...
364
+ def getfield (self , dtype : Union [DtypeLike , str ], offset : int = ...) -> ndarray : ...
360
365
def setflags (
361
366
self , write : bool = ..., align : bool = ..., uic : bool = ...
362
367
) -> None : ...
@@ -484,26 +489,26 @@ class str_(character): ...
484
489
485
490
def array (
486
491
object : object ,
487
- dtype : _DtypeLike = ...,
492
+ dtype : DtypeLike = ...,
488
493
copy : bool = ...,
489
494
subok : bool = ...,
490
495
ndmin : int = ...,
491
496
) -> ndarray : ...
492
497
def zeros (
493
- shape : _ShapeLike , dtype : _DtypeLike = ..., order : Optional [str ] = ...
498
+ shape : _ShapeLike , dtype : DtypeLike = ..., order : Optional [str ] = ...
494
499
) -> ndarray : ...
495
500
def ones (
496
- shape : _ShapeLike , dtype : _DtypeLike = ..., order : Optional [str ] = ...
501
+ shape : _ShapeLike , dtype : DtypeLike = ..., order : Optional [str ] = ...
497
502
) -> ndarray : ...
498
503
def zeros_like (
499
- a : _ArrayLike ,
504
+ a : ArrayLike ,
500
505
dtype : Optional [dtype ] = ...,
501
506
order : str = ...,
502
507
subok : bool = ...,
503
508
shape : Optional [Union [int , Sequence [int ]]] = ...,
504
509
) -> ndarray : ...
505
510
def ones_like (
506
- a : _ArrayLike ,
511
+ a : ArrayLike ,
507
512
dtype : Optional [dtype ] = ...,
508
513
order : str = ...,
509
514
subok : bool = ...,
@@ -513,43 +518,43 @@ def full(
513
518
shape : _ShapeLike , fill_value : Any , dtype : Optional [dtype ] = ..., order : str = ...
514
519
) -> ndarray : ...
515
520
def full_like (
516
- a : _ArrayLike ,
521
+ a : ArrayLike ,
517
522
fill_value : Any ,
518
523
dtype : Optional [dtype ] = ...,
519
524
order : str = ...,
520
525
subok : bool = ...,
521
526
shape : Optional [_ShapeLike ] = ...,
522
527
) -> ndarray : ...
523
528
def count_nonzero (
524
- a : _ArrayLike , axis : Optional [Union [int , Tuple [int ], Tuple [int , int ]]] = ...
529
+ a : ArrayLike , axis : Optional [Union [int , Tuple [int ], Tuple [int , int ]]] = ...
525
530
) -> Union [int , ndarray ]: ...
526
531
def isfortran (a : ndarray ) -> bool : ...
527
- def argwhere (a : _ArrayLike ) -> ndarray : ...
528
- def flatnonzero (a : _ArrayLike ) -> ndarray : ...
529
- def correlate (a : _ArrayLike , v : _ArrayLike , mode : str = ...) -> ndarray : ...
530
- def convolve (a : _ArrayLike , v : _ArrayLike , mode : str = ...) -> ndarray : ...
531
- def outer (a : _ArrayLike , b : _ArrayLike , out : ndarray = ...) -> ndarray : ...
532
+ def argwhere (a : ArrayLike ) -> ndarray : ...
533
+ def flatnonzero (a : ArrayLike ) -> ndarray : ...
534
+ def correlate (a : ArrayLike , v : ArrayLike , mode : str = ...) -> ndarray : ...
535
+ def convolve (a : ArrayLike , v : ArrayLike , mode : str = ...) -> ndarray : ...
536
+ def outer (a : ArrayLike , b : ArrayLike , out : ndarray = ...) -> ndarray : ...
532
537
def tensordot (
533
- a : _ArrayLike ,
534
- b : _ArrayLike ,
538
+ a : ArrayLike ,
539
+ b : ArrayLike ,
535
540
axes : Union [
536
541
int , Tuple [int , int ], Tuple [Tuple [int , int ], ...], Tuple [List [int , int ], ...]
537
542
] = ...,
538
543
) -> ndarray : ...
539
544
def roll (
540
- a : _ArrayLike ,
545
+ a : ArrayLike ,
541
546
shift : Union [int , Tuple [int , ...]],
542
547
axis : Optional [Union [int , Tuple [int , ...]]] = ...,
543
548
) -> ndarray : ...
544
- def rollaxis (a : _ArrayLike , axis : int, start : int = ...) -> ndarray : ...
549
+ def rollaxis (a : ArrayLike , axis : int , start : int = ...) -> ndarray : ...
545
550
def moveaxis (
546
551
a : ndarray ,
547
552
source : Union [int , Sequence [int ]],
548
553
destination : Union [int , Sequence [int ]],
549
554
) -> ndarray : ...
550
555
def cross (
551
- a : _ArrayLike ,
552
- b : _ArrayLike ,
556
+ a : ArrayLike ,
557
+ b : ArrayLike ,
553
558
axisa : int = ...,
554
559
axisb : int = ...,
555
560
axisc : int = ...,
@@ -564,21 +569,21 @@ def binary_repr(num: int, width: Optional[int] = ...) -> str: ...
564
569
def base_repr (number : int , base : int = ..., padding : int = ...) -> str : ...
565
570
def identity (n : int , dtype : Optional [dtype ] = ...) -> ndarray : ...
566
571
def allclose (
567
- a : _ArrayLike ,
568
- b : _ArrayLike ,
572
+ a : ArrayLike ,
573
+ b : ArrayLike ,
569
574
rtol : float = ...,
570
575
atol : float = ...,
571
576
equal_nan : bool = ...,
572
577
) -> bool : ...
573
578
def isclose (
574
- a : _ArrayLike ,
575
- b : _ArrayLike ,
579
+ a : ArrayLike ,
580
+ b : ArrayLike ,
576
581
rtol : float = ...,
577
582
atol : float = ...,
578
583
equal_nan : bool = ...,
579
584
) -> Union [bool_ , ndarray ]: ...
580
- def array_equal (a1 : _ArrayLike , a2 : _ArrayLike ) -> bool : ...
581
- def array_equiv (a1 : _ArrayLike , a2 : _ArrayLike ) -> bool : ...
585
+ def array_equal (a1 : ArrayLike , a2 : ArrayLike ) -> bool : ...
586
+ def array_equiv (a1 : ArrayLike , a2 : ArrayLike ) -> bool : ...
582
587
583
588
#
584
589
# Constants
@@ -632,7 +637,7 @@ class ufunc:
632
637
def __name__ (self ) -> str : ...
633
638
def __call__ (
634
639
self ,
635
- * args : _ArrayLike ,
640
+ * args : ArrayLike ,
636
641
out : Optional [Union [ndarray , Tuple [ndarray , ...]]] = ...,
637
642
where : Optional [ndarray ] = ...,
638
643
# The list should be a list of tuples of ints, but since we
@@ -647,7 +652,7 @@ class ufunc:
647
652
casting : str = ...,
648
653
# TODO: make this precise when we can use Literal.
649
654
order : Optional [str ] = ...,
650
- dtype : Optional [_DtypeLike ] = ...,
655
+ dtype : Optional [DtypeLike ] = ...,
651
656
subok : bool = ...,
652
657
signature : Union [str , Tuple [str ]] = ...,
653
658
# In reality this should be a length of list 3 containing an
@@ -845,74 +850,74 @@ def take(
845
850
) -> _ScalarNumpy : ...
846
851
@overload
847
852
def take (
848
- a : _ArrayLike ,
853
+ a : ArrayLike ,
849
854
indices : int ,
850
855
axis : Optional [int ] = ...,
851
856
out : Optional [ndarray ] = ...,
852
857
mode : _Mode = ...,
853
858
) -> _ScalarNumpy : ...
854
859
@overload
855
860
def take (
856
- a : _ArrayLike ,
861
+ a : ArrayLike ,
857
862
indices : _ArrayLikeInt ,
858
863
axis : Optional [int ] = ...,
859
864
out : Optional [ndarray ] = ...,
860
865
mode : _Mode = ...,
861
866
) -> Union [_ScalarNumpy , ndarray ]: ...
862
- def reshape (a : _ArrayLike , newshape : _ShapeLike , order : _Order = ...) -> ndarray : ...
867
+ def reshape (a : ArrayLike , newshape : _ShapeLike , order : _Order = ...) -> ndarray : ...
863
868
@overload
864
869
def choose (
865
870
a : _ScalarGeneric ,
866
- choices : Union [Sequence [_ArrayLike ], ndarray ],
871
+ choices : Union [Sequence [ArrayLike ], ndarray ],
867
872
out : Optional [ndarray ] = ...,
868
873
mode : _Mode = ...,
869
874
) -> _ScalarGeneric : ...
870
875
@overload
871
876
def choose (
872
877
a : _Scalar ,
873
- choices : Union [Sequence [_ArrayLike ], ndarray ],
878
+ choices : Union [Sequence [ArrayLike ], ndarray ],
874
879
out : Optional [ndarray ] = ...,
875
880
mode : _Mode = ...,
876
881
) -> _ScalarNumpy : ...
877
882
@overload
878
883
def choose (
879
- a : _ArrayLike ,
880
- choices : Union [Sequence [_ArrayLike ], ndarray ],
884
+ a : ArrayLike ,
885
+ choices : Union [Sequence [ArrayLike ], ndarray ],
881
886
out : Optional [ndarray ] = ...,
882
887
mode : _Mode = ...,
883
888
) -> ndarray : ...
884
889
def repeat (
885
- a : _ArrayLike , repeats : _ArrayLikeInt , axis : Optional [int ] = ...
890
+ a : ArrayLike , repeats : _ArrayLikeInt , axis : Optional [int ] = ...
886
891
) -> ndarray : ...
887
- def put (a : ndarray , ind : _ArrayLikeInt , v : _ArrayLike , mode : _Mode = ...) -> None : ...
892
+ def put (a : ndarray , ind : _ArrayLikeInt , v : ArrayLike , mode : _Mode = ...) -> None : ...
888
893
def swapaxes (
889
- a : Union [Sequence [_ArrayLike ], ndarray ], axis1 : int , axis2 : int
894
+ a : Union [Sequence [ArrayLike ],
10000
ndarray ], axis1 : int , axis2 : int
890
895
) -> ndarray : ...
891
896
def transpose (
892
- a : _ArrayLike , axes : Union [None , Sequence [int ], ndarray ] = ...
897
+ a : ArrayLike , axes : Union [None , Sequence [int ], ndarray ] = ...
893
898
) -> ndarray : ...
894
899
def partition (
895
- a : _ArrayLike ,
900
+ a : ArrayLike ,
896
901
kth : _ArrayLikeInt ,
897
902
axis : Optional [int ] = ...,
898
903
kind : _PartitionKind = ...,
899
904
order : Union [None , str , Sequence [str ]] = ...,
900
905
) -> ndarray : ...
901
906
def argpartition (
902
- a : _ArrayLike ,
907
+ a : ArrayLike ,
903
908
kth : _ArrayLikeInt ,
904
909
axis : Optional [int ] = ...,
905
910
kind : _PartitionKind = ...,
906
911
order : Union [None , str , Sequence [str ]] = ...,
907
912
) -> ndarray : ...
908
913
def sort (
909
- a : Union [Sequence [_ArrayLike ], ndarray ],
914
+ a : Union [Sequence [ArrayLike ], ndarray ],
910
915
axis : Optional [int ] = ...,
911
916
kind : Optional [_SortKind ] = ...,
912
917
order : Union [None , str , Sequence [str ]] = ...,
913
918
) -> ndarray : ...
914
919
def argsort (
915
- a : Union [Sequence [_ArrayLike ], ndarray ],
920
+ a : Union [Sequence [ArrayLike ], ndarray ],
916
921
axis : Optional [int ] = ...,
917
922
kind : Optional [_SortKind ] = ...,
918
923
order : Union [None , str , Sequence [str ]] = ...,
0 commit comments