|
1 | 1 | import sys |
| 2 | +import types |
2 | 3 | from typing import ( |
3 | | - TypeVar, |
4 | | - Optional, |
5 | 4 | Type, |
6 | 5 | Union, |
7 | 6 | Tuple, |
8 | | - Sequence, |
9 | 7 | overload, |
10 | 8 | Any, |
11 | 9 | TypeVar, |
12 | 10 | Dict, |
13 | 11 | List, |
| 12 | + Iterable, |
14 | 13 | ) |
15 | 14 |
|
16 | 15 | from numpy import ( |
@@ -48,58 +47,94 @@ from numpy.core._type_aliases import ( |
48 | 47 | sctypes as sctypes, |
49 | 48 | ) |
50 | 49 |
|
51 | | -from numpy.typing import DTypeLike, ArrayLike |
| 50 | +from numpy.typing import DTypeLike, ArrayLike, _SupportsDType |
52 | 51 |
|
53 | 52 | if sys.version_info >= (3, 8): |
54 | | - from typing import Literal, Protocol, TypedDict |
| 53 | + from typing import Literal as L, Protocol, TypedDict |
55 | 54 | else: |
56 | | - from typing_extensions import Literal, Protocol, TypedDict |
| 55 | + from typing_extensions import Literal as L, Protocol, TypedDict |
57 | 56 |
|
58 | 57 | _T = TypeVar("_T") |
59 | | -_ScalarType = TypeVar("_ScalarType", bound=generic) |
| 58 | +_SCT = TypeVar("_SCT", bound=generic) |
| 59 | + |
| 60 | +# A paramtrizable subset of `npt.DTypeLike` |
| 61 | +_DTypeLike = Union[ |
| 62 | + Type[_SCT], |
| 63 | + dtype[_SCT], |
| 64 | + _SupportsDType[dtype[_SCT]], |
| 65 | +] |
60 | 66 |
|
61 | 67 | class _CastFunc(Protocol): |
62 | 68 | def __call__( |
63 | 69 | self, x: ArrayLike, k: DTypeLike = ... |
64 | 70 | ) -> ndarray[Any, dtype[Any]]: ... |
65 | 71 |
|
66 | 72 | class _TypeCodes(TypedDict): |
67 | | - Character: Literal['c'] |
68 | | - Integer: Literal['bhilqp'] |
69 | | - UnsignedInteger: Literal['BHILQP'] |
70 | | - Float: Literal['efdg'] |
71 | | - Complex: Literal['FDG'] |
72 | | - AllInteger: Literal['bBhHiIlLqQpP'] |
73 | | - AllFloat: Literal['efdgFDG'] |
74 | | - Datetime: Literal['Mm'] |
75 | | - All: Literal['?bhilqpBHILQPefdgFDGSUVOMm'] |
| 73 | + Character: L['c'] |
| 74 | + Integer: L['bhilqp'] |
| 75 | + UnsignedInteger: L['BHILQP'] |
| 76 | + Float: L['efdg'] |
| 77 | + Complex: L['FDG'] |
| 78 | + AllInteger: L['bBhHiIlLqQpP'] |
| 79 | + AllFloat: L['efdgFDG'] |
| 80 | + Datetime: L['Mm'] |
| 81 | + All: L['?bhilqpBHILQPefdgFDGSUVOMm'] |
76 | 82 |
|
77 | 83 | class _typedict(Dict[Type[generic], _T]): |
78 | 84 | def __getitem__(self, key: DTypeLike) -> _T: ... |
79 | 85 |
|
| 86 | +if sys.version_info >= (3, 10): |
| 87 | + _TypeTuple = Union[ |
| 88 | + Type[Any], |
| 89 | + types.Union, |
| 90 | + Tuple[Union[Type[Any], types.Union, Tuple[Any, ...]], ...], |
| 91 | + ] |
| 92 | +else: |
| 93 | + _TypeTuple = Union[ |
| 94 | + Type[Any], |
| 95 | + Tuple[Union[Type[Any], Tuple[Any, ...]], ...], |
| 96 | + ] |
| 97 | + |
80 | 98 | __all__: List[str] |
81 | 99 |
|
82 | | -# TODO: Clean up the annotations for the 7 functions below |
| 100 | +@overload |
| 101 | +def maximum_sctype(t: _DTypeLike[_SCT]) -> Type[_SCT]: ... |
| 102 | +@overload |
| 103 | +def maximum_sctype(t: DTypeLike) -> Type[Any]: ... |
| 104 | + |
| 105 | +@overload |
| 106 | +def issctype(rep: dtype[Any] | Type[Any]) -> bool: ... |
| 107 | +@overload |
| 108 | +def issctype(rep: object) -> L[False]: ... |
83 | 109 |
|
84 | | -def maximum_sctype(t: DTypeLike) -> dtype: ... |
85 | | -def issctype(rep: object) -> bool: ... |
86 | 110 | @overload |
87 | | -def obj2sctype(rep: object) -> Optional[generic]: ... |
| 111 | +def obj2sctype(rep: _DTypeLike[_SCT], default: None = ...) -> None | Type[_SCT]: ... |
88 | 112 | @overload |
89 | | -def obj2sctype(rep: object, default: None) -> Optional[generic]: ... |
| 113 | +def obj2sctype(rep: _DTypeLike[_SCT], default: _T) -> _T | Type[_SCT]: ... |
90 | 114 | @overload |
91 | | -def obj2sctype( |
92 | | - rep: object, default: Type[_T] |
93 | | -) -> Union[generic, Type[_T]]: ... |
94 | | -def issubclass_(arg1: object, arg2: Union[object, Tuple[object, ...]]) -> bool: ... |
95 | | -def issubsctype( |
96 | | - arg1: Union[ndarray, DTypeLike], arg2: Union[ndarray, DTypeLike] |
97 | | -) -> bool: ... |
| 115 | +def obj2sctype(rep: DTypeLike, default: None = ...) -> None | Type[Any]: ... |
| 116 | +@overload |
| 117 | +def obj2sctype(rep: DTypeLike, default: _T) -> _T | Type[Any]: ... |
| 118 | +@overload |
| 119 | +def obj2sctype(rep: object, default: None = ...) -> None: ... |
| 120 | +@overload |
| 121 | +def obj2sctype(rep: object, default: _T) -> _T: ... |
| 122 | + |
| 123 | +@overload |
| 124 | +def issubclass_(arg1: Type[Any], arg2: _TypeTuple) -> bool: ... |
| 125 | +@overload |
| 126 | +def issubclass_(arg1: object, arg2: object) -> L[False]: ... |
| 127 | + |
| 128 | +def issubsctype(arg1: DTypeLike, arg2: DTypeLike) -> bool: ... |
| 129 | + |
98 | 130 | def issubdtype(arg1: DTypeLike, arg2: DTypeLike) -> bool: ... |
99 | | -def sctype2char(sctype: object) -> str: ... |
| 131 | + |
| 132 | +def sctype2char(sctype: DTypeLike) -> str: ... |
| 133 | + |
100 | 134 | def find_common_type( |
101 | | - array_types: Sequence[DTypeLike], scalar_types: Sequence[DTypeLike] |
102 | | -) -> dtype: ... |
| 135 | + array_types: Iterable[DTypeLike], |
| 136 | + scalar_types: Iterable[DTypeLike], |
| 137 | +) -> dtype[Any]: ... |
103 | 138 |
|
104 | 139 | cast: _typedict[_CastFunc] |
105 | 140 | nbytes: _typedict[int] |
|
0 commit comments