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

Fix typing for test_plot.py #9234

Merged
merged 7 commits into from
Jul 13, 2024
Merged

Conversation

Illviljan
Copy link
Contributor

@Illviljan Illviljan commented Jul 11, 2024

Fixes mypy errors related to plotting seen in CI.

xref: #9231

@Illviljan Illviljan marked this pull request as ready for review July 11, 2024 12:57
@Illviljan
Copy link
Contributor Author

Illviljan commented Jul 11, 2024

These looks like an error in matplotlib:

xarray/tests/test_plot.py: note: In function "test_plot1d_default_rcparams":
xarray/tests/test_plot.py:3380: error: Incompatible types in assignment (expression has type "tuple[float, float, float] | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float] | Sequence[tuple[float, float, float] | str | str | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float]]", variable has type "ndarray[Any, Any]")  [assignment]
xarray/tests/test_plot.py:3387: error: Incompatible types in assignment (expression has type "tuple[float, float, float] | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float] | Sequence[tuple[float, float, float] | str | str | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float]]", variable has type "ndarray[Any, Any]")  [assignment]
xarray/tests/test_plot.py:3399: error: Incompatible types in assignment (expression has type "tuple[float, float, float] | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float] | Sequence[tuple[float, float, float] | str | str | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float]]", variable has type "ndarray[Any, Any]")  [assignment]

The test boils down to:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import xarray as xr

ds = xr.tutorial.scatter_example_dataset(seed=42)

fig, ax = plt.subplots(1, 1)
ds.plot.scatter(x="A", y="B", marker="o", ax=ax)
actual: np.ndarray = mpl.colors.to_rgba_array("w")
assert isinstance(actual, np.ndarray)
expected: np.ndarray = ax.collections[0].get_edgecolor() # mypy complains here.
assert isinstance(expected, np.ndarray)

np.testing.assert_allclose(actual, expected)

get_edgecolor returns an np.ndarray and the asserts proves that but mypy disagrees, ignoring these.

A test with matplotlib only:

def test_debug() -> None:
    import numpy as np
    import matplotlib as mpl
    import matplotlib.pyplot as plt


    fig, ax = plt.subplots(1, 1)
    ax.scatter(x=np.array([1, 2, 3]), y=np.array([6, 7, 8]), color="k")
    actual: np.ndarray = mpl.colors.to_rgba_array("k")
    assert isinstance(actual, np.ndarray)
    expected: np.ndarray = ax.collections[0].get_edgecolor() # error: Incompatible types in assignment (expression has type "tuple[float, float, float] | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float] | Sequence[tuple[float, float, float] | str | str | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float]]", variable has type "ndarray[Any, Any]")  [assignment]
    assert isinstance(expected, np.ndarray)
    np.testing.assert_allclose(actual, expected)

Which also fails

xarray/tests/test_plot.py: note: In function "test_debug":
xarray/tests/test_plot.py:3377: error: Incompatible types in assignment (expression has type "tuple[float, float, float] | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float] | Sequence[tuple[float, float, float] | str | str | tuple[float, float, float, float] | tuple[tuple[float, float, float] | str, float] | tuple[tuple[float, float, float, float], float]]", variable has type "ndarray[Any, Any]")  [assignment]

Cc @ksunden if this still interests you.

CI has installed this:

INSTALLED VERSIONS
------------------
commit: ac3f09fdcd9d406784303240b534e31fbfd411d7
python: 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) [GCC 12.3.0]
python-bits: 64
OS: Linux
OS-release: 6.5.0-1023-azure
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.3
libnetcdf: 4.9.2

xarray: 2024.6.1.dev51+gac3f09fd
pandas: 2.2.2
numpy: 2.0.0
scipy: 1.14.0
netCDF4: 1.7.1
pydap: None
h5netcdf: 1.3.0
h5py: 3.11.0
zarr: 2.18.2
cftime: 1.6.4
nc_time_axis: 1.4.1
iris: 3.9.0
bottleneck: 1.4.0
dask: 2024.7.0
distributed: 2024.7.0
matplotlib: 3.9.1
cartopy: 0.23.0
seaborn: 0.13.2
numbagg: 0.8.1
fsspec: 2024.6.1
cupy: None
pint: None
sparse: 0.15.4
flox: 0.9.8
numpy_groupies: 0.11.1
setuptools: 70.2.0
pip: 24.0
conda: 24.5.0
pytest: 8.2.2
mypy: None
IPython: None
sphinx: None

@Illviljan Illviljan added the plan to merge Final call for comments label Jul 12, 2024
@headtr1ck
Copy link
Collaborator

Are we trying to support numpy 1 and 2 in typing? Or do we simply switch to 2 entirely?

@max-sixty
Copy link
Collaborator

Are we trying to support numpy 1 and 2 in typing? Or do we simply switch to 2 entirely?

Possibly others have decided already — I defer to that if so.

But if not — I would advocate for minimizing developer burden in typing, and so only supporting a single version of dependencies. To the extent we're moving to numpy=2, that seems preferable...

@headtr1ck
Copy link
Collaborator

But if not — I would advocate for minimizing developer burden in typing, and so only supporting a single version of dependencies. To the extent we're moving to numpy=2, that seems preferable...

I totally agree. For internal typing checks we can restrict to numpy2. But anything exposed to user side should be compatible, I guess.

But actually I am not sure how to achieve that because mypy does not understand package version checks, only python version checks.

@ksunden
Copy link
Contributor

ksunden commented Jul 13, 2024

This is another case of "np.ndarray doesn't count as a "Sequence", but is used as one regularly"

@max-sixty
Copy link
Collaborator

But anything exposed to user side should be compatible, I guess.

Though do we expose much of the numpy type system ourselves?

To the extent we do — I think it might be really difficult to maintain compat — I might give this up quite easily...

@max-sixty max-sixty merged commit d8b7644 into pydata:main Jul 13, 2024
38 of 40 checks passed
dcherian added a commit to dcherian/xarray that referenced this pull request Jul 17, 2024
* main:
  Enable pandas type checking (pydata#9213)
  Per-variable specification of boolean parameters in open_dataset (pydata#9218)
  test push
  Added a space to the documentation (pydata#9247)
  Fix typing for test_plot.py (pydata#9234)
  Allow mypy to run in vscode (pydata#9239)
  Revert "Test main push"
  Test main push
  Revert "Update _typing.py"
  Update _typing.py
  Add a `.drop_attrs` method (pydata#8258)
dcherian added a commit that referenced this pull request Jul 22, 2024
* main:
  add backend intro and how-to diagram (#9175)
  Fix copybutton for multi line examples in double digit ipython cells (#9264)
  Update signature for _arrayfunction.__array__ (#9237)
  Add encode_cf_datetime benchmark (#9262)
  groupby, resample: Deprecate some positional args (#9236)
  Delete ``base`` and ``loffset`` parameters to resample (#9233)
  Update dropna docstring (#9257)
  Grouper, Resampler as public api (#8840)
  Fix mypy on main (#9252)
  fix fallback isdtype method (#9250)
  Enable pandas type checking (#9213)
  Per-variable specification of boolean parameters in open_dataset (#9218)
  test push
  Added a space to the documentation (#9247)
  Fix typing for test_plot.py (#9234)
dcherian added a commit that referenced this pull request Jul 24, 2024
* main: (54 commits)
  Adding `open_datatree` backend-specific keyword arguments (#9199)
  [pre-commit.ci] pre-commit autoupdate (#9202)
  Restore ability to specify _FillValue as Python native integers (#9258)
  add backend intro and how-to diagram (#9175)
  Fix copybutton for multi line examples in double digit ipython cells (#9264)
  Update signature for _arrayfunction.__array__ (#9237)
  Add encode_cf_datetime benchmark (#9262)
  groupby, resample: Deprecate some positional args (#9236)
  Delete ``base`` and ``loffset`` parameters to resample (#9233)
  Update dropna docstring (#9257)
  Grouper, Resampler as public api (#8840)
  Fix mypy on main (#9252)
  fix fallback isdtype method (#9250)
  Enable pandas type checking (#9213)
  Per-variable specification of boolean parameters in open_dataset (#9218)
  test push
  Added a space to the documentation (#9247)
  Fix typing for test_plot.py (#9234)
  Allow mypy to run in vscode (#9239)
  Revert "Test main push"
  ...
dcherian added a commit to JoelJaeschke/xarray that referenced this pull request Jul 25, 2024
…monotonic-variable

* main: (995 commits)
  Adding `open_datatree` backend-specific keyword arguments (pydata#9199)
  [pre-commit.ci] pre-commit autoupdate (pydata#9202)
  Restore ability to specify _FillValue as Python native integers (pydata#9258)
  add backend intro and how-to diagram (pydata#9175)
  Fix copybutton for multi line examples in double digit ipython cells (pydata#9264)
  Update signature for _arrayfunction.__array__ (pydata#9237)
  Add encode_cf_datetime benchmark (pydata#9262)
  groupby, resample: Deprecate some positional args (pydata#9236)
  Delete ``base`` and ``loffset`` parameters to resample (pydata#9233)
  Update dropna docstring (pydata#9257)
  Grouper, Resampler as public api (pydata#8840)
  Fix mypy on main (pydata#9252)
  fix fallback isdtype method (pydata#9250)
  Enable pandas type checking (pydata#9213)
  Per-variable specification of boolean parameters in open_dataset (pydata#9218)
  test push
  Added a space to the documentation (pydata#9247)
  Fix typing for test_plot.py (pydata#9234)
  Allow mypy to run in vscode (pydata#9239)
  Revert "Test main push"
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to merge Final call for comments topic-typing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants