Skip to content
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

DataArray.get_axis_num is incorrectly typed #9822

Closed
4 of 5 tasks
bmerry opened this issue Nov 25, 2024 · 1 comment · Fixed by #9827
Closed
4 of 5 tasks

DataArray.get_axis_num is incorrectly typed #9822

bmerry opened this issue Nov 25, 2024 · 1 comment · Fixed by #9827

Comments

@bmerry
Copy link
Contributor

bmerry commented Nov 25, 2024

What happened?

When using mypy to check my code, I'm getting a spurious error. When passing a single string to get_axis_num, the return type is determined to be tuple[int, ...] instead of int.

What did you expect to happen?

I expected the return type to be int when passing a single single.

Minimal Complete Verifiable Example

# Note: to be run through mypy
import xarray as xr

def foo(x: xr.DataArray) -> None:
    reveal_type(x.get_axis_num("time"))

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

# Output from mypy
test.py:4: note: Revealed type is "builtins.tuple[builtins.int, ...]"
Success: no issues found in 1 source file

Anything else we need to know?

From a quick look at the code, I'm guessing this is because str is iterable and so is matching the first @overload.

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.3 (main, Nov 6 2024, 18:32:19) [GCC 13.2.0]
python-bits: 64
OS: Linux
OS-release: 6.8.0-49-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: ('C', 'UTF-8')
libhdf5: 1.14.2
libnetcdf: None

xarray: 2024.11.0
pandas: 2.1.4
numpy: 1.26.2
scipy: 1.11.4
netCDF4: None
pydap: None
h5netcdf: None
h5py: 3.11.0
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: 2023.12.0
distributed: None
matplotlib: 3.9.1
cartopy: None
seaborn: None
numbagg: None
fsspec: 2023.12.1
cupy: 13.2.0
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 75.1.0
pip: 24.3.1
conda: None
pytest: 8.2.2
mypy: 1.11.1
IPython: 8.18.1
sphinx: 7.2.6

@bmerry bmerry added bug needs triage Issue that has not been reviewed by xarray team member labels Nov 25, 2024
Copy link

welcome bot commented Nov 25, 2024

Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!

@headtr1ck headtr1ck added topic-typing and removed needs triage Issue that has not been reviewed by xarray team member labels Nov 25, 2024
@bmerry bmerry changed the title DataArray.get_axis_num is incorrectly type DataArray.get_axis_num is incorrectly typed Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants