@@ -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
7
+
6
8
from typing import (
7
9
Any ,
8
10
ByteString ,
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 : ...
@@ -48,57 +50,11 @@ _Shape = Tuple[int, ...]
48
50
# Anything that can be coerced to a shape tuple
49
51
_ShapeLike = Union [int , Sequence [int ]]
50
52
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
53
_NdArraySubClass = TypeVar ("_NdArraySubClass" , bound = ndarray )
94
54
95
- _ArrayLike = TypeVar ("_ArrayLike" )
96
-
97
55
class dtype :
98
56
names : Optional [Tuple [str , ...]]
99
- def __init__ (
100
- self , obj : _DtypeLike , align : bool = ..., copy : bool = ...
101
- ) -> None : ...
57
+ def __init__ (self , obj : DtypeLike , align : bool = ..., copy : bool = ...) -> None : ...
102
58
@property
103
59
def alignment (self ) -> int : ...
104
60
@property
@@ -217,6 +173,7 @@ class _ArrayOrScalarCommon(
217
173
def shape (self ) -> _Shape : ...
218
174
@property
219
175
def strides (self ) -> _Shape : ...
176
+ def __array__ (self , __dtype : DtypeLike = ...) -> ndarray : ...
220
177
def __int__ (self ) -> int : ...
221
178
def __float__ (self ) -> float : ...
222
179
def __complex__ (self ) -> complex : ...
@@ -299,7 +256,7 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
299
256
def __new__ (
300
257
cls ,
301
258
shape : Sequence [int ],
302
- dtype : Union [ _DtypeLike , str ] = ...,
259
+ dtype : DtypeLike = ...,
303
260
buffer : _BufferType = ...,
304
261
offset : int = ...,
305
262
strides : _ShapeLike = ...,
@@ -338,7 +295,7 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
338
295
def dumps (self ) -> bytes : ...
339
296
def astype (
340
297
self ,
341
- dtype : _DtypeLike ,
298
+ dtype : DtypeLike ,
342
299
order : str = ...,
343
300
casting : str = ...,
344
301
subok : bool = ...,
@@ -349,14 +306,14 @@ class ndarray(_ArrayOrScalarCommon, Iterable, Sized, Container):
349
306
@overload
350
307
def view (self , dtype : Type [_NdArraySubClass ]) -> _NdArraySubClass : ...
351
308
@overload
352
- def view (self , dtype : _DtypeLike = ...) -> ndarray : ...
309
+ def view (self , dtype : DtypeLike = ...) -> ndarray : ...
353
310
@overload
354
311
def view (
355
- self , dtype : _DtypeLike , type : Type [_NdArraySubClass ]
312
+ self , dtype : DtypeLike , type : Type [_NdArraySubClass ]
356
313
) -> _NdArraySubClass : ...
357
314
@overload
358
315
def view (self , * , type : Type [_NdArraySubClass ]) -> _NdArraySubClass : ...
359
- def getfield (self , dtype : Union [ _DtypeLike , str ] , offset : int = ...) -> ndarray : ...
316
+ def getfield (self , dtype : DtypeLike , offset : int = ...) -> ndarray : ...
360
317
def setflags (
361
318
self , write : bool = ..., align : bool = ..., uic : bool = ...
362
319
) -> None : ...
@@ -501,26 +458,26 @@ class str_(character): ...
501
458
502
459
def array (
503
460
object : object ,
504
- dtype : _DtypeLike = ...,
461
+ dtype : DtypeLike = ...,
505
462
copy : bool = ...,
506
463
subok : bool = ...,
507
464
ndmin : int = ...,
508
465
) -> ndarray : ...
509
466
def zeros (
510
- shape : _ShapeLike , dtype : _DtypeLike = ..., order : Optional [str ] = ...
467
+ shape : _ShapeLike , dtype : DtypeLike = ..., order : Optional [str ] = ...
511
468
) -> ndarray : ...
512
469
def ones (
513
- shape : _ShapeLike , dtype : _DtypeLike = ..., order : Optional [str ] = ...
470
+ shape : _ShapeLike , dtype : DtypeLike = ..., order : Optional [str ] = ...
514
471
) -> ndarray : ...
515
472
def zeros_like (
516
- a : _ArrayLike ,
473
+ a : ArrayLike ,
517
474
dtype : Optional [dtype ] = ...,
518
475
order : str = ...,
519
476
subok : bool = ...,
520
477
shape : Optional [Union [int , Sequence [int ]]] = ...,
521
478
) -> ndarray : ...
522
479
def ones_like (
523
- a : _ArrayLike ,
480
+ a : ArrayLike ,
524
481
dtype : Optional [dtype ] = ...,
525
482
order : str = ...,
526
483
subok : bool = ...,
@@ -530,43 +487,43 @@ def full(
530
487
shape : _ShapeLike , fill_value : Any , dtype : Optional [dtype ] = ..., order : str = ...
531
488
) -> ndarray : ...
532
489
def full_like (
533
- a : _ArrayLike ,
490
+ a : ArrayLike ,
534
491
fill_value : Any ,
535
492
dtype : Optional [dtype ] = ...,
536
493
order : str = ...,
537
494
subok : bool = ...,
538
495
shape : Optional [_ShapeLike ] = ...,
539
496
) -> ndarray : ...
540
497
def count_nonzero (
541
- a : _ArrayLike , axis : Optional [Union [int , Tuple [int ], Tuple [int , int ]]] = ...
498
+ a : ArrayLike , axis : Optional [Union [int , Tuple [int ], Tuple [int , int ]]] = ...
542
499
) -> Union [int , ndarray ]: ...
543
500
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 : ...
501
+ def argwhere (a : ArrayLike ) -> ndarray : ...
502
+ def flatnonzero (a : ArrayLike ) -> ndarray : ...
503
+ def correlate (a : ArrayLike , v : ArrayLike , mode : str = ...) -> ndarray : ...
504
+ def convolve (a : ArrayLike , v : ArrayLike , mode : str = ...) -> ndarray : ...
505
+ def outer (a : ArrayLike , b : ArrayLike , out : ndarray = ...) -> ndarray : ...
549
506
def tensordot (
550
- a : _ArrayLike ,
551
- b : _ArrayLike ,
507
+ a : ArrayLike ,
508
+ b : ArrayLike ,
552
509
axes : Union [
553
510
int , Tuple [int , int ], Tuple [Tuple [int , int ], ...], Tuple [List [int , int ], ...]
554
511
] = ...,
555
512
) -> ndarray : ...
556
513
def roll (
557
- a : _ArrayLike ,
514
+ a : ArrayLike ,
558
515
shift : Union [int , Tuple [int , ...]],
559
516
axis : Optional [Union [int , Tuple [int , ...]]] = ...,
560
517
) -> ndarray : ...
561
- def rollaxis (a : _ArrayLike , axis : int , start : int = ...) -> ndarray : ...
518
+ def rollaxis (a : ArrayLike , axis : int , start : int = ...) -> ndarray : ...
562
519
def moveaxis (
563
520
a : ndarray ,
564
521
source : Union [int , Sequence [int ]],
565
522
destination : Union [int , Sequence [int ]],
566
523
) -> ndarray : ...
567
524
def cross (
568
- a : _ArrayLike ,
569
- b : _ArrayLike ,
525
+ a : ArrayLike ,
526
+ b : ArrayLike ,
570
527
axisa : int = ...,
571
528
axisb : int = ...,
572
529
axisc : int = ...,
@@ -581,21 +538,21 @@ def binary_repr(num: int, width: Optional[int] = ...) -> str: ...
581
538
def base_repr (number : int , base : int = ..., padding : int = ...) -> str : ...
582
539
def identity (n : int , dtype : Optional [dtype ] = ...) -> ndarray : ...
583
540
def allclose (
584
- a : _ArrayLike ,
585
- b : _ArrayLike ,
541
+ a : ArrayLike ,
542
+ b : ArrayLike ,
586
543
rtol : float = ...,
587
544
atol : float = ...,
588
545
equal_nan : bool = ...,
589
546
) -> bool : ...
590
547
def isclose (
591
- a : _ArrayLike ,
592
- b : _ArrayLike ,
548
+ a : ArrayLike ,
549
+ b : ArrayLike ,
593
550
rtol : float = ...,
594
551
atol : float = ...,
595
552
equal_nan : bool = ...,
596
553
) -> Union [bool_ , ndarray ]: ...
597
- def array_equal (a1 : _ArrayLike , a2 : _ArrayLike ) -> bool : ...
598
- def array_equiv (a1 : _ArrayLike , a2 : _ArrayLike ) -> bool : ...
554
+ def array_equal (a1 : ArrayLike , a2 : ArrayLike ) -> bool : ...
555
+ def array_equiv (a1 : ArrayLike , a2 : ArrayLike ) -> bool : ...
599
556
600
557
#
601
558
# Constants
@@ -649,7 +606,7 @@ class ufunc:
649
606
def __name__ (self ) -> str : ...
650
607
def __call__ (
651
608
self ,
652
- * args : _ArrayLike ,
609
+ * args : ArrayLike ,
653
610
out : Optional [Union [ndarray , Tuple [ndarray , ...]]] = ...,
654
611
where : Optional [ndarray ] = ...,
655
612
# The list should be a list of tuples of ints, but since we
@@ -664,7 +621,7 @@ class ufunc:
664
621
casting : str = ...,
665
622
# TODO: make this precise when we can use Literal.
666
623
order : Optional [str ] = ...,
667
- dtype : Optional [ _DtypeLike ] = ...,
624
+ dtype : DtypeLike = ...,
668
625
subok : bool = ...,
669
626
signature : Union [str , Tuple [str ]] = ...,
670
627
# In reality this should be a length of list 3 containing an
@@ -876,56 +833,56 @@ def take(
876
833
) -> _ScalarNumpy : ...
877
834
@overload
878
835
def take (
879
- a : _ArrayLike ,
836
+ a : ArrayLike ,
880
837
indices : int ,
881
838
axis : Optional [int ] = ...,
882
839
out : Optional [ndarray ] = ...,
883
840
mode : _Mode = ...,
884
841
) -> _ScalarNumpy : ...
885
842
@overload
886
843
def take (
887
- a : _ArrayLike ,
844
+ a : ArrayLike ,
888
845
indices : _ArrayLikeIntOrBool ,
889
846
axis : Optional [int ] = ...,
890
847
out : Optional [ndarray ] = ...,
891
848
mode : _Mode = ...,
892
849
) -> Union [_ScalarNumpy , ndarray ]: ...
893
- def reshape (a : _ArrayLike , newshape : _ShapeLike , order : _Order = ...) -> ndarray : ...
850
+ def reshape (a : ArrayLike , newshape : _ShapeLike , order : _Order = ...) -> ndarray : ...
894
851
@overload
895
852
def choose (
896
853
a : _ScalarIntOrBool ,
897
- choices : Union [Sequence [_ArrayLike ], ndarray ],
854
+ choices : Union [Sequence [ArrayLike ], ndarray ],
898
855
out : Optional [ndarray ] = ...,
899
856
mode : _Mode = ...,
900
857
) -> _ScalarIntOrBool : ...
901
858
@overload
902
859
def choose (
903
860
a : _IntOrBool ,
904
- choices : Union [Sequence [_ArrayLike ], ndarray ],
861
+ choices : Union [Sequence [ArrayLike ], ndarray ],
905
862
out : Optional [ndarray ] = ...,
906
863
mode : _Mode = ...,
907
864
) -> Union [integer , bool_ ]: ...
908
865
@overload
909
866
def choose (
910
867
a : _ArrayLikeIntOrBool ,
911
- choices : Union [Sequence [_ArrayLike ], ndarray ],
868
+ choices : Union [Sequence [ArrayLike ], ndarray ],
912
869
out : Optional [ndarray ] = ...,
913
870
mode : _Mode = ...,
914
871
) -> ndarray : ...
915
872
def repeat (
916
- a : _ArrayLike , repeats : _ArrayLikeIntOrBool , axis : Optional [int ] = ...
873
+ a : ArrayLike , repeats : _ArrayLikeIntOrBool , axis : Optional [int ] = ...
917
874
) -> ndarray : ...
918
875
def put (
919
- a : ndarray , ind : _ArrayLikeIntOrBool , v : _ArrayLike , mode : _Mode = ...
876
+ a : ndarray , ind : _ArrayLikeIntOrBool , v : ArrayLike , mode : _Mode = ...
920
877
) -> None : ...
921
878
def swapaxes (
922
- a : Union [Sequence [_ArrayLike ], ndarray ], axis1 : int , axis2 : int
879
+ a : Union [Sequence [ArrayLike ], ndarray ], axis1 : int , axis2 : int
923
880
) -> ndarray : ...
924
881
def transpose (
925
- a : _ArrayLike , axes : Union [None , Sequence [int ], ndarray ] = ...
882
+ a : ArrayLike , axes : Union [None , Sequence [int ], ndarray ] = ...
926
883
) -> ndarray : ...
927
884
def partition (
928
- a : _ArrayLike ,
885
+ a : ArrayLike ,
929
886
kth : _ArrayLikeIntOrBool ,
930
887
axis : Optional [int ] = ...,
931
888
kind : _PartitionKind = ...,
@@ -949,20 +906,20 @@ def argpartition(
949
906
) -> ndarray : ...
950
907
@overload
951
908
def argpartition (
952
- a : _ArrayLike ,
909
+ a : ArrayLike ,
953
910
kth : _ArrayLikeIntOrBool ,
954
911
axis : Optional [int ] = ...,
955
912
kind : _PartitionKind = ...,
956
913
order : Union [None , str , Sequence [str ]] = ...,
957
914
) -> ndarray : ...
958
915
def sort (
959
- a : Union [Sequence [_ArrayLike ], ndarray ],
916
+ a : Union [Sequence [ArrayLike ], ndarray ],
960
917
axis : Optional [int ] = ...,
961
918
kind : Optional [_SortKind ] = ...,
962
919
order : Union [None , str , Sequence [str ]] = ...,
963
920
) -> ndarray : ...
964
921
def argsort (
965
- a : Union [Sequence [_ArrayLike ], ndarray ],
922
+ a : Union [Sequence [ArrayLike ], ndarray ],
966
923
axis : Optional [int ] = ...,
967
924
kind : Optional [_SortKind ] = ...,
968
925
order : Union [None , str , Sequence [str ]] = ...,
0 commit comments