-
-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Expand file tree
/
Copy path_dtype_like.py
More file actions
108 lines (86 loc) · 3.62 KB
/
_dtype_like.py
File metadata and controls
108 lines (86 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from collections.abc import Sequence
from typing import Any, NotRequired, Protocol, TypedDict, runtime_checkable
import numpy as np
from ._char_codes import (
_BoolCodes,
_BytesCodes,
_ComplexFloatingCodes,
_DT64Codes,
_FloatingCodes,
_NumberCodes,
_ObjectCodes,
_SignedIntegerCodes,
_StrCodes,
_TD64Codes,
_UnsignedIntegerCodes,
_VoidCodes,
)
type _DTypeLikeNested = Any # TODO: wait for support for recursive types
class _DTypeDict(TypedDict):
names: Sequence[str]
formats: Sequence[_DTypeLikeNested]
# Only `str` elements are usable as indexing aliases,
# but `titles` can in principle accept any object
offsets: NotRequired[Sequence[int]]
titles: NotRequired[Sequence[Any]]
itemsize: NotRequired[int]
aligned: NotRequired[bool]
# A protocol for anything with the dtype attribute
@runtime_checkable
class _HasDType[DTypeT: np.dtype](Protocol):
@property
def dtype(self) -> DTypeT: ...
@runtime_checkable
class _HasNumPyDType[DTypeT: np.dtype](Protocol):
@property
def __numpy_dtype__(self, /) -> DTypeT: ...
type _SupportsDType[DTypeT: np.dtype] = _HasDType[DTypeT] | _HasNumPyDType[DTypeT]
# A subset of `npt.DTypeLike` that can be parametrized w.r.t. `np.generic`
type _DTypeLike[ScalarT: np.generic] = (
type[ScalarT] | np.dtype[ScalarT] | _SupportsDType[np.dtype[ScalarT]]
)
# Would create a dtype[np.void]
type _VoidDTypeLike = (
# If a tuple, then it can be either:
# - (flexible_dtype, itemsize)
# - (fixed_dtype, shape)
# - (base_dtype, new_dtype)
# But because `_DTypeLikeNested = Any`, the first two cases are redundant
# tuple[_DTypeLikeNested, int] | tuple[_DTypeLikeNested, _ShapeLike] |
tuple[_DTypeLikeNested, _DTypeLikeNested]
# [(field_name, field_dtype, field_shape), ...]
# The type here is quite broad because NumPy accepts quite a wide
# range of inputs inside the list; see the tests for some examples.
| list[Any]
# {'names': ..., 'formats': ..., 'offsets': ..., 'titles': ..., 'itemsize': ...}
| _DTypeDict
)
# Aliases for commonly used dtype-like objects.
# Note that the precision of `np.number` subclasses is ignored herein.
type _DTypeLikeBool = type[bool] | _DTypeLike[np.bool] | _BoolCodes
type _DTypeLikeInt = type[int] | _DTypeLike[np.signedinteger] | _SignedIntegerCodes
type _DTypeLikeUInt = _DTypeLike[np.unsignedinteger] | _UnsignedIntegerCodes
type _DTypeLikeFloat = type[float] | _DTypeLike[np.floating] | _FloatingCodes
type _DTypeLikeComplex = (
type[complex] | _DTypeLike[np.complexfloating] | _ComplexFloatingCodes
)
type _DTypeLikeComplex_co = (
type[complex] | _DTypeLike[np.bool | np.number] | _BoolCodes | _NumberCodes
)
type _DTypeLikeDT64 = _DTypeLike[np.timedelta64] | _TD64Codes
type _DTypeLikeTD64 = _DTypeLike[np.datetime64] | _DT64Codes
type _DTypeLikeBytes = type[bytes] | _DTypeLike[np.bytes_] | _BytesCodes
type _DTypeLikeStr = type[str] | _DTypeLike[np.str_] | _StrCodes
type _DTypeLikeVoid = (
type[memoryview] | _DTypeLike[np.void] | _VoidDTypeLike | _VoidCodes
)
type _DTypeLikeObject = type[object] | _DTypeLike[np.object_] | _ObjectCodes
# Anything that can be coerced into numpy.dtype.
# Reference: https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
type DTypeLike = type | str | np.dtype | _SupportsDType[np.dtype] | _VoidDTypeLike
# NOTE: while it is possible to provide the dtype as a dict of
# dtype-like objects (e.g. `{'field1': ..., 'field2': ..., ...}`),
# this syntax is officially discouraged and
# therefore not included in the type-union defining `DTypeLike`.
#
# See https://github.com/numpy/numpy/issues/16891 for more details.