@@ -124,6 +124,17 @@ from numpy._typing import (
124
124
_VoidCodes ,
125
125
_ObjectCodes ,
126
126
127
+ _UnsignedIntegerCodes ,
128
+ _SignedIntegerCodes ,
129
+ _IntegerCodes ,
130
+ _FloatingCodes ,
131
+ _ComplexFloatingCodes ,
132
+ _InexactCodes ,
133
+ _NumberCodes ,
134
+ _CharacterCodes ,
135
+ _FlexibleCodes ,
136
+ _GenericCodes ,
137
+
127
138
# Ufuncs
128
139
_UFunc_Nin1_Nout1 ,
129
140
_UFunc_Nin2_Nout1 ,
@@ -751,6 +762,22 @@ _DTypeBuiltinKind: TypeAlias = L[
751
762
2 , # user-defined
752
763
]
753
764
765
+ # NOTE: `type[S] | type[T]` is equivalent to `type[S | T]`
766
+ _UnsignedIntegerCType : TypeAlias = type [
767
+ ct .c_uint8 | ct .c_uint16 | ct .c_uint32 | ct .c_uint64
768
+ | ct .c_ubyte | ct .c_ushort | ct .c_uint | ct .c_ulong | ct .c_ulonglong
769
+ | ct .c_size_t | ct .c_void_p
770
+ ]
771
+ _SignedIntegerCType : TypeAlias = type [
772
+ ct .c_int8 | ct .c_int16 | ct .c_int32 | ct .c_int64
773
+ | ct .c_byte | ct .c_short | ct .c_int | ct .c_long | ct .c_longlong
774
+ | ct .c_ssize_t
775
+ ]
776
+ _FloatingCType : TypeAlias = type [ct .c_float | ct .c_double | ct .c_longdouble ]
777
+ _IntegerCType : TypeAlias = _UnsignedIntegerCType | _SignedIntegerCType
778
+ _NumberCType : TypeAlias = _IntegerCType | _IntegerCType
779
+ _GenericCType : TypeAlias = _NumberCType | type [ct .c_bool | ct .c_char | ct .py_object [Any ]]
780
+
754
781
# some commonly used builtin types that are known to result in a
755
782
# `dtype[object_]`, when their *type* is passed to the `dtype` constructor
756
783
# NOTE: `builtins.object` should not be included here
@@ -805,7 +832,6 @@ class dtype(Generic[_DTypeScalar_co]):
805
832
# NOTE: This also accepts `dtype: type[complex | float | int | bool]`
806
833
@overload
807
834
def __new__ (cls , dtype : type [complex ], align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [complex128 | float64 | int_ | np .bool ]: ...
808
-
809
835
# TODO: This weird `memoryview` order is needed to work around a bug in
810
836
# typeshed, which causes typecheckers to treat `memoryview` as a subtype
811
837
# of `bytes`, even though there's no mention of that in the typing docs.
@@ -822,6 +848,9 @@ class dtype(Generic[_DTypeScalar_co]):
822
848
def __new__ (cls , dtype : type [builtins .str | bytes ], align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [character ]: ...
823
849
@overload
824
850
def __new__ (cls , dtype : type [memoryview | builtins .str | bytes ], align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [flexible ]: ...
851
+ # NOTE: `dtype: type[object]` also accepts e.g. `type[object | complex | ...]`
852
+ @overload
853
+ def __new__ (cls , dtype : type [_BuiltinObjectLike ], align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [object_ ]: ...
825
854
826
855
# `unsignedinteger` string-based representations and ctypes
827
856
@overload
@@ -846,8 +875,6 @@ class dtype(Generic[_DTypeScalar_co]):
846
875
def __new__ (cls , dtype : _ULongCodes | type [ct .c_ulong ], align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [ulong ]: ...
847
876
@overload
848
877
def __new__ (cls , dtype : _ULongLongCodes | type [ct .c_ulonglong ], align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [ulonglong ]: ...
849
- # TODO: (dtype: Union[{all unsigned integer codes}]) -> dtype[unsignedinteger]
850
- # TODO: (dtype: type[Union[{all unsigned integer ctypes}]]) -> dtype[unsignedinteger]
851
878
852
879
# `signedinteger` string-based representations and ctypes
853
880
@overload
@@ -870,10 +897,6 @@ class dtype(Generic[_DTypeScalar_co]):
870
897
def __new__ (cls , dtype : _LongCodes | type [ct .c_long ], align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [long ]: ...
871
898
@overload
872
899
def __new__ (cls , dtype : _LongLongCodes | type [ct .c_longlong ], align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [longlong ]: ...
873
- # TODO: (dtype: Union[{all signed integer codes}]) -> dtype[signedinteger]
874
- # TODO: (dtype: type[Union[{all signed integer ctypes}]]) -> dtype[signedinteger]
875
- # TODO: (dtype: Union[{all integer codes}]) -> dtype[integer]
876
- # TODO: (dtype: type[Union[{all integer ctypes}]]) -> dtype[integer]
877
900
878
901
# `floating` string-based representations and ctypes
879
902
@overload
@@ -890,8 +913,6 @@ class dtype(Generic[_DTypeScalar_co]):
890
913
def __new__ (cls , dtype : _DoubleCodes | type [ct .c_double ], align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [double ]: ...
891
914
@overload
892
915
def __new__ (cls , dtype : _LongDoubleCodes | type [ct .c_longdouble ], align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [longdouble ]: ...
893
- # TODO: (dtype: Union[{all floating codes}]) -> dtype[floating]
894
- # TODO: (dtype: type[ct.c_float | ct.c_double | ct.c_longdouble]) -> dtype[floating]
895
916
896
917
# `complexfloating` string-based representations
897
918
@overload
@@ -904,10 +925,6 @@ class dtype(Generic[_DTypeScalar_co]):
904
925
def __new__ (cls , dtype : _CDoubleCodes , align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [cdouble ]: ...
905
926
@overload
906
927
def __new__ (cls , dtype : _CLongDoubleCodes , align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [clongdouble ]: ...
907
- # TODO: (dtype: Union[{all complex codes}]) -> dtype[complexfloating]
908
- # TODO: (dtype: Union[{all inexact codes}]) -> dtype[inexact]
909
- # TODO: (dtype: Union[{all number codes}]) -> dtype[number]
910
- # TODO: (dtype: type[Union[{all number ctypes}]]) -> dtype[number]
911
928
912
929
# Miscellaneous string-based representations and ctypes
913
930
@overload
@@ -916,55 +933,111 @@ class dtype(Generic[_DTypeScalar_co]):
916
933
def __new__ (cls , dtype : _TD64Codes , align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [timedelta64 ]: ...
917
934
@overload
918
935
def __new__ (cls , dtype : _DT64Codes , align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [datetime64 ]: ...
919
-
920
936
@overload
921
937
def __new__ (cls , dtype : _StrCodes , align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [str_ ]: ...
922
938
@overload
923
939
def __new__ (cls , dtype : _BytesCodes | type [ct .c_char ], align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [bytes_ ]: ...
924
940
@overload
925
- def __new__ (cls , dtype : _BytesCodes , align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [bytes_ ]: ...
926
-
927
- # TODO: (dtype: _StrCodes | _BytesCodes) -> dtype[character]
928
- @overload
929
- def __new__ (cls , dtype : _VoidCodes , align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [void ]: ...
930
- # TODO: (dtype: _StrCodes | _BytesCodes | _VoidCodes) -> dtype[flexible]
941
+ def __new__ (cls , dtype : _VoidCodes | _VoidDTypeLike , align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [void ]: ...
931
942
@overload
932
943
def __new__ (cls , dtype : _ObjectCodes | type [ct .py_object [Any ]], align : builtins .bool = ..., copy : builtins .bool = ..., metadata : dict [builtins .str , Any ] = ...) -> dtype [object_ ]: ...
933
- # TODO: (dtype: Union[{all literal codes}]) -> dtype[generic]
934
944
935
- # Structured (`void`-like) dtypes
945
+ # Combined char-codes and ctypes, analogous to the scalar-type hierarchy
936
946
@overload
937
947
def __new__ (
938
948
cls ,
939
- dtype : _VoidDTypeLike ,
949
+ dtype : _UnsignedIntegerCodes | _UnsignedIntegerCType ,
940
950
align : builtins .bool = ...,
941
951
copy : builtins .bool = ...,
942
952
metadata : dict [builtins .str , Any ] = ...,
943
- ) -> dtype [void ]: ...
944
-
945
- # Handle strings that can't be expressed as literals; i.e. S1, S2, ...
946
- # NOTE: This isn't limited to flexible types, because `dtype: str` also
947
- # accepts e.g. `str | Literal['f8']`
953
+ ) -> dtype [unsignedinteger [Any ]]: ...
948
954
@overload
949
955
def __new__ (
950
956
cls ,
951
- dtype : builtins . str ,
957
+ dtype : _SignedIntegerCodes | _SignedIntegerCType ,
952
958
align : builtins .bool = ...,
953
959
copy : builtins .bool = ...,
954
960
metadata : dict [builtins .str , Any ] = ...,
955
- ) -> dtype [Any ]: ...
961
+ ) -> dtype [signedinteger [Any ]]: ...
962
+ @overload
963
+ def __new__ (
964
+ cls ,
965
+ dtype : _IntegerCodes | _IntegerCType ,
966
+ align : builtins .bool = ...,
967
+ copy : builtins .bool = ...,
968
+ metadata : dict [builtins .str , Any ] = ...,
969
+ ) -> dtype [integer [Any ]]: ...
970
+ @overload
971
+ def __new__ (
972
+ cls ,
973
+ dtype : _FloatingCodes | _FloatingCType ,
974
+ align : builtins .bool = ...,
975
+ copy : builtins .bool = ...,
976
+ metadata : dict [builtins .str , Any ] = ...,
977
+ ) -> dtype [floating [Any ]]: ...
978
+ @overload
979
+ def __new__ (
980
+ cls ,
981
+ dtype : _ComplexFloatingCodes ,
982
+ align : builtins .bool = ...,
983
+ copy : builtins .bool = ...,
984
+ metadata : dict [builtins .str , Any ] = ...,
985
+ ) -> dtype [complexfloating [Any , Any ]]: ...
986
+ @overload
987
+ def __new__ (
988
+ cls ,
989
+ dtype : _InexactCodes | _FloatingCType ,
990
+ align : builtins .bool = ...,
991
+ copy : builtins .bool = ...,
992
+ metadata : dict [builtins .str , Any ] = ...,
993
+ ) -> dtype [inexact [Any ]]: ...
994
+ @overload
995
+ def __new__ (
996
+ cls ,
997
+ dtype : _NumberCodes | _NumberCType ,
998
+ align : builtins .bool = ...,
999
+ copy : builtins .bool = ...,
1000
+ metadata : dict [builtins .str , Any ] = ...,
1001
+ ) -> dtype [number [Any ]]: ...
1002
+ @overload
1003
+ def __new__ (
1004
+ cls ,
1005
+ dtype : _CharacterCodes | type [ct .c_char ],
1006
+ align : builtins .bool = ...,
1007
+ copy : builtins .bool = ...,
1008
+ metadata : dict [builtins .str , Any ] = ...,
1009
+ ) -> dtype [character ]: ...
1010
+ @overload
1011
+ def __new__ (
1012
+ cls ,
1013
+ dtype : _FlexibleCodes | type [ct .c_char ],
1014
+ align : builtins .bool = ...,
1015
+ copy : builtins .bool = ...,
1016
+ metadata : dict [builtins .str , Any ] = ...,
1017
+ ) -> dtype [flexible ]: ...
1018
+ @overload
1019
+ def __new__ (
1020
+ cls ,
1021
+ dtype : _GenericCodes | _GenericCType ,
1022
+ align : builtins .bool = ...,
1023
+ copy : builtins .bool = ...,
1024
+ metadata : dict [builtins .str , Any ] = ...,
1025
+ ) -> dtype [generic ]: ...
956
1026
957
- # Catch-all overload for object-likes
958
- # NOTE: `dtype: type[object]` also accepts e.g. `type[object | complex | ...]`
1027
+ # Handle strings that can't be expressed as literals; i.e. "S1", "S2", ...
959
1028
@overload
960
1029
def __new__ (
961
1030
cls ,
962
- dtype : type [ _BuiltinObjectLike ] ,
1031
+ dtype : builtins . str ,
963
1032
align : builtins .bool = ...,
964
1033
copy : builtins .bool = ...,
965
1034
metadata : dict [builtins .str , Any ] = ...,
966
- ) -> dtype [object_ ]: ...
967
- # NOTE: `object_ | Any` is *not* equivalent to `Any`, see:
1035
+ ) -> dtype [Any ]: ...
1036
+
1037
+ # Catch-all overload for object-likes
1038
+ # NOTE: `object_ | Any` is *not* equivalent to `Any` -- it describes some
1039
+ # (static) type `T` s.t. `object_ <: T <: builtins.object` (`<:` denotes
1040
+ # the subtyping relation, the (gradual) typing analogue of `issubclass()`).
968
1041
# https://typing.readthedocs.io/en/latest/spec/concepts.html#union-types
969
1042
@overload
970
1043
def __new__ (
0 commit comments