pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.64k stars 1.09k forks source link

DataArray.get_axis_num is incorrectly typed #9822

Closed bmerry closed 3 days ago

bmerry commented 4 days ago

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

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
welcome[bot] commented 4 days ago

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!