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

mypy 1.10.0 fails with matplotlib wrappers #9155

Closed
singledoggy opened this issue Jun 23, 2024 · 10 comments
Closed

mypy 1.10.0 fails with matplotlib wrappers #9155

singledoggy opened this issue Jun 23, 2024 · 10 comments

Comments

@singledoggy
Copy link

What is your issue?

Pyright is a full-featured, standards-based static type checker for Python, recently I tried it with xarray and get an error.
And mypy generates the same error.
So I wonder if we can fix this error, although this error won't have any impacts except for static type checker errors.

I have been using xarray for several years and recently began using Pyright. However, I encountered the following error: Argument missing for parameter 'self' Although my code still executes normally, I am curious about the cause of this error. Is this behavior expected?

import xarray as xr
import matplotlib.pyplot as plt
data = xr.DataArray([1, 2, 3], dims=['x'])
fig, ax= plt.subplots(ncols=1, nrows=1)
data.plot(ax=ax)

It seems to related to the codes here:

    plot = utils.UncachedAccessor(DataArrayPlotAccessor)

class UncachedAccessor(Generic[_Accessor]):
    """Acts like a property, but on both classes and class instances

    This class is necessary because some tools (e.g. pydoc and sphinx)
    inspect classes for which property returns itself and not the
    accessor.
    """

    def __init__(self, accessor: type[_Accessor]) -> None:
        self._accessor = accessor

    @overload
    def __get__(self, obj: None, cls) -> type[_Accessor]: ...

    @overload
    def __get__(self, obj: object, cls) -> _Accessor: ...

    def __get__(self, obj: None | object, cls) -> type[_Accessor] | _Accessor:
        if obj is None:
            return self._accessor

        return self._accessor(obj)  # type: ignore  # assume it is a valid accessor!

Here is my xarray version:

xarray                    2023.2.0           pyhd8ed1ab_0    conda-forge

Originally posted by @singledoggy in #9150

Pyright is behaving correctly here. Not surprisingly, mypy generates the same error.

The problem appears to be in the xarray library here:

class DataArrayPlotAccessor:
    ...
    @functools.wraps(dataarray_plot.plot, assigned=("__doc__", "__annotations__"))
    def __call__(self, **kwargs) -> Any:
        return dataarray_plot.plot(self._da, **kwargs)

Originally posted by @erictraut in microsoft/pyright#8203 (comment)

@singledoggy singledoggy added the needs triage Issue that has not been reviewed by xarray team member label Jun 23, 2024
Copy link

welcome bot commented Jun 23, 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!

@max-sixty
Copy link
Collaborator

Is the error still present on a recent version of xarray?

@max-sixty max-sixty added plan to close May be closeable, needs more eyeballs and removed needs triage Issue that has not been reviewed by xarray team member labels Jun 23, 2024
@singledoggy
Copy link
Author

Before, I only read through the latest code and didn't find any relevant changes. I just had time to test the latest version, but the issue still persists.

@max-sixty
Copy link
Collaborator

max-sixty commented Jun 24, 2024

OK. Mypy does pass — so if you find a mypy error in the current version then that's news, we'd like to understand that.

We do have a pyright job in CI, but it doesn't pass and so the check is allowed to fail. We're definitely open to contributions that improve it, and probably we'd be happy to turn on the check if we could get everything to pass.

Though it's not feasible to have an open issue for every pyright error so we can close this unless there's more info; hope that's reasonable.

@singledoggy
Copy link
Author

mypy also get this error

As @erictraut points out here, mypy wil generate the same error.
I also tested it with command: mypy test.py with the latest version (v2024.06.0) by myself.

It's reasonable if you choose to close this issue.

import xarray as xr
import matplotlib.pyplot as plt

data = xr.DataArray([1, 2, 3], dims=["x"])
fig, ax = plt.subplots(ncols=1, nrows=1)
data.plot(ax=ax)

test.py:6: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]
Found 1 error in 1 file (checked 1 source file)

Version information

INSTALLED VERSIONS
------------------
commit: None
python: 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:39:40) [Clang 15.0.7 ]
python-bits: 64
OS: Darwin
OS-release: 22.3.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.12.2
libnetcdf: 4.9.1

xarray: 2024.6.0
pandas: 2.2.2
numpy: 1.25.2
scipy: 1.11.2
netCDF4: 1.6.3
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: 1.6.2
nc_time_axis: None
iris: None
bottleneck: None
dask: 2023.8.1
distributed: 2023.8.1
matplotlib: 3.8.4
cartopy: 0.23.0
seaborn: 0.13.2
numbagg: None
fsspec: 2023.6.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 68.1.2
pip: 23.2.1
conda: None
pytest: None
mypy: 1.10.0
IPython: 8.14.0

max-sixty added a commit to max-sixty/xarray that referenced this issue Jun 24, 2024
I can't get this to fail locally, so adding a test to assess what's going on.

Alos excludes matplotlib from type exclusions
@max-sixty
Copy link
Collaborator

Thanks for the example and mypy output.

I see now — it's that our mypy version in CI is a bit out of date, and it does indeed raise an error with the newer mypy 1.10.0. Thanks for helping debug it.

I'll adjust the title of the issue if that's OK

@max-sixty max-sixty changed the title pyright error Argument missing for parameter "self" when plot mypy 1.10.0 fails with matplotlib wrappers Jun 24, 2024
@max-sixty
Copy link
Collaborator

max-sixty commented Jun 24, 2024

I (edit: can't) immediately see what's going on, so I'll have to leave it for the moment. (If anyone wants to dive in, that would be great).

Thanks for finding it @singledoggy

max-sixty added a commit that referenced this issue Jun 24, 2024
* Add test for #9155

I can't get this to fail locally, so adding a test to assess what's going on.

Alos excludes matplotlib from type exclusions

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@headtr1ck
Copy link
Collaborator

Just ran into the same problem. Also when running mypy on xarray codebase (-> 70 errors)

@headtr1ck headtr1ck added topic-typing and removed plan to close May be closeable, needs more eyeballs labels Jun 24, 2024
@headtr1ck
Copy link
Collaborator

Looks like python/mypy#17166

dcherian added a commit to dcherian/xarray that referenced this issue Jun 28, 2024
* main:
  promote floating-point numeric datetimes to 64-bit before decoding (pydata#9182)
  also pin `numpy` in the all-but-dask CI (pydata#9184)
  temporarily remove `pydap` from CI (pydata#9183)
  temporarily pin `numpy<2` (pydata#9181)
  Change np.core.defchararray to np.char (pydata#9165) (pydata#9166)
  Fix example code formatting for CachingFileManager (pydata#9178)
  Slightly improve DataTree repr (pydata#9064)
  switch to unit `"D"` (pydata#9170)
  Docs: Add page with figure for navigating help resources (pydata#9147)
  Add test for pydata#9155 (pydata#9161)
  Remove mypy exclusions for a couple more libraries (pydata#9160)
  Include numbagg in type checks (pydata#9159)
  Improve zarr chunks docs (pydata#9140)
dcherian added a commit that referenced this issue Jul 24, 2024
* main: (48 commits)
  Add test for #9155 (#9161)
  Remove mypy exclusions for a couple more libraries (#9160)
  Include numbagg in type checks (#9159)
  Improve zarr chunks docs (#9140)
  groupby: remove some internal use of IndexVariable (#9123)
  Improve `to_zarr` docs (#9139)
  Split out distributed writes in zarr docs (#9132)
  Update zendoo badge link (#9133)
  Support duplicate dimensions in `.chunk` (#9099)
  Bump the actions group with 2 updates (#9130)
  adjust repr tests to account for different platforms (#9127) (#9128)
  Grouper refactor (#9122)
  Update docstring in api.py for open_mfdataset(), clarifying "chunks" argument (#9121)
  Add test for rechunking to a size string (#9117)
  Move Sphinx directives out of `See also` (#8466)
  new whats-new section (#9115)
  release v2024.06.0 (#9113)
  release notes for 2024.06.0 (#9092)
  [skip-ci] Try fixing hypothesis CI trigger (#9112)
  Undo custom padding-top. (#9107)
  ...
@max-sixty
Copy link
Collaborator

I think this is mostly covered now — we have ignores, we can wait for the mypy upstream issue before removing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants