-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Order of generic types for ndarray #16547
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
Comments
I think My reason for preferring that the shape specification come before the dtype specification is that I anticipate that we'll probably want to have type aliases. Putting the Edit: based on my comment below, I'm reconsidering this position. |
Perhaps we should quickly survey other type systems for multidimensional arrays. If there's a clear majority for one formulation or the other, that might better indicate which scheme is most natural. |
Another option - allow product types to be created using
|
@eric-wieser That looks like the |
Thanks for the reference. There's a comment about |
One (admittedly rather weak!) argument for putting dtype before shape is the precedent of Rust, whose array declaration syntax is |
@wkschwartz: C++ sets the same precedent, |
I have a slight tendency towards putting the dtype first as well, but no big argument to back that up. |
Personally I'm leaning more towards |
With PEP 604, there's now precedent. Here's another proposal in the same space: why not something like this? dtype_and_shape: np.Array[np.int_ & (2, 2)] |
The problem here is that static type checkers will be unable to understand the I do believe that work is currently being done on an |
Do we expect static type checkers to be able to do anything with |
Not yet, though this is an area which is actively being worked on (search for "tensor typing" on typing-sig). The closest thing to a concrete shape type is in the Variadic generics PEP draft, which presents a Long story short, it would allow for a syntaxes similar to
For the time being we'll have to settle for a |
This is exciting! For me the fact that non-typing syntax is always (?) |
I feel the same, dtype first because is more descriptive. There are huge amount of possible shapes while not so many dtypes |
All the Array construction functions are usually of the signature |
Ok, the PR is up: #17719. The signature is currently set to |
Per python/typing#516, it seems likely that we will have a choice between two ways to write type hints:
ndarray[Dtype, Shape]
ndarray[Shape, Dtype]
We'll probably have two generic arguments, but will have the freedom to choose in which order they appear. Do we have any reason to prefer one order over the other?
One weak argument in favor of
[Shape, Dtype]
is that it matchesndarray.__new__
(but that's so rarely used that it probably doesn't matter).The text was updated successfully, but these errors were encountered: