Skip to content

mypy 1.10.0 fails with matplotlib wrappers #9155

Closed
@singledoggy

Description

@singledoggy

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions