issues: 631940742
This data as json
id | node_id | number | title | user | state | locked | assignee | milestone | comments | created_at | updated_at | closed_at | author_association | active_lock_reason | draft | pull_request | body | reactions | performed_via_github_app | state_reason | repo | type |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
631940742 | MDU6SXNzdWU2MzE5NDA3NDI= | 4125 | Improving typing of `xr.Dataset.__getitem__` | 1386642 | closed | 0 | 2 | 2020-06-05T20:40:39Z | 2020-06-15T11:25:53Z | 2020-06-15T11:25:53Z | CONTRIBUTOR | 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 MCVE Code Sample``` def func(ds: xr.Dataset): pass dataset: xr.Dataset = ... error:this line will give type error because mypy doesn't knowif ds[['a', 'b]] is Dataset or a DataArrayfunc(ds[['a', 'b']]) ``` Expected OutputMypy should be able to infer that Problem DescriptionThis requires any routine with type hints that consume an output of VersionsOutput of <tt>xr.show_versions()</tt>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: NonePotential solutionI 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: ``` from typing import overload class Dataset @overload def getitem(self, key: Hashable) -> DataArray: ...
``` |
{ "url": "https://api.github.com/repos/pydata/xarray/issues/4125/reactions", "total_count": 0, "+1": 0, "-1": 0, "laugh": 0, "hooray": 0, "confused": 0, "heart": 0, "rocket": 0, "eyes": 0 } |
completed | 13221727 | issue |