Skip to content

Commit 09eb52c

Browse files
committed
TYP: fix type-check incompatibilities with matplotlib 3.8
1 parent 8757cf2 commit 09eb52c

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

yt/visualization/_handlers.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
import weakref
33
from numbers import Real
4-
from typing import Any, Literal, Optional, Union
4+
from typing import TYPE_CHECKING, Any, Literal, Optional, Union
55

66
import matplotlib as mpl
77
import numpy as np
@@ -18,6 +18,24 @@
1818
else:
1919
from typing_extensions import TypeAlias
2020

21+
if TYPE_CHECKING:
22+
# RGBColorType, RGBAColorType and ColorType are backported from matplotlib 3.8.0
23+
RGBColorType = Union[tuple[float, float, float], str]
24+
RGBAColorType = Union[
25+
str, # "none" or "#RRGGBBAA"/"#RGBA" hex strings
26+
tuple[float, float, float, float],
27+
# 2 tuple (color, alpha) representations, not infinitely recursive
28+
# RGBColorType includes the (str, float) tuple, even for RGBA strings
29+
tuple[RGBColorType, float],
30+
# (4-tuple, float) is odd, but accepted as the outer float overriding A of 4-tuple
31+
tuple[tuple[float, float, float, float], float],
32+
]
33+
34+
ColorType = Union[RGBColorType, RGBAColorType]
35+
36+
# this type alias is unique to the present module
37+
ColormapInput: TypeAlias = Union[Colormap, str, None]
38+
2139

2240
class NormHandler:
2341
"""
@@ -398,16 +416,6 @@ def get_minmax(data):
398416
return linthresh
399417

400418

401-
BackgroundColor: TypeAlias = Union[
402-
tuple[float, float, float, float],
403-
# np.ndarray is only runtime-subscribtable since numpy 1.22
404-
"np.ndarray[Any, Any]",
405-
str,
406-
None,
407-
]
408-
ColormapInput: TypeAlias = Union[Colormap, str, None]
409-
410-
411419
class ColorbarHandler:
412420
__slots__ = ("_draw_cbar", "_draw_minorticks", "_cmap", "_background_color")
413421

@@ -416,14 +424,14 @@ def __init__(
416424
*,
417425
draw_cbar: bool = True,
418426
draw_minorticks: bool = True,
419-
cmap: ColormapInput = None,
427+
cmap: "ColormapInput" = None,
420428
background_color: Optional[str] = None,
421429
):
422430
self._draw_cbar = draw_cbar
423431
self._draw_minorticks = draw_minorticks
424432
self._cmap: Optional[Colormap] = None
425433
self._set_cmap(cmap)
426-
self._background_color: BackgroundColor = background_color
434+
self._background_color: Optional["ColorType"] = background_color
427435

428436
@property
429437
def draw_cbar(self) -> bool:
@@ -454,18 +462,18 @@ def cmap(self) -> Colormap:
454462
return self._cmap or mpl.colormaps[ytcfg.get("yt", "default_colormap")]
455463

456464
@cmap.setter
457-
def cmap(self, newval: ColormapInput) -> None:
465+
def cmap(self, newval: "ColormapInput") -> None:
458466
self._set_cmap(newval)
459467

460-
def _set_cmap(self, newval: ColormapInput) -> None:
468+
def _set_cmap(self, newval: "ColormapInput") -> None:
461469
# a separate setter function is better supported by type checkers (mypy)
462470
# than relying purely on a property setter to narrow type
463471
# from ColormapInput to Colormap
464472
if isinstance(newval, Colormap) or newval is None:
465473
self._cmap = newval
466474
elif isinstance(newval, str):
467475
self._cmap = mpl.colormaps[newval]
468-
elif is_sequence(newval):
476+
elif is_sequence(newval): # type: ignore[unreachable]
469477
# tuple colormaps are from palettable (or brewer2mpl)
470478
self._cmap = get_brewer_cmap(newval)
471479
else:
@@ -475,14 +483,11 @@ def _set_cmap(self, newval: ColormapInput) -> None:
475483
)
476484

477485
@property
478-
def background_color(self) -> BackgroundColor:
486+
def background_color(self) -> "ColorType":
479487
return self._background_color or "white"
480488

481489
@background_color.setter
482-
def background_color(self, newval: BackgroundColor) -> None:
483-
# not attempting to constrain types here because
484-
# down the line it really depends on matplotlib.axes.Axes.set_faceolor
485-
# which is very type-flexibile
490+
def background_color(self, newval: Optional["ColorType"]) -> None:
486491
if newval is None:
487492
self._background_color = self.cmap(0)
488493
else:

yt/visualization/base_plot_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import warnings
33
from abc import ABC
44
from io import BytesIO
5-
from types import FunctionType
65
from typing import TYPE_CHECKING, Optional, TypedDict, Union
76

87
import matplotlib
@@ -37,6 +36,7 @@
3736
from matplotlib.axes import Axes
3837
from matplotlib.axis import Axis
3938
from matplotlib.figure import Figure
39+
from matplotlib.transforms import Transform
4040

4141
class FormatKwargs(TypedDict):
4242
style: Literal["scientific"]
@@ -249,7 +249,7 @@ def __init__(
249249
):
250250
"""Initialize ImagePlotMPL class object"""
251251

252-
self._transform: Optional[FunctionType]
252+
self._transform: Optional["Transform"]
253253
setdefaultattr(self, "_transform", None)
254254

255255
self.colorbar_handler = colorbar_handler

0 commit comments

Comments
 (0)