diff --git a/src/numpy-stubs/__init__.pyi b/src/numpy-stubs/__init__.pyi index 78521987..d4af50a0 100644 --- a/src/numpy-stubs/__init__.pyi +++ b/src/numpy-stubs/__init__.pyi @@ -342,7 +342,6 @@ from ._typing import ( _ArrayLikeObject_co, _ArrayLikeTD64_co, _DTypeLike, - _DTypeLikeVoid, _NestedSequence, _NumberLike_co, _ScalarLike_co, @@ -5026,7 +5025,7 @@ class void(flexible[bytes | tuple[Any, ...]]): # type: ignore[misc] # pyright: @overload def __new__(cls, length_or_data: _nt.CoInteger_0d | bytes, /, dtype: None = None) -> Self: ... @overload - def __new__(cls, length_or_data: object, /, dtype: _DTypeLikeVoid) -> Self: ... + def __new__(cls, length_or_data: object, /, dtype: _nt.ToDTypeVoid) -> Self: ... # @type_check_only diff --git a/src/numpy-stubs/_core/einsumfunc.pyi b/src/numpy-stubs/_core/einsumfunc.pyi index fd27c45e..fc9ce33e 100644 --- a/src/numpy-stubs/_core/einsumfunc.pyi +++ b/src/numpy-stubs/_core/einsumfunc.pyi @@ -12,13 +12,6 @@ from numpy._typing import ( _ArrayLikeInt_co, _ArrayLikeObject_co, _ArrayLikeUInt_co, - _DTypeLikeBool, - _DTypeLikeComplex, - _DTypeLikeComplex_co, - _DTypeLikeFloat, - _DTypeLikeInt, - _DTypeLikeObject, - _DTypeLikeUInt, ) __all__ = ["einsum", "einsum_path"] @@ -30,6 +23,16 @@ _OptimizeKind: TypeAlias = bool | Literal["greedy", "optimal"] | Sequence[str | _CastingSafe: TypeAlias = Literal["no", "equiv", "safe", "same_kind", "same_value"] _CastingUnsafe: TypeAlias = Literal["unsafe"] +_ToDTypeBool: TypeAlias = _nt.ToDTypeBool +_ToDTypeUInt: TypeAlias = ( + _nt.ToDTypeUInt8 | _nt.ToDTypeUInt16 | _nt.ToDTypeUInt32 | _nt.ToDTypeUInt64 | _nt.ToDTypeULong +) +_ToDTypeInt: TypeAlias = _nt.ToDTypeInt8 | _nt.ToDTypeInt16 | _nt.ToDTypeInt32 | _nt.ToDTypeInt64 | _nt.ToDTypeLong +_ToDTypeFloat: TypeAlias = _nt.ToDTypeFloat16 | _nt.ToDTypeFloat32 | _nt.ToDTypeFloat64 | _nt.ToDTypeLongDouble +_ToDTypeComplex: TypeAlias = _nt.ToDTypeComplex64 | _nt.ToDTypeComplex128 | _nt.ToDTypeCLongDouble +_ToDTypeComplex_co: TypeAlias = _ToDTypeBool | _ToDTypeUInt | _ToDTypeInt | _ToDTypeFloat | _ToDTypeComplex +_ToDTypeObject: TypeAlias = _nt.ToDTypeObject + # TODO: Properly handle the `casting`-based combinatorics # TODO: We need to evaluate the content `__subscripts` in order # to identify whether or an array or scalar is returned. At a cursory @@ -43,7 +46,7 @@ def einsum( *operands: _ArrayLikeBool_co, out: None = None, optimize: _OptimizeKind = False, - dtype: _DTypeLikeBool | None = None, + dtype: _ToDTypeBool | None = None, order: _OrderKACF = "K", casting: _CastingSafe = "safe", ) -> Incomplete: ... @@ -53,7 +56,7 @@ def einsum( /, *operands: _ArrayLikeUInt_co, out: None = None, - dtype: _DTypeLikeUInt | None = None, + dtype: _ToDTypeUInt | None = None, order: _OrderKACF = "K", casting: _CastingSafe = "safe", optimize: _OptimizeKind = False, @@ -64,7 +67,7 @@ def einsum( /, *operands: _ArrayLikeInt_co, out: None = None, - dtype: _DTypeLikeInt | None = None, + dtype: _ToDTypeInt | None = None, order: _OrderKACF = "K", casting: _CastingSafe = "safe", optimize: _OptimizeKind = False, @@ -75,7 +78,7 @@ def einsum( /, *operands: _ArrayLikeFloat_co, out: None = None, - dtype: _DTypeLikeFloat | None = None, + dtype: _ToDTypeFloat | None = None, order: _OrderKACF = "K", casting: _CastingSafe = "safe", optimize: _OptimizeKind = False, @@ -86,7 +89,7 @@ def einsum( /, *operands: _ArrayLikeComplex_co, out: None = None, - dtype: _DTypeLikeComplex | None = None, + dtype: _ToDTypeComplex | None = None, order: _OrderKACF = "K", casting: _CastingSafe = "safe", optimize: _OptimizeKind = False, @@ -97,7 +100,7 @@ def einsum( /, *operands: Any, casting: _CastingUnsafe, - dtype: _DTypeLikeComplex_co | None = None, + dtype: _ToDTypeComplex_co | None = None, out: None = None, order: _OrderKACF = "K", optimize: _OptimizeKind = False, @@ -108,7 +111,7 @@ def einsum( /, *operands: _ArrayLikeComplex_co, out: _ArrayT, - dtype: _DTypeLikeComplex_co | None = None, + dtype: _ToDTypeComplex_co | None = None, order: _OrderKACF = "K", casting: _CastingSafe = "safe", optimize: _OptimizeKind = False, @@ -120,7 +123,7 @@ def einsum( *operands: Any, out: _ArrayT, casting: _CastingUnsafe, - dtype: _DTypeLikeComplex_co | None = None, + dtype: _ToDTypeComplex_co | None = None, order: _OrderKACF = "K", optimize: _OptimizeKind = False, ) -> _ArrayT: ... @@ -130,7 +133,7 @@ def einsum( /, *operands: _ArrayLikeObject_co, out: None = None, - dtype: _DTypeLikeObject | None = None, + dtype: _ToDTypeObject | None = None, order: _OrderKACF = "K", casting: _CastingSafe = "safe", optimize: _OptimizeKind = False, @@ -141,7 +144,7 @@ def einsum( /, *operands: Any, casting: _CastingUnsafe, - dtype: _DTypeLikeObject | None = None, + dtype: _ToDTypeObject | None = None, out: None = None, order: _OrderKACF = "K", optimize: _OptimizeKind = False, @@ -152,7 +155,7 @@ def einsum( /, *operands: _ArrayLikeObject_co, out: _ArrayT, - dtype: _DTypeLikeObject | None = None, + dtype: _ToDTypeObject | None = None, order: _OrderKACF = "K", casting: _CastingSafe = "safe", optimize: _OptimizeKind = False, @@ -164,7 +167,7 @@ def einsum( *operands: Any, out: _ArrayT, casting: _CastingUnsafe, - dtype: _DTypeLikeObject | None = None, + dtype: _ToDTypeObject | None = None, order: _OrderKACF = "K", optimize: _OptimizeKind = False, ) -> _ArrayT: ... @@ -175,7 +178,7 @@ def einsum( def einsum_path( subscripts: str | _ArrayLikeInt_co, /, - *operands: _ArrayLikeComplex_co | _DTypeLikeObject, + *operands: _ArrayLikeComplex_co | _ToDTypeObject, optimize: _OptimizeKind = "greedy", einsum_call: L[False] = False, ) -> tuple[list[str | tuple[int, ...]], str]: ... diff --git a/src/numpy-stubs/_core/umath.pyi b/src/numpy-stubs/_core/umath.pyi index c77b7221..d2605d46 100644 --- a/src/numpy-stubs/_core/umath.pyi +++ b/src/numpy-stubs/_core/umath.pyi @@ -18,12 +18,7 @@ from typing_extensions import TypeAliasType, TypeVar, Unpack import _numtype as _nt import numpy as np from numpy import _CastingKind, _OrderKACF # noqa: ICN003 -from numpy._typing import ( - _DTypeLike as _ToDType, - _DTypeLikeComplex as _ToDTypeComplex, - _DTypeLikeFloat as _ToDTypeFloat, - _ShapeLike, -) +from numpy._typing import _ShapeLike from . import _multiarray_umath as _multiarray_umath from ._multiarray_umath import ( @@ -192,6 +187,10 @@ _ToStringLike: TypeAlias = bytes | str | np.character _CoFloat: TypeAlias = float | _nt.co_float _CoComplex: TypeAlias = complex | _nt.co_complex +_ToDType = TypeAliasType("_ToDType", _nt._ToDType[_ScalarT], type_params=(_ScalarT,)) +_ToDTypeFloat: TypeAlias = _nt.ToDTypeFloat16 | _nt.ToDTypeFloat32 | _nt.ToDTypeFloat64 | _nt.ToDTypeLongDouble +_ToDTypeComplex: TypeAlias = _nt.ToDTypeComplex64 | _nt.ToDTypeComplex128 | _nt.ToDTypeCLongDouble + _ToDTypeInexact: TypeAlias = _ToDTypeFloat | _ToDTypeComplex _BoolND: TypeAlias = _nt.Array[np.bool_] diff --git a/src/numpy-stubs/_typing/__init__.pyi b/src/numpy-stubs/_typing/__init__.pyi index 92b12314..faebb2c9 100644 --- a/src/numpy-stubs/_typing/__init__.pyi +++ b/src/numpy-stubs/_typing/__init__.pyi @@ -79,18 +79,6 @@ from ._char_codes import ( from ._dtype_like import ( DTypeLike as DTypeLike, _DTypeLike as _DTypeLike, - _DTypeLikeBool as _DTypeLikeBool, - _DTypeLikeBytes as _DTypeLikeBytes, - _DTypeLikeComplex as _DTypeLikeComplex, - _DTypeLikeComplex_co as _DTypeLikeComplex_co, - _DTypeLikeDT64 as _DTypeLikeDT64, - _DTypeLikeFloat as _DTypeLikeFloat, - _DTypeLikeInt as _DTypeLikeInt, - _DTypeLikeObject as _DTypeLikeObject, - _DTypeLikeStr as _DTypeLikeStr, - _DTypeLikeTD64 as _DTypeLikeTD64, - _DTypeLikeUInt as _DTypeLikeUInt, - _DTypeLikeVoid as _DTypeLikeVoid, _SupportsDType as _SupportsDType, _VoidDTypeLike as _VoidDTypeLike, ) diff --git a/src/numpy-stubs/_typing/_dtype_like.pyi b/src/numpy-stubs/_typing/_dtype_like.pyi index dfd68a3f..595e407e 100644 --- a/src/numpy-stubs/_typing/_dtype_like.pyi +++ b/src/numpy-stubs/_typing/_dtype_like.pyi @@ -5,21 +5,6 @@ from typing_extensions import TypeVar import numpy as np -from ._char_codes import ( - _BoolCodes, - _BytesCodes, - _ComplexFloatingCodes, - _DT64Codes, - _FloatingCodes, - _NumberCodes, - _ObjectCodes, - _SignedIntegerCodes, - _StrCodes, - _TD64Codes, - _UnsignedIntegerCodes, - _VoidCodes, -) - _ScalarT = TypeVar("_ScalarT", bound=np.generic) _DTypeT = TypeVar("_DTypeT", bound=np.dtype) _DTypeT_co = TypeVar("_DTypeT_co", covariant=True, bound=np.dtype) @@ -56,26 +41,6 @@ _DTypeLike: TypeAlias = type[_ScalarT] | np.dtype[_ScalarT] | _SupportsDType[np. # Would create a dtype[np.void] _VoidDTypeLike: TypeAlias = tuple[Any, Any] | list[Any] | _DTypeDict -# TODO(jorenahm): remove these `_DTypeLike*` aliases in favor of `_numtype.ToDType*`: -# https://github.com/numpy/numtype/issues/566 -# ruff: noqa: PYI047 - -# Aliases for commonly used dtype-like objects. -# Note that the precision of `np.number` subclasses is ignored herein. -_DTypeLikeBool: TypeAlias = type[bool] | _DTypeLike[np.bool] | _BoolCodes -_DTypeLikeUInt: TypeAlias = _DTypeLike[np.unsignedinteger] | _UnsignedIntegerCodes -_DTypeLikeInt: TypeAlias = type[int] | _DTypeLike[np.signedinteger] | _SignedIntegerCodes -_DTypeLikeFloat: TypeAlias = type[float] | _DTypeLike[np.floating] | _FloatingCodes -_DTypeLikeComplex: TypeAlias = type[complex] | _DTypeLike[np.complexfloating] | _ComplexFloatingCodes -_DTypeLikeDT64: TypeAlias = _DTypeLike[np.timedelta64] | _TD64Codes -_DTypeLikeTD64: TypeAlias = _DTypeLike[np.datetime64] | _DT64Codes -_DTypeLikeStr: TypeAlias = type[str] | _DTypeLike[np.str_] | _StrCodes -_DTypeLikeBytes: TypeAlias = type[bytes] | _DTypeLike[np.bytes_] | _BytesCodes -_DTypeLikeVoid: TypeAlias = _DTypeLike[np.void] | _VoidCodes | _VoidDTypeLike -_DTypeLikeObject: TypeAlias = type | _DTypeLike[np.object_] | _ObjectCodes - -_DTypeLikeComplex_co: TypeAlias = type[complex] | _DTypeLike[np.number] | _NumberCodes | _BoolCodes - # 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 diff --git a/src/numpy-stubs/lib/recfunctions.pyi b/src/numpy-stubs/lib/recfunctions.pyi index c009886d..afd76f75 100644 --- a/src/numpy-stubs/lib/recfunctions.pyi +++ b/src/numpy-stubs/lib/recfunctions.pyi @@ -6,7 +6,7 @@ from typing_extensions import TypeVar import _numtype as _nt import numpy as np import numpy.typing as npt -from numpy._typing import _DTypeLike, _DTypeLikeVoid +from numpy._typing import _DTypeLike from numpy.ma.mrecords import MaskedRecords __all__ = [ @@ -274,7 +274,7 @@ def assign_fields_by_name(dst: _nt.Array[np.void], src: _nt.Array[np.void], zero # def require_fields( - array: _nt.Array[np.void, _ShapeT], required_dtype: _DTypeLikeVoid + array: _nt.Array[np.void, _ShapeT], required_dtype: _nt.ToDTypeVoid ) -> _nt.Array[np.void, _ShapeT]: ... # TODO(jorenham): Attempt shape-typing diff --git a/src/numpy-stubs/random/_generator.pyi b/src/numpy-stubs/random/_generator.pyi index 81d4df5f..b21e67ec 100644 --- a/src/numpy-stubs/random/_generator.pyi +++ b/src/numpy-stubs/random/_generator.pyi @@ -6,7 +6,7 @@ from typing_extensions import TypeVar, override import _numtype as _nt import numpy as np import numpy.typing as npt -from numpy._typing import _ArrayLike, _BoolCodes, _DTypeLike, _ShapeLike +from numpy._typing import _ArrayLike, _BoolCodes, _ShapeLike from numpy.random import BitGenerator, RandomState, SeedSequence ### @@ -15,7 +15,7 @@ _ScalarT = TypeVar("_ScalarT", bound=np.generic) _IntegerT = TypeVar("_IntegerT", bound=_nt.co_integer) _ShapeT = TypeVar("_ShapeT", bound=_nt.Shape) -_DTypeLikeFloat: TypeAlias = _nt.ToDTypeFloat32 | _nt.ToDTypeFloat64 +_ToDTypeFloat: TypeAlias = _nt.ToDTypeFloat32 | _nt.ToDTypeFloat64 _CoInt_nnd: TypeAlias = _nt._ToArray_nnd[_nt.co_integer] _CoFloat_nnd: TypeAlias = _nt._ToArray_nnd[_nt.co_float] @@ -139,7 +139,7 @@ class Generator: # @overload - def random(self, /, size: None = None, dtype: _DTypeLikeFloat = ..., out: None = None) -> float: ... + def random(self, /, size: None = None, dtype: _ToDTypeFloat = ..., out: None = None) -> float: ... @overload def random( self, /, size: _ShapeLike | None = None, dtype: _nt.ToDTypeFloat64 = ..., *, out: _nt.Array[np.float64, _ShapeT] @@ -233,7 +233,7 @@ class Generator: # @overload def standard_exponential( - self, /, size: None = None, dtype: _DTypeLikeFloat = ..., method: _ExpMethod = "zig", out: None = None + self, /, size: None = None, dtype: _ToDTypeFloat = ..., method: _ExpMethod = "zig", out: None = None ) -> float: ... @overload def standard_exponential( @@ -448,7 +448,7 @@ class Generator: # @overload - def standard_normal(self, /, size: None = None, dtype: _DTypeLikeFloat = ..., out: None = None) -> float: ... + def standard_normal(self, /, size: None = None, dtype: _ToDTypeFloat = ..., out: None = None) -> float: ... @overload def standard_normal( self, /, size: _ShapeLike | None = None, dtype: _nt.ToDTypeFloat64 = ..., *, out: _nt.Array[np.float64] @@ -575,7 +575,7 @@ class Generator: ) -> float | _nt.Array[np.float32]: ... @overload def standard_gamma( - self, /, shape: _nt.CoFloating_0d, size: None = None, dtype: _DTypeLikeFloat = ..., out: None = None + self, /, shape: _nt.CoFloating_0d, size: None = None, dtype: _ToDTypeFloat = ..., out: None = None ) -> float: ... @overload def standard_gamma( @@ -737,7 +737,7 @@ class Generator: low: int, high: int | None = None, size: None = None, - dtype: _DTypeLike[np.int64] = ..., + dtype: _nt._ToDType[np.int64] = ..., endpoint: bool = False, ) -> np.int64: ... @overload @@ -752,7 +752,7 @@ class Generator: high: int | None = None, size: None = None, *, - dtype: _DTypeLike[_IntegerT], + dtype: _nt._ToDType[_IntegerT], endpoint: bool = False, ) -> _IntegerT: ... @overload @@ -847,7 +847,7 @@ class Generator: low: _nt.CoInteger_nd, high: _nt.CoInteger_nd, size: _ShapeLike, - dtype: _DTypeLike[np.int64] = ..., + dtype: _nt._ToDType[np.int64] = ..., endpoint: bool = False, ) -> _nt.Array[np.int64]: ... @overload @@ -857,7 +857,7 @@ class Generator: low: _nt.CoInteger_nd, high: _nt.CoInteger_nd, size: _ShapeLike, - dtype: _DTypeLike[_IntegerT], + dtype: _nt._ToDType[_IntegerT], endpoint: bool = False, ) -> _nt.Array[_IntegerT]: ... @overload @@ -958,7 +958,7 @@ class Generator: high: _nt.CoInteger_nd | None = None, *, size: _ShapeLike, - dtype: _DTypeLike[np.int64] = ..., + dtype: _nt._ToDType[np.int64] = ..., endpoint: bool = False, ) -> _nt.Array[np.int64]: ... @overload @@ -969,7 +969,7 @@ class Generator: high: _nt.CoInteger_nd | None = None, *, size: _ShapeLike, - dtype: _DTypeLike[_IntegerT], + dtype: _nt._ToDType[_IntegerT], endpoint: bool = False, ) -> _nt.Array[_IntegerT]: ... @overload @@ -1090,7 +1090,7 @@ class Generator: high: _nt.CoInteger_nd | None = None, size: _ShapeLike | None = None, *, - dtype: _DTypeLike[_IntegerT], + dtype: _nt._ToDType[_IntegerT], endpoint: bool = False, ) -> _IntegerT | _nt.Array[_IntegerT]: ... @overload diff --git a/src/numpy-stubs/random/bit_generator.pyi b/src/numpy-stubs/random/bit_generator.pyi index 3c3009cc..cbc1847f 100644 --- a/src/numpy-stubs/random/bit_generator.pyi +++ b/src/numpy-stubs/random/bit_generator.pyi @@ -18,7 +18,7 @@ from typing_extensions import CapsuleType, TypeVar, override import _numtype as _nt import numpy as np -from numpy._typing import _ArrayLikeInt_co, _DTypeLike, _ShapeLike, _UInt32Codes, _UInt64Codes +from numpy._typing import _ArrayLikeInt_co, _ShapeLike __all__ = ["BitGenerator", "SeedSequence"] @@ -26,8 +26,8 @@ __all__ = ["BitGenerator", "SeedSequence"] _StateT = TypeVar("_StateT", bound=Mapping[str, object], default=Mapping[str, Any]) -_ToDTypeUInt32: TypeAlias = _DTypeLike[np.uint32] | _UInt32Codes -_ToDTypeUInt64: TypeAlias = _DTypeLike[np.uint64] | _UInt64Codes +_ToDTypeUInt32: TypeAlias = _nt.ToDTypeUInt32 +_ToDTypeUInt64: TypeAlias = _nt.ToDTypeUInt64 ### diff --git a/src/numpy-stubs/random/mtrand.pyi b/src/numpy-stubs/random/mtrand.pyi index be3897b9..dcf1b535 100644 --- a/src/numpy-stubs/random/mtrand.pyi +++ b/src/numpy-stubs/random/mtrand.pyi @@ -6,7 +6,7 @@ from typing_extensions import TypeVar, override import _numtype as _nt import numpy as np import numpy.typing as npt -from numpy._typing import _ArrayLike, _BoolCodes, _DTypeLike, _ShapeLike +from numpy._typing import _ArrayLike, _BoolCodes, _ShapeLike from numpy.random.bit_generator import BitGenerator __all__ = [ @@ -481,7 +481,7 @@ class RandomState: def randint(self, /, low: int, high: int | None = None, size: None = None, *, dtype: type[_nt.JustInt]) -> int: ... @overload def randint( # type: ignore[overload-overlap] - self, /, low: int, high: int | None = None, size: None = None, *, dtype: _DTypeLike[_IntegerT] + self, /, low: int, high: int | None = None, size: None = None, *, dtype: _nt._ToDType[_IntegerT] ) -> _IntegerT: ... @overload def randint(self, /, low: int, high: int | None = None, size: None = None, *, dtype: _BoolCodes) -> np.bool: ... @@ -525,7 +525,7 @@ class RandomState: ) -> _nt.Array[np.bool]: ... @overload def randint( # type: ignore[overload-overlap] - self, /, low: _nt.CoInteger_nd, high: _nt.CoInteger_nd, size: _ShapeLike, dtype: _DTypeLike[_IntegerT] + self, /, low: _nt.CoInteger_nd, high: _nt.CoInteger_nd, size: _ShapeLike, dtype: _nt._ToDType[_IntegerT] ) -> _nt.Array[_IntegerT]: ... @overload def randint( @@ -581,7 +581,7 @@ class RandomState: high: _nt.CoInteger_nd | None = None, *, size: _ShapeLike, - dtype: _DTypeLike[_IntegerT], + dtype: _nt._ToDType[_IntegerT], ) -> _nt.Array[_IntegerT]: ... @overload def randint( @@ -685,7 +685,7 @@ class RandomState: high: _nt.CoInteger_nd | None = None, size: _ShapeLike | None = None, *, - dtype: _DTypeLike[_IntegerT], + dtype: _nt._ToDType[_IntegerT], ) -> _IntegerT | _nt.Array[_IntegerT]: ... @overload def randint(