-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
MAINT: Add aliases for commonly used ArrayLike
objects
#18050
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
Conversation
# A union representing array-like objects; consists of two typevars: | ||
# One representing types that can be parametrized w.r.t. `np.dtype` | ||
# and another one for the rest | ||
_ArrayLike = Union[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we'd only need a single typevar here so that we can just use, e.g., _ArrayLike[np.float64]
.
Unfortunately this would require us to somehow express a np.generic
via its' builtin counterpart (e.g. expressing float
as SuperFancyProtocol[np.float64]
), which is simply not possible as of the moment. Not without some plugin magic at least.
How can it be determined that some array_like is, say, complex? I am probably not understanding how these will be used. |
Right, so there is the pre-existing |
How is it determined if something can be coerced to complex? Is that something declared by a user? |
This is based on the |
That works for scalars, but how does it work for a deeply nested list without examining every element? I guess my question is, how can that be determined except at runtime? |
Note that I'm perfectly happy to merge this, I just don't understand how it works :) |
If this list is created by a function, it would its responsibility to ensure that its type and nesting level are annotated correctly. On the other hand, If the list is defined as is then type checkers can already infer these things. from typing import TYPE_CHECKING
if TYPE_CHECKING:
# Revealed type is 'builtins.list[builtins.list*[builtins.list*[builtins.list*[builtins.list*[builtins.float*]]]]]'
reveal_type([[[[[0.0]]]]]) As a side note, with mypy lacking support for proper recursive objects we are limited to how deep we can go down the recursion rabbit hole. _ScalarType = TypeVar("_ScalarType")
# Plan A: nesting level <= 4 (can potentially be made larger or smaller)
_NestedSequence = Union[
_ScalarType,
Sequence[_ScalarType],
Sequence[Sequence[_ScalarType]],
Sequence[Sequence[Sequence[_ScalarType]]],
Sequence[Sequence[Sequence[Sequence[_ScalarType]]]],
]
# Plan B: nesting level > 4 (can't infer the scalar type here)
_RecursiveSequence = Sequence[Sequence[Sequence[Sequence[Sequence[Any]]]]] |
Thanks Bas. |
This PR adds a number of
_ArrayLike<x>
aliases,each representing all array-like objects that can be coerced into
x
(assumingcasting="same_kind"
).The herein introduced aliases will be necessary for handling
dtype
-specific overloads of array-likes.Examples
A simplified mockup example of
np.exp
(pretending for a second that it's an ordinary python function):