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
ndarray.astype
typing ignores the passed dtype
#19716
Comments
Hey, I would like to solve this issue. can you guide me |
ndarray.astype
ndarray.astype
typing ignores the passed dtype
Ah yes, the issue here is that prior to #19140 incorrect typevar usage would cause the passed dtype to be ignored.
True, there is some information about it in the As for the parameters: I'd strongly recommend keeping the shape-type as |
Interesting, thanks. Ideally I would want to be able to specify something like:
I'm sure this was discussed at great length already, but are these use cases covered in the proposed syntax under PEP 646? I would love to read more about the future roadmap for this, and hopefully participate in some constructive fashion. I also know that there are problems with statically type-checking exact array dimensions - I've been implementing a Numpy-like interface in Idris 2 for example, and I quickly ran into situations where I not only needed to use the dependent type features, but also ended up needing some non-trivial goal rewriting, which seems way beyond the scope of what you can do in a Mypy plugin. With fixed-size arrays at the type level, even something as mundane as |
So PEP 646, the introduction of variadic generics, should allow us to implement a shape-typing system. I'd recommend taking a look at the Summary Examples section, as it contains a few examples in the context of array-shape-typing (including both dimensionality as well as exact shapes).
Right, there is currently an issue on the implementation of dtype-typing-support (#19252). In short, there are four general categories of dtypes that we'll have to be able to parse:
|
Closed by #19745. |
Thanks again @BvB93. So if I wanted to support "any float dtype" I should do something like this? from typing import TypeVar
import numpy as np
from numpy.typing import NDArray
FloatArray = TypeVar('FloatArray', NDArray[np.floating])
def f(x: FloatArray, y: FloatArray) -> FloatArray:
return x + y I got |
- FloatArray = TypeVar('FloatArray', NDArray[np.floating])
+ FloatArray = TypeVar('FloatArray', bound=NDArray[np.floating]) Yup, besides the missing |
Mypy is not aware that changing the
dtype
of an array changes its annotated type, even if thedtype
is passed as a literalnp.dtype[np.float64]
,np.dtype[np.float32]
, etc.I imagine that this kind of type inference could be very difficult in general, but it would be nice if at least this special case worked.
Also - the parameters of
np.ndarray
don't appear to be documented anywhere. I kind of just guessed that it was something liketuple[int]
, where the length of the tuple represented the shape, but honestly I have no idea if this is right. It would be great if this was stated in the docs somewhere!Reproducing code example:
Error message:
Mypy output:
NumPy/Python version information:
mypy.ini
:The text was updated successfully, but these errors were encountered: