@@ -3,6 +3,8 @@ import sys
3
3
import datetime as dt
4
4
5
5
from numpy .core ._internal import _ctypes
6
+ from numpy .typing import ArrayLike , DtypeLike , _Shape , _ShapeLike
7
+
6
8
from typing import (
7
9
Any ,
8
10
ByteString ,
@@ -36,69 +38,18 @@ else:
36
38
from typing import SupportsBytes
37
39
38
40
if sys .version_info >= (3 , 8 ):
39
- from typing import Literal
41
+ from typing import Literal , Protocol
40
42
else :
41
- from typing_extensions import Literal
43
+ from typing_extensions import Literal , Protocol
42
44
43
45
# TODO: remove when the full numpy namespace is defined
44
46
def __getattr__ (name : str ) -> Any : ...
45
47
46
- _Shape = Tuple [int , ...]
47
-
48
- # Anything that can be coerced to a shape tuple
49
- _ShapeLike = Union [int , Sequence [int ]]
50
-
51
- _DtypeLikeNested = Any # TODO: wait for support for recursive types
52
-
53
- # Anything that can be coerced into numpy.dtype.
54
- # Reference: https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
55
- _DtypeLike = Union [
56
- dtype ,
57
- # default data type (float64)
58
- None ,
59
- # array-scalar types and generic types
60
- type , # TODO: enumerate these when we add type hints for numpy scalars
61
- # TODO: add a protocol for anything with a dtype attribute
62
- # character codes, type strings or comma-separated fields, e.g., 'float64'
63
- str ,
64
- # (flexible_dtype, itemsize)
65
- Tuple [_DtypeLikeNested , int ],
66
- # (fixed_dtype, shape)
67
- Tuple [_DtypeLikeNested , _ShapeLike ],
68
- # [(field_name, field_dtype, field_shape), ...]
69
- #
70
- # The type here is quite broad because NumPy accepts quite a wide
71
- # range of inputs inside the list; see the tests for some
72
- # examples.
73
- List [Any ],
74
- # {'names': ..., 'formats': ..., 'offsets': ..., 'titles': ...,
75
- # 'itemsize': ...}
76
- # TODO: use TypedDict when/if it's officially supported
77
- Dict [
78
- str ,
79
- Union [
80
- Sequence [str ], # names
81
- Sequence [_DtypeLikeNested ], # formats
82
- Sequence [int ], # offsets
83
- Sequence [Union [bytes , Text , None ]], # titles
84
- int , # itemsize
85
- ],
86
- ],
87
- # {'field1': ..., 'field2': ..., ...}
88
- Dict [str , Tuple [_DtypeLikeNested , int ]],
89
- # (base_dtype, new_dtype)
90
- Tuple [_DtypeLikeNested , _DtypeLikeNested ],
91
- ]
92
-
93
48
_NdArraySubClass = TypeVar ("_NdArraySubClass" , bound = ndarray )
94
49
95
- _ArrayLike = TypeVar ("_ArrayLike" )
96
-
97
50
class dtype :
98
51
names : Optional [Tuple [str , ...]]
99
- def __init__ (
100
- self , obj : _DtypeLike , align : bool = ..., copy : bool = ...
101
- ) -> None : ...
52
+ def __init__ (self , obj : DtypeLike , align : bool = ..., copy : bool = ...) -> None : ...
102
53
@property
103
54
def alignment (self ) -> int : ...
104
55
@property
@@ -217,6 +168,7 @@ class _ArrayOrScalarCommon(
217
168
def shape (self ) -> _Shape : ...
218
169
@property
219
170
def strides (self ) -> _Shape : ...
171
+ def __array__ (self , __dtype : DtypeLike = ...) -> ndarray : ...
220
172
def __int__ (self ) -> int : ...
221
173
def __float__ (self ) -> float : ...
222
174
def __complex__ (self ) -> complex : ...
@@ -299,7 +251,7 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
299
251
def __new__ (
300
252
cls ,
301
253
shape : Sequence [int ],
302
- dtype : Union [ _DtypeLike , str ] = ...,
254
+ dtype : DtypeLike = ...,
303
255
buffer : _BufferType = ...,
304
256
offset : int = ...,
305
257
strides : _ShapeLike = ...,
@@ -338,7 +290,7 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
338
290
def dumps (self ) -> bytes : ...
339
291
def astype (
340
292
self ,
341
- dtype : _DtypeLike ,
293
+ dtype : DtypeLike ,
342
294
order : str = ...,
343
295
casting : str = ...,
344
296
subok : bool = ...,
@@ -349,14 +301,14 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
349
301
@overload
350
302
def view (self , dtype : Type [_NdArraySubClass ]) -> _NdArraySubClass : ...
351
303
@overload
352
- def view (self , dtype : _DtypeLike = ...) -> ndarray : ...
304
+ def view (self , dtype : DtypeLike = ...) -> ndarray : ...
353
305
@overload
354
306
def view (
355
- self , dtype : _DtypeLike , type : Type [_NdArraySubClass ]
307
+ self , dtype : DtypeLike , type : Type [_NdArraySubClass ]
356
308
) -> _NdArraySubClass : ...
357
309
@overload
358
310
def view (self , * , type : Type [_NdArraySubClass ]) -> _NdArraySubClass : ...
359
- def getfield (self , dtype : Union [ _DtypeLike , str ] , offset : int = ...) -> ndarray : ...
311
+ def getfield (self , dtype : DtypeLike , offset : int = ...) -> ndarray : ...
360
312
def setflags (
361
313
self , write : bool = ..., align : bool = ..., uic : bool = ...
362
314
) -> None : ...
@@ -501,26 +453,26 @@ class str_(character): ...
501
453
502
454
def array (
503
455
object : object ,
504
- dtype : _DtypeLike = ...,
456
+ dtype : DtypeLike = ...,
505
457
copy : bool = ...,
506
458
subok : bool = ...,
507
459
ndmin : int = ...,
508
460
) -> ndarray : ...
509
461
def zeros (
510
- shape : _ShapeLike , dtype : _DtypeLike = ..., order : Optional [str ] = ...
462
+ shape : _ShapeLike , dtype : DtypeLike = ..., order : Optional [str ] = ...
511
463
) -> ndarray : ...
512
464
def ones (
513
- shape : _ShapeLike , dtype : _DtypeLike = ..., order : Optional [str ] = ...
465
+ shape : _ShapeLike , dtype : DtypeLike = ..., order : Optional [str ] = ...
514
466
) -> ndarray : ...
515
467
def zeros_like (
516
- a : _ArrayLike ,
468
+ a : ArrayLike ,
517
469
dtype : Optional [dtype ] = ...,
518
470
order : str = ...,
519
471
subok : bool = ...,
520
472
shape : Optional [Union [int , Sequence [int ]]] = ...,
521
473
) -> ndarray : ...
522
474
def ones_like (
523
- a : _ArrayLike ,
475
+ a : ArrayLike ,
524
476
dtype : Optional [dtype ] = ...,
525
477
order : str = ...,
526
478
subok : bool = ...,
@@ -530,43 +482,43 @@ def full(
530
482
shape : _ShapeLike , fill_value : Any , dtype : Optional [dtype ] = ..., order : str = ...
531
483
) -> ndarray : ...
532
484
def full_like (
533
- a : _ArrayLike ,
485
+ a : ArrayLike ,
534
486
fill_value : Any ,
535
487
dtype : Optional [dtype ] = ...,
536
488
order : str = ...,
537
489
subok : bool = ...,
538
490
shape : Optional [_ShapeLike ] = ...,
539
491
) -> ndarray : ...
540
492
def count_nonzero (
541
- a : _ArrayLike , axis : Optional [Union [int , Tuple [int ], Tuple [int , int ]]] = ...
493
+ a : ArrayLike , axis : Optional [Union [int , Tuple [int ], Tuple [int , int ]]] = ...
542
494
) -> Union [int , ndarray ]: ...
543
495
def isfortran (a : ndarray ) -> bool : ...
544
- def argwhere (a : _ArrayLike ) -> ndarray : ...
545
- def flatnonzero (a : _ArrayLike ) -> ndarray : ...
546
- def correlate (a : _ArrayLike , v : _ArrayLike , mode : str = ...) -> ndarray : ...
547
- def convolve (a : _ArrayLike , v : _ArrayLike , mode : str = ...) -> ndarray : ...
548
- def outer (a : _ArrayLike , b : _ArrayLike , out : ndarray = ...) -> ndarray : ...
496
+ def argwhere (a : ArrayLike ) -> ndarray : ...
497
+ def flatnonzero (a : ArrayLike ) -> ndarray : ...
498
+ def correlate (a : ArrayLike , v : ArrayLike , mode : str = ...) -> ndarray : ...
499
+ def convolve (a : ArrayLike , v : ArrayLike , mode : str = ...) -> ndarray : ...
500
+ def outer (a : ArrayLike , b : ArrayLike , out : ndarray = ...) -> ndarray : ...
549
501
def tensordot (
550
- a : _ArrayLike ,
551
- b : _ArrayLike ,
502
+ a : ArrayLike ,
503
+ b : ArrayLike ,
552
504
axes : Union [
553
505
int , Tuple [int , int ], Tuple [Tuple [int , int ], ...], Tuple [List [int , int ], ...]
554
506
] = ...,
555
507
) -> ndarray : ...
556
508
def roll (
557
- a : _ArrayLike ,
509
+ a : ArrayLike ,
558
510
shift : Union [int , Tuple [int , ...]],
559
511
axis : Optional [Union [int , Tuple [int , ...]]] = ...,
560
512
) -> ndarray : ...
561
- def rollaxis (a : _ArrayLike , axis : int , start : int = ...) -> ndarray : ...
513
+ def rollaxis (a : ArrayLike , axis : int , start : int = ...) -> ndarray : ...
562
514
def moveaxis (
563
515
a : ndarray ,
564
516
source : Union [int , Sequence [int ]],
565
517
destination : Union [int , Sequence [int ]],
566
518
) -> ndarray : ...
567
519
def cross (
568
- a : _ArrayLike ,
569
- b : _ArrayLike ,
520
+ a : ArrayLike ,
521
+ b : ArrayLike ,
570
522
axisa : int = ...,
571
523
axisb : int = ...,
572
524
axisc : int = ...,
@@ -581,21 +533,21 @@ def binary_repr(num: int, width: Optional[int] = ...) -> str: ...
581
533
def base_repr (number : int , base : int = ..., padding : int = ...) -> str : ...
582
534
def identity (n : int , dtype : Optional [dtype ] = ...) -> ndarray : ...
583
535
def allclose (
584
- a : _ArrayLike ,
585
- b : _ArrayLike ,
536
+ a : ArrayLike ,
537
+ b : ArrayLike ,
586
538
rtol : float = ...,
587
539
atol : float = ...,
588
540
equal_nan : bool = ...,
589
541
) -> bool : ...
590
542
def isclose (
591
- a : _ArrayLike ,
592
- b : _ArrayLike ,
543
+ a : ArrayLike ,
544
+ b : ArrayLike ,
593
545
rtol : float = ...,
594
546
atol : float = ...,
595
547
equal_nan : bool = ...,
596
548
) -> Union [bool_ , ndarray ]: ...
597
- def array_equal (a1 : _ArrayLike , a2 : _ArrayLike ) -> bool : ...
598
- def array_equiv (a1 : _ArrayLike , a2 : _ArrayLike ) -> bool : ...
549
+ def array_equal (a1 : ArrayLike , a2 : ArrayLike ) -> bool : ...
550
+ def array_equiv (a1 : ArrayLike , a2 : ArrayLike ) -> bool : ...
599
551
600
552
#
601
553
# Constants
@@ -649,7 +601,7 @@ class ufunc:
649
601
def __name__ (self ) -> str : ...
650
602
def __call__ (
651
603
self ,
652
- * args : _ArrayLike ,
604
+ * args : ArrayLike ,
653
605
out : Optional [Union [ndarray , Tuple [ndarray , ...]]] = ...,
654
606
where : Optional [ndarray ] = ...,
655
607
# The list should be a list of tuples of ints, but since we
@@ -664,7 +616,7 @@ class ufunc:
664
616
casting : str = ...,
665
617
# TODO: make this precise when we can use Literal.
666
618
order : Optional [str ] = ...,
667
- dtype : Optional [ _DtypeLike ] = ...,
619
+ dtype : DtypeLike = ...,
668
620
subok : bool = ...,
669
621
signature : Union [str , Tuple [str ]] = ...,
670
622
# In reality this should be a length of list 3 containing an
@@ -876,56 +828,56 @@ def take(
876
828
) -> _ScalarNumpy : ...
877
829
@overload
878
830
def take (
879
- a : _ArrayLike ,
831
+ a : ArrayLike ,
880
832
indices : int ,
881
833
axis : Optional [int ] = ...,
882
834
out : Optional [ndarray ] = ...,
883
835
mode : _Mode = ...,
884
836
) -> _ScalarNumpy : ...
885
837
@overload
886
838
def take (
887
- a : _ArrayLike ,
839
+ a : ArrayLike ,
888
840
indices : _ArrayLikeIntOrBool ,
889
841
axis : Optional [int ] = ...,
890
842
out : Optional [ndarray ] = ...,
891
843
mode : _Mode = ...,
892
844
) -> Union [_ScalarNumpy , ndarray ]: ...
893
- def reshape (a : _ArrayLike , newshape : _ShapeLike , order : _Order = ...) -> ndarray : ...
845
+ def reshape (a : ArrayLike , newshape : _ShapeLike , order : _Order = ...) -> ndarray : ...
894
846
@overload
895
847
def choose (
896
848
a : _ScalarIntOrBool ,
897
- choices : Union [Sequence [_ArrayLike ], ndarray ],
849
+ choices : Union [Sequence [ArrayLike ], ndarray ],
898
850
out : Optional [ndarray ] = ...,
899
851
mode : _Mode = ...,
900
852
) -> _ScalarIntOrBool : ...
901
853
@overload
902
854
def choose (
903
855
a : _IntOrBool ,
904
- choices : Union [Sequence [_ArrayLike ], ndarray ],
856
+ choices : Union [Sequence [ArrayLike ], ndarray ],
905
857
out : Optional [ndarray ] = ...,
906
858
mode : _Mode = ...,
907
859
) -> Union [integer , bool_ ]: ...
908
860
@overload
909
861
def choose (
910
862
a : _ArrayLikeIntOrBool ,
911
- choices : Union [Sequence [_ArrayLike ], ndarray ],
863
+ choices : Union [Sequence [ArrayLike ], ndarray ],
912
864
out : Optional [ndarray ] = ...,
913
865
mode : _Mode = ...,
914
866
) -> ndarray : ...
915
867
def repeat (
916
- a : _ArrayLike , repeats : _ArrayLikeIntOrBool , axis : Optional [int ] = ...
868
+ a : ArrayLike , repeats : _ArrayLikeIntOrBool , axis : Optional [int ] = ...
917
869
) -> ndarray : ...
918
870
def put (
919
- a : ndarray , ind : _ArrayLikeIntOrBool , v : _ArrayLike , mode : _Mode = ...
871
+ a : ndarray , ind : _ArrayLikeIntOrBool , v : ArrayLike , mode : _Mode = ...
920
872
) -> None : ...
921
873
def swapaxes (
922
- a : Union [Sequence [_ArrayLike ], ndarray ], axis1 : int , axis2 : int
874
+ a : Union [Sequence [ArrayLike ], ndarray ], axis1 : int , axis2 : int
923
875
) -> ndarray : ...
924
876
def transpose (
925
- a : _ArrayLike , axes : Union [None , Sequence [int ], ndarray ] = ...
877
+ a : ArrayLike , axes : Union [None , Sequence [int ], ndarray ] = ...
926
878
) -> ndarray : ...
927
879
def partition (
928
- a : _ArrayLike ,
880
+ a : ArrayLike ,
929
881
kth : _ArrayLikeIntOrBool ,
930
882
axis : Optional [int ] = ...,
931
883
kind : _PartitionKind = ...,
@@ -949,20 +901,20 @@ def argpartition(
949
901
) -> ndarray : ...
950
902
@overload
951
903
def argpartition (
952
- a : _ArrayLike ,
904
+ a : ArrayLike ,
953
905
kth : _ArrayLikeIntOrBool ,
954
906
axis : Optional [int ] = ...,
955
907
kind : _PartitionKind = ...,
956
908
order : Union [None , str , Sequence [str ]] = ...,
957
909
) -> ndarray : ...
958
910
def sort (
959
- a : Union [Sequence [_ArrayLike ], ndarray ],
911
+ a : Union [Sequence [ArrayLike ], ndarray ],
960
912
axis : Optional [int ] = ...,
961
913
kind : Optional [_SortKind ] = ...,
962
914
order : Union [None , str , Sequence [str ]] = ...,
963
915
) -> ndarray : ...
964
916
def argsort (
965
- a : Union [Sequence [_ArrayLike ], ndarray ],
917
+ a : Union [Sequence [ArrayLike ], ndarray ],
966
918
axis : Optional [int ] = ...,
967
919
kind : Optional [_SortKind ] = ...,
968
920
order : Union [None , str , Sequence [str ]] = ...,
0 commit comments