diff options
author | Bas van Beek <b.f.van.beek@vu.nl> | 2021-02-24 00:49:52 +0100 |
---|---|---|
committer | Bas van Beek <b.f.van.beek@vu.nl> | 2021-04-20 14:37:22 +0200 |
commit | 83c420f9d0a81e546b61af36392c4cbddcd727a6 (patch) | |
tree | 14c18de547622053f5acea61a459c4854db44d58 /numpy | |
parent | 3e8b865690457abf8ec710e2fd867476432bea73 (diff) | |
download | numpy-83c420f9d0a81e546b61af36392c4cbddcd727a6.tar.gz |
ENH: Add annotations for 4 objects in `np.core.numerictypes`
* `cast`
* `nbytes`
* `ScalarType`
* `typecodes`
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/__init__.pyi | 12 | ||||
-rw-r--r-- | numpy/core/numerictypes.pyi | 56 |
2 files changed, 56 insertions, 12 deletions
diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 2fa5b3d41..08050a524 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -327,6 +327,10 @@ from numpy.core.numerictypes import ( issubdtype as issubdtype, sctype2char as sctype2char, find_common_type as find_common_type, + nbytes as nbytes, + cast as cast, + ScalarType as ScalarType, + typecodes as typecodes, ) from numpy.core.shape_base import ( @@ -504,14 +508,6 @@ class vectorize: def __call__(self, *args: Any, **kwargs: Any) -> Any: ... def __getattr__(self, key: str) -> Any: ... -# Placeholders for miscellaneous objects -# NOTE: `cast` and `nbytes` are in fact instances of a `dict` subclass that -# converts passed `DTypeLike` objects into the actual keys (`np.generic`) -ScalarType: Tuple[Type[Any], ...] -cast: Dict[DTypeLike, Callable[..., ndarray[Any, dtype[Any]]]] -nbytes: Dict[DTypeLike, int] -typecodes: Dict[str, str] - # Placeholders for Python-based functions def angle(z, deg=...): ... def append(arr, values, axis=...): ... diff --git a/numpy/core/numerictypes.pyi b/numpy/core/numerictypes.pyi index c8b4e1c65..1c9c84e95 100644 --- a/numpy/core/numerictypes.pyi +++ b/numpy/core/numerictypes.pyi @@ -1,9 +1,55 @@ -from typing import TypeVar, Optional, Type, Union, Tuple, Sequence, overload, Any +import sys +from typing import ( + TypeVar, + Optional, + Type, + Union, + Tuple, + Sequence, + overload, + Any, + TypeVar, + Dict, + List, +) from numpy import generic, ndarray, dtype -from numpy.typing import DTypeLike + +from numpy.core._type_aliases import ( + sctypeDict as sctypeDict, + sctypes as sctypes, +) + +from numpy.typing import DTypeLike, ArrayLike + +if sys.version_info >= (3, 8): + from typing import Literal, Protocol, TypedDict +else: + from typing_extensions import Literal, Protocol, TypedDict _T = TypeVar("_T") +_ScalarType = TypeVar("_ScalarType", bound=generic) + +class _CastFunc(Protocol): + def __call__( + self, x: ArrayLike, k: DTypeLike = ... + ) -> ndarray[Any, dtype[Any]]: ... + +class _TypeCodes(TypedDict): + Character: Literal['c'] + Integer: Literal['bhilqp'] + UnsignedInteger: Literal['BHILQP'] + Float: Literal['efdg'] + Complex: Literal['FDG'] + AllInteger: Literal['bBhHiIlLqQpP'] + AllFloat: Literal['efdgFDG'] + Datetime: Literal['Mm'] + All: Literal['?bhilqpBHILQPefdgFDGSUVOMm'] + +class _typedict(Dict[Type[generic], _T]): + def __getitem__(self, key: DTypeLike) -> _T: ... + +__all__: List[str] def maximum_sctype(t: DTypeLike) -> dtype: ... def issctype(rep: object) -> bool: ... @@ -25,5 +71,7 @@ def find_common_type( array_types: Sequence[DTypeLike], scalar_types: Sequence[DTypeLike] ) -> dtype: ... -# TODO: Add annotations for the following objects: -# nbytes, cast, ScalarType & typecodes +cast: _typedict[_CastFunc] +nbytes: _typedict[int] +typecodes: _TypeCodes +ScalarType: Tuple[type, ...] |