Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/numpy-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ from ._typing import (
_ArrayLikeObject_co,
_ArrayLikeTD64_co,
_DTypeLike,
_DTypeLikeVoid,
_NestedSequence,
_NumberLike_co,
_ScalarLike_co,
Expand Down Expand Up @@ -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
Expand Down
43 changes: 23 additions & 20 deletions src/numpy-stubs/_core/einsumfunc.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
Expand All @@ -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: ...
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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: ...
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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: ...
Expand All @@ -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]: ...
11 changes: 5 additions & 6 deletions src/numpy-stubs/_core/umath.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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_]
Expand Down
12 changes: 0 additions & 12 deletions src/numpy-stubs/_typing/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
35 changes: 0 additions & 35 deletions src/numpy-stubs/_typing/_dtype_like.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/numpy-stubs/lib/recfunctions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand Down Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions src/numpy-stubs/random/_generator.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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

###
Expand All @@ -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]

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading