Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TYP: Better ndarray binop return types for float64 & ``complex128" #28112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2025
Merged
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
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies:
- hypothesis
# For type annotations
- typing_extensions>=4.2.0 # needed for python < 3.10
- mypy=1.13.0
- mypy=1.14.1
- orjson # makes mypy faster
# For building docs
- sphinx>=4.5.0
Expand Down
491 changes: 358 additions & 133 deletions numpy/__init__.pyi

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions numpy/_typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,14 @@
NDArray as NDArray,
ArrayLike as ArrayLike,
_ArrayLike as _ArrayLike,
_FiniteNestedSequence as _FiniteNestedSequence,
_SupportsArray as _SupportsArray,
_SupportsArrayFunc as _SupportsArrayFunc,
_ArrayLikeInt as _ArrayLikeInt,
_ArrayLikeBool_co as _ArrayLikeBool_co,
_ArrayLikeUInt_co as _ArrayLikeUInt_co,
_ArrayLikeInt_co as _ArrayLikeInt_co,
_ArrayLikeFloat_co as _ArrayLikeFloat_co,
_ArrayLikeFloat64_co as _ArrayLikeFloat64_co,
_ArrayLikeComplex_co as _ArrayLikeComplex_co,
_ArrayLikeComplex128_co as _ArrayLikeComplex128_co,
_ArrayLikeNumber_co as _ArrayLikeNumber_co,
_ArrayLikeTD64_co as _ArrayLikeTD64_co,
_ArrayLikeDT64_co as _ArrayLikeDT64_co,
Expand All @@ -140,6 +139,9 @@
_ArrayLikeString_co as _ArrayLikeString_co,
_ArrayLikeAnyString_co as _ArrayLikeAnyString_co,
_ArrayLikeUnknown as _ArrayLikeUnknown,
_FiniteNestedSequence as _FiniteNestedSequence,
_SupportsArray as _SupportsArray,
_SupportsArrayFunc as _SupportsArrayFunc,
_UnknownType as _UnknownType,
)

Expand Down
25 changes: 15 additions & 10 deletions numpy/_typing/_array_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
str_,
bytes_,
)
from ._nbit_base import _32Bit, _64Bit
from ._nested_sequence import _NestedSequence
from ._shape import _Shape

Expand Down Expand Up @@ -87,17 +88,16 @@ def __array_function__(
)

if sys.version_info >= (3, 12):
from collections.abc import Buffer

ArrayLike: TypeAlias = Buffer | _DualArrayLike[
dtype[Any],
bool | int | float | complex | str | bytes,
]
from collections.abc import Buffer as _Buffer
else:
ArrayLike: TypeAlias = _DualArrayLike[
dtype[Any],
bool | int | float | complex | str | bytes,
]
@runtime_checkable
class _Buffer(Protocol):
def __buffer__(self, flags: int, /) -> memoryview: ...

ArrayLike: TypeAlias = _Buffer | _DualArrayLike[
dtype[Any],
bool | int | float | complex | str | bytes,
]

# `ArrayLike<X>_co`: array-like objects that can be coerced into `X`
# given the casting rules `same_kind`
Expand Down Expand Up @@ -165,6 +165,11 @@ def __array_function__(
_ArrayLikeString_co
)

__Float64_co: TypeAlias = np.floating[_64Bit] | np.float32 | np.float16 | np.integer | np.bool
__Complex128_co: TypeAlias = np.number[_64Bit] | np.number[_32Bit] | np.float16 | np.integer | np.bool
_ArrayLikeFloat64_co: TypeAlias = _DualArrayLike[dtype[__Float64_co], float | int]
_ArrayLikeComplex128_co: TypeAlias = _DualArrayLike[dtype[__Complex128_co], complex | float | int]

# NOTE: This includes `builtins.bool`, but not `numpy.bool`.
_ArrayLikeInt: TypeAlias = _DualArrayLike[
dtype[integer[Any]],
Expand Down
Loading
Loading