-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Can reset_index
return None?
#5533
Comments
Good spot @joooeey . I think you're right. Does changing it cause any mypy errors? |
Well, when I change the return type to "DataArray" in my local project the mypy error I had reported is gone. It also doesn't introduce any new errors. |
Great @joooeey |
maybe |
Both `set_index` and `reset_index` are wrappers to other methods that return `"DataArray"`, not `Optional["DataArray"]`. That is, they will never return None. That's why these methods should also have only `"DataArray"` in there return signature. This way it will be possible to do something like `myarray = myarray.reset_index(...)` without getting a complaint from Mypy. For extended discussion, see pydata#5533 (comment)
Both `set_index` and `reset_index` are wrappers to other methods that return `"DataArray"`, not `Optional["DataArray"]`. That is, they will never return None. That's why these methods should also have only `"DataArray"` in there return signature. This way it will be possible to do something like `myarray = myarray.reset_index(...)` without getting a complaint from Mypy. For extended discussion, see #5533 (comment)
What happened:
The line
was not accepted by mypy. Mypy gave the following error:
What you expected to happen:
I think that the return type of
DataArray.reset_index
should beDataArray
, notOptional[DataArray]
.Optional[Array]
also includesNone
but the source code doesn't return None anywhere as far as I can see. Specifically,reset_index
just redirects to_replace
which returns aDataArray
notOptional[DataArray]
according to its type annotation.Relevant code in
xarray.core.dataarray.py
:Question:
Do I need to worry that there is any input for
img
, whereimg.reset_index
would returnNone
?If I understand everything correctly and this is just a bug, I'm happy to put in a pull request to change the annotation.
Environment:
Output of xr.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.9.4 (default, Apr 9 2021, 16:34:09)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 5.4.0-73-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.0
libnetcdf: 4.7.4
xarray: 0.18.2
pandas: 1.2.4
numpy: 1.19.5
scipy: 1.6.1
netCDF4: 1.5.6
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: None
cftime: 1.5.0
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.2.4
cfgrib: None
iris: None
bottleneck: None
dask: installed
distributed: None
matplotlib: 3.4.2
cartopy: None
seaborn: None
numbagg: None
pint: None
setuptools: 52.0.0.post20210125
pip: 21.0.1
conda: None
pytest: 6.2.4
IPython: 7.24.1
sphinx: 4.0.2
The text was updated successfully, but these errors were encountered: