-
-
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
Improving typing of xr.Dataset.__getitem__
#4125
Comments
Here is an examle of this: xarray/xarray/core/weighted.py Lines 71 to 80 in c07160d
the |
Edit: NVM @mathause's example is in the code already. |
* Improve typehints of xr.Dataset.__getitem__ Resolves #4125 * Add overload for Mapping behavior Sadly this is not working with my version of mypy. See python/mypy#7328 * Overload only Hashable inputs Given mypy's use of overloads, I think this is all we can do. If the argument is not Hashable, then return the Union type as before. * Lint * Quote the DataArray to avoid error in py3.6 * Code review Co-authored-by: crusaderky <crusaderky@gmail.com>
First, I'd like the thank the xarray dev's for adding type hints to this library, not many libraries have this feature!
That said, the indexing notation of
xr.Dataset
does not currently play well wit mypy since it returns a Union type. This results in a lot of mypy errors like this:MCVE Code Sample
Expected Output
Mypy should be able to infer that
ds[['a', b']]
is a Dataset, and thatds['a']
is a DataArray.Problem Description
This requires any routine with type hints that consume an output of
xr.Dataset.__getitem__
to require aUnion[DataArray, Dataset]
even if it really intends to be used with eitherDataArray
orDataArray
. Becauseds[something]
is a ubiquitous syntax, this behavior accounts for approximately 50% of mypy errors in my xarray heavy code.Versions
Output of xr.show_versions()
In [1]: import xarray as xr
xr.
In [2]: xr.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.7.7 (default, May 7 2020, 21:25:33)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 5.3.0-1020-gcp
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: en_US.UTF-8
libhdf5: 1.10.4
libnetcdf: 4.7.3
xarray: 0.15.1
pandas: 1.0.1
numpy: 1.18.1
scipy: 1.4.1
netCDF4: 1.5.3
pydap: None
h5netcdf: 0.8.0
h5py: 2.10.0
Nio: None
zarr: 2.4.0
cftime: 1.1.2
nc_time_axis: 1.2.0
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.17.2
distributed: 2.17.0
matplotlib: 3.1.3
cartopy: 0.17.0
seaborn: 0.10.1
numbagg: None
setuptools: 46.4.0.post20200518
pip: 20.0.2
conda: 4.8.3
pytest: 5.4.2
IPython: 7.13.0
sphinx: None
Potential solution
I think we can fix this with typing.overload. I am not too familiar with that librariy, but I think something like the following might work:
The text was updated successfully, but these errors were encountered: