-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmytypes.py
More file actions
67 lines (51 loc) · 1.79 KB
/
mytypes.py
File metadata and controls
67 lines (51 loc) · 1.79 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
import sys
from typing import TYPE_CHECKING, Any, List, Sequence, Tuple, Union, overload
# %% Taken from https://github.com/numpy/numpy/tree/master/numpy/typing
from numpy import dtype, ndarray
if sys.version_info >= (3, 8):
from typing import Protocol, TypedDict
HAVE_PROTOCOL = True
else:
try:
from typing_extensions import Protocol, TypedDict
except ImportError:
HAVE_PROTOCOL = False
else:
HAVE_PROTOCOL = True
_Shape = Tuple[int, ...]
# Anything that can be coerced to a shape tuple
_ShapeLike = Union[int, Sequence[int]]
_DtypeLikeNested = Any # TODO: wait for support for recursive types
if TYPE_CHECKING or HAVE_PROTOCOL:
# Mandatory keys
class _DtypeDictBase(TypedDict):
names: Sequence[str]
formats: Sequence[_DtypeLikeNested]
# Mandatory + optional keys
class _DtypeDict(_DtypeDictBase, total=False):
offsets: Sequence[int]
# Only `str` elements are usable as indexing aliases, but all objects are legal
titles: Sequence[Any]
itemsize: int
aligned: bool
# A protocol for anything with the dtype attribute
class _SupportsDtype(Protocol):
dtype: _DtypeLikeNested
else:
_DtypeDict = Any
_SupportsDtype = Any
DtypeLike = Union[
dtype, None, type, _SupportsDtype, str, Tuple[_DtypeLikeNested, int],
Tuple[_DtypeLikeNested, _ShapeLike], List[Any], _DtypeDict,
Tuple[_DtypeLikeNested, _DtypeLikeNested],
]
if TYPE_CHECKING or HAVE_PROTOCOL:
class _SupportsArray(Protocol):
@overload
def __array__(self, __dtype: DtypeLike = ...) -> ndarray: ...
@overload
def __array__(self, dtype: DtypeLike = ...) -> ndarray: ...
else:
_SupportsArray = Any
ArrayLike = Union[bool, int, float, complex, _SupportsArray, Sequence]
# %%