-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Type checking fails for multiplication #4054
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
We're currently working on a library largely based on xarray and have seen the same types of errors from mypy (PR in our project that is currently trying to integrate mypy: Ouranosinc/xclim#532). Currently working off of xarray v0.16. I also want to note this error is raised for other operations as well ( |
Yes, this looks like incomplete typing. We would take a PR for these for sure. |
Yeah there's no type info in |
Just dicsovered that the same things is true for |
Given these are generated dynamically, is there a clever way to define types for these in Python? Or we have to write out all the methods? https://github.com/pydata/xarray/blob/v0.16.0/xarray/core/ops.py#L312-L316 |
I have implemented a possible solution to have typing for all unary and binary operators which works for mypy and also displays correctly inferred type hints on hovering over the code in Pylance (with VS Code) and PyCharm. The approach involves generating a stub file using a simple helper script. I'll open a draft PR so you can see this approach and for soliciting feedback. I have been experimenting with all kinds of other approaches to solve this issue but this was the only one with a satisfactory result. For reference, and jotting down from memory, I think I tried the following approaches without much success:
|
MCVE Code Sample
I save the following in a file called "foo.py"
and run
Expected Output
This should give
Problem Description
Instead I get the output
It gives a similar error for addition.
If I reverse the order of multiplication it works as expected, namely
gives
But it would be nice if it also worked when the float is first, so I don't have to change a lot existing code to get typechecking to work.
Versions
I tested on version 0.15.1 and on the current master
Output of xr.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.8.2 (default, May 7 2020, 20:00:49)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 5.4.0-29-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: None
libnetcdf: None
xarray: 0.15.2.dev61+gbd84186a
pandas: 1.0.3
numpy: 1.18.4
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
pint: None
setuptools: 46.2.0.post20200511
pip: 20.0.2
conda: None
pytest: None
IPython: None
sphinx: None
The text was updated successfully, but these errors were encountered: