Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9772bea

Browse filesBrowse files
authored
Merge pull request #18236 from BvB93/dtype-like
ENH: Add aliases for commonly used dtype-like objects
2 parents b969f8c + d788788 commit 9772bea
Copy full SHA for 9772bea

File tree

Expand file treeCollapse file tree

4 files changed

+182
-13
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+182
-13
lines changed

‎numpy/__init__.pyi

Copy file name to clipboardExpand all lines: numpy/__init__.pyi
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,14 +682,13 @@ class dtype(Generic[_DTypeScalar_co]):
682682
align: bool = ...,
683683
copy: bool = ...,
684684
) -> dtype[_DTypeScalar_co]: ...
685-
# TODO: handle _SupportsDType better
686685
@overload
687686
def __new__(
688687
cls,
689-
dtype: _SupportsDType,
688+
dtype: _SupportsDType[dtype[_DTypeScalar_co]],
690689
align: bool = ...,
691690
copy: bool = ...,
692-
) -> dtype[Any]: ...
691+
) -> dtype[_DTypeScalar_co]: ...
693692
# Handle strings that can't be expressed as literals; i.e. s1, s2, ...
694693
@overload
695694
def __new__(

‎numpy/typing/__init__.py

Copy file name to clipboardExpand all lines: numpy/typing/__init__.py
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,22 @@ class _8Bit(_16Bit): ... # type: ignore[misc]
297297
_VoidLike_co,
298298
)
299299
from ._shape import _Shape, _ShapeLike
300-
from ._dtype_like import _SupportsDType, _VoidDTypeLike, DTypeLike as DTypeLike
300+
from ._dtype_like import (
301+
DTypeLike as DTypeLike,
302+
_SupportsDType,
303+
_VoidDTypeLike,
304+
_DTypeLikeBool,
305+
_DTypeLikeUInt,
306+
_DTypeLikeInt,
307+
_DTypeLikeFloat,
308+
_DTypeLikeComplex,
309+
_DTypeLikeTD64,
310+
_DTypeLikeDT64,
311+
_DTypeLikeObject,
312+
_DTypeLikeVoid,
313+
_DTypeLikeStr,
314+
_DTypeLikeBytes,
315+
)
301316
from ._array_like import (
302317
ArrayLike as ArrayLike,
303318
_ArrayLike,

‎numpy/typing/_dtype_like.py

Copy file name to clipboardExpand all lines: numpy/typing/_dtype_like.py
+155-6Lines changed: 155 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
2-
from typing import Any, List, Sequence, Tuple, Union, TYPE_CHECKING
2+
from typing import Any, List, Sequence, Tuple, Union, Type, TypeVar, TYPE_CHECKING
33

4-
from numpy import dtype
4+
import numpy as np
55
from ._shape import _ShapeLike
66

77
if sys.version_info >= (3, 8):
@@ -15,6 +15,48 @@
1515
else:
1616
HAVE_PROTOCOL = True
1717

18+
from ._char_codes import (
19+
_BoolCodes,
20+
_UInt8Codes,
21+
_UInt16Codes,
22+
_UInt32Codes,
23+
_UInt64Codes,
24+
_Int8Codes,
25+
_Int16Codes,
26+
_Int32Codes,
27+
_Int64Codes,
28+
_Float16Codes,
29+
_Float32Codes,
30+
_Float64Codes,
31+
_Complex64Codes,
32+
_Complex128Codes,
33+
_ByteCodes,
34+
_ShortCodes,
35+
_IntCCodes,
36+
_IntPCodes,
37+
_IntCodes,
38+
_LongLongCodes,
39+
_UByteCodes,
40+
_UShortCodes,
41+
_UIntCCodes,
42+
_UIntPCodes,
43+
_UIntCodes,
44+
_ULongLongCodes,
45+
_HalfCodes,
46+
_SingleCodes,
47+
_DoubleCodes,
48+
_LongDoubleCodes,
49+
_CSingleCodes,
50+
_CDoubleCodes,
51+
_CLongDoubleCodes,
52+
_DT64Codes,
53+
_TD64Codes,
54+
_StrCodes,
55+
_BytesCodes,
56+
_VoidCodes,
57+
_ObjectCodes,
58+
)
59+
1860
_DTypeLikeNested = Any # TODO: wait for support for recursive types
1961

2062
if TYPE_CHECKING or HAVE_PROTOCOL:
@@ -30,9 +72,12 @@ class _DTypeDict(_DTypeDictBase, total=False):
3072
itemsize: int
3173
aligned: bool
3274

75+
_DType_co = TypeVar("_DType_co", covariant=True, bound=np.dtype)
76+
3377
# A protocol for anything with the dtype attribute
34-
class _SupportsDType(Protocol):
35-
dtype: _DTypeLikeNested
78+
class _SupportsDType(Protocol[_DType_co]):
79+
@property
80+
def dtype(self) -> _DType_co: ...
3681

3782
else:
3883
_DTypeDict = Any
@@ -61,13 +106,13 @@ class _SupportsDType(Protocol):
61106
# Anything that can be coerced into numpy.dtype.
62107
# Reference: https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
63108
DTypeLike = Union[
64-
dtype,
109+
np.dtype,
65110
# default data type (float64)
66111
None,
67112
# array-scalar types and generic types
68113
type, # TODO: enumerate these when we add type hints for numpy scalars
69114
# anything with a dtype attribute
70-
_SupportsDType,
115+
"_SupportsDType[np.dtype[Any]]",
71116
# character codes, type strings or comma-separated fields, e.g., 'float64'
72117
str,
73118
_VoidDTypeLike,
@@ -79,3 +124,107 @@ class _SupportsDType(Protocol):
79124
# therefore not included in the Union defining `DTypeLike`.
80125
#
81126
# See https://github.com/numpy/numpy/issues/16891 for more details.
127+
128+
# Aliases for commonly used dtype-like objects.
129+
# Note that the precision of `np.number` subclasses is ignored herein.
130+
_DTypeLikeBool = Union[
131+
Type[bool],
132+
Type[np.bool_],
133+
"np.dtype[np.bool_]",
134+
"_SupportsDType[np.dtype[np.bool_]]",
135+
_BoolCodes,
136+
]
137+
_DTypeLikeUInt = Union[
138+
Type[np.unsignedinteger],
139+
"np.dtype[np.unsignedinteger]",
140+
"_SupportsDType[np.dtype[np.unsignedinteger]]",
141+
_UInt8Codes,
142+
_UInt16Codes,
143+
_UInt32Codes,
144+
_UInt64Codes,
145+
_UByteCodes,
146+
_UShortCodes,
147+
_UIntCCodes,
148+
_UIntPCodes,
149+
_UIntCodes,
150+
_ULongLongCodes,
151+
]
152+
_DTypeLikeInt = Union[
153+
Type[int],
154+
Type[np.signedinteger],
155+
"np.dtype[np.signedinteger]",
156+
"_SupportsDType[np.dtype[np.signedinteger]]",
157+
_Int8Codes,
158+
_Int16Codes,
159+
_Int32Codes,
160+
_Int64Codes,
161+
_ByteCodes,
162+
_ShortCodes,
163+
_IntCCodes,
164+
_IntPCodes,
165+
_IntCodes,
166+
_LongLongCodes,
167+
]
168+
_DTypeLikeFloat = Union[
169+
Type[float],
170+
Type[np.floating],
171+
"np.dtype[np.floating]",
172+
"_SupportsDType[np.dtype[np.floating]]",
173+
_Float16Codes,
174+
_Float32Codes,
175+
_Float64Codes,
176+
_HalfCodes,
177+
_SingleCodes,
178+
_DoubleCodes,
179+
_LongDoubleCodes,
180+
]
181+
_DTypeLikeComplex = Union[
182+
Type[complex],
183+
Type[np.complexfloating],
184+
"np.dtype[np.complexfloating]",
185+
"_SupportsDType[np.dtype[np.complexfloating]]",
186+
_Complex64Codes,
187+
_Complex128Codes,
188+
_CSingleCodes,
189+
_CDoubleCodes,
190+
_CLongDoubleCodes,
191+
]
192+
_DTypeLikeDT64 = Union[
193+
Type[np.timedelta64],
194+
"np.dtype[np.timedelta64]",
195+
"_SupportsDType[np.dtype[np.timedelta64]]",
196+
_TD64Codes,
197+
]
198+
_DTypeLikeTD64 = Union[
199+
Type[np.datetime64],
200+
"np.dtype[np.datetime64]",
201+
"_SupportsDType[np.dtype[np.datetime64]]",
202+
_DT64Codes,
203+
]
204+
_DTypeLikeStr = Union[
205+
Type[str],
206+
Type[np.str_],
207+
"np.dtype[np.str_]",
208+
"_SupportsDType[np.dtype[np.str_]]",
209+
_StrCodes,
210+
]
211+
_DTypeLikeBytes = Union[
212+
Type[bytes],
213+
Type[np.bytes_],
214+
"np.dtype[np.bytes_]",
215+
"_SupportsDType[np.dtype[np.bytes_]]",
216+
_BytesCodes,
217+
]
218+
_DTypeLikeVoid = Union[
219+
Type[np.void],
220+
"np.dtype[np.void]",
221+
"_SupportsDType[np.dtype[np.void]]",
222+
_VoidCodes,
223+
_VoidDTypeLike,
224+
]
225+
_DTypeLikeObject = Union[
226+
type,
227+
"np.dtype[np.object_]",
228+
"_SupportsDType[np.dtype[np.object_]]",
229+
_ObjectCodes,
230+
]

‎numpy/typing/tests/data/fail/dtype.py

Copy file name to clipboardExpand all lines: numpy/typing/tests/data/fail/dtype.py
+9-3Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import numpy as np
22

3-
class Test:
4-
not_dtype = float
53

4+
class Test1:
5+
not_dtype = np.dtype(float)
66

7-
np.dtype(Test()) # E: No overload variant of "dtype" matches
7+
8+
class Test2:
9+
dtype = float
10+
11+
12+
np.dtype(Test1()) # E: No overload variant of "dtype" matches
13+
np.dtype(Test2()) # E: incompatible type
814

915
np.dtype( # E: No overload variant of "dtype" matches
1016
{

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.