-
Notifications
You must be signed in to change notification settings - Fork 23
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
_ArrayLike[Any] vs. ndarray[Any] #14
Comments
It seems to me that many methods are defined in the _ArrayLike class and return _ArrayLike but the actual numpy methods are only defined in numpy.ndarray and always return an ndarray. Maybe they should move to class ndarray to remove any type confusion. E.g. slicing an ndarray should also create an ndarray and not a variable of type _ArrayLike. |
Agreed. |
@kjyv can you show a small sample snippet that shows this? I usually try to use abstractions in annotations instead of concrete types (it's what you usually mean in python, given duck typing), although they were some tricky things around numpy semantic that can make this general advice wrong. |
We're often a sometimes a little sloppy on terminology, but it can be useful to distinguish between "array likes" and "duck arrays" (I think that's the source of our confusion here).
We definitely want at least support for NumPy Supposing that
where In practice, this gets pretty complex and I'm not sure it's worth the trouble. If someone is using type checking, they probably would be happy with slightly stricter functions, even if they aren't defined on everything. So the later signature might be enough for now. |
@dmoisset
I wonder now why I didn't try to set np._ArrayLike as expected input type since that covers both cases. The Readme does not give that hint, but maybe the correct answer is to simply use that type. |
It seems a bit unclear to me how to properly annotate functions that expect an ndarray. Many ndarray functions (e.g. flatten) return _ArrayLike which is then not recognized to be an ndarray. I guess I can't and shouldn't use _ArrayLike in my own annotations. Is this currently not supported properly by mypy or do I have to do this differently?
Btw., the numpy docs actually specify the return type of e.g. flatten to be ndarray, not "array_like".
The text was updated successfully, but these errors were encountered: