8000 better use of union for colormap types · matplotlib/matplotlib@37f1e7e · GitHub
[go: up one dir, main page]

Skip to content

Commit 37f1e7e

Browse files
committed
better use of union for colormap types
Defines ColorMapType as Union[colors.Colormap, colors.BivarColormap, colors.MultivarColormap] in typing.py
1 parent cf1cd72 commit 37f1e7e

File tree

3 files changed

+5
-13
lines changed

3 files changed

+5
-13
lines changed

lib/matplotlib/cm.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@
2727
from matplotlib._cm_listed import cmaps as cmaps_listed
2828
from matplotlib._cm_multivar import cmap_families as multivar_cmaps
2929
from matplotlib._cm_bivar import cmaps as bivar_cmaps
30-
from typing import Union
31-
32-
ColorMapType = Union[colors.Colormap,
33-
colors.BivarColormap,
34-
colors.MultivarColormap]
30+
from .typing import ColorMapType
3531

3632
_LUTSIZE = mpl.rcParams['image.lut']
3733

@@ -227,9 +223,7 @@ def get_cmap(self, cmap):
227223
return self[mpl.rcParams["image.cmap"]]
228224

229225
# if the user passed in a valid colormap type, simply return it
230-
if isinstance(cmap, (colors.Colormap,
231-
colors.BivarColormap,
232-
colors.MultivarColormap)):
226+
if isinstance(cmap, ColorMapType):
233227
return cmap
234228
if isinstance(cmap, str):
235229
_api.check_in_list(sorted(_colormaps), cmap=cmap)

lib/matplotlib/cm.pyi

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ from matplotlib.colorbar import Colorbar
55
import numpy as np
66
from numpy.typing import ArrayLike
77
from typing import Union
8-
9-
ColorMapType = Union[colors.Colormap,
10-
colors.BivarColormap,
11-
colors.MultivarColormap]
8+
from .typing import ColorMapType
129

1310
class ColormapRegistry(Mapping[str, ColorMapType]):
1411
def __init__(self, cmaps: Mapping[str, ColorMapType]) -> None: ...

lib/matplotlib/typing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import pathlib
1414
from typing import Any, Literal, TypeAlias, TypeVar
1515

16-
from . import path
16+
from . import path, colors
1717
from ._enums import JoinStyle, CapStyle
1818
from .markers import MarkerStyle
1919

@@ -34,6 +34,7 @@
3434
RGBAColourType: TypeAlias = RGBAColorType
3535
ColourType: TypeAlias = ColorType
3636

37+
ColorMapType = Union[colors.Colormap, colors.BivarColormap, colors.MultivarColormap]
3738
LineStyleType: TypeAlias = str | tuple[float, Sequence[float]]
3839
DrawStyleType: TypeAlias = Literal["default", "steps", "steps-pre", "steps-mid",
3940
"steps-post"]

0 commit comments

Comments
 (0)
0