1
1
import sys
2
2
import weakref
3
3
from numbers import Real
4
- from typing import Any , Literal , Optional , Union
4
+ from typing import TYPE_CHECKING , Any , Literal , Optional , Union
5
5
6
6
import matplotlib as mpl
7
7
import numpy as np
18
18
else :
19
19
from typing_extensions import TypeAlias
20
20
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
+
21
39
22
40
class NormHandler :
23
41
"""
@@ -398,16 +416,6 @@ def get_minmax(data):
398
416
return linthresh
399
417
400
418
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
-
411
419
class ColorbarHandler :
412
420
__slots__ = ("_draw_cbar" , "_draw_minorticks" , "_cmap" , "_background_color" )
413
421
@@ -416,14 +424,14 @@ def __init__(
416
424
* ,
417
425
draw_cbar : bool = True ,
418
426
draw_minorticks : bool = True ,
419
- cmap : ColormapInput = None ,
427
+ cmap : " ColormapInput" = None ,
420
428
background_color : Optional [str ] = None ,
421
429
):
422
430
self ._draw_cbar = draw_cbar
423
431
self ._draw_minorticks = draw_minorticks
424
432
self ._cmap : Optional [Colormap ] = None
425
433
self ._set_cmap (cmap )
426
- self ._background_color : BackgroundColor = background_color
434
+ self ._background_color : Optional [ "ColorType" ] = background_color
427
435
428
436
@property
429
437
def draw_cbar (self ) -> bool :
@@ -454,18 +462,18 @@ def cmap(self) -> Colormap:
454
462
return self ._cmap or mpl .colormaps [ytcfg .get ("yt" , "default_colormap" )]
455
463
456
464
@cmap .setter
457
- def cmap (self , newval : ColormapInput ) -> None :
465
+ def cmap (self , newval : " ColormapInput" ) -> None :
458
466
self ._set_cmap (newval )
459
467
460
- def _set_cmap (self , newval : ColormapInput ) -> None :
468
+ def _set_cmap (self , newval : " ColormapInput" ) -> None :
461
469
# a separate setter function is better supported by type checkers (mypy)
462
470
# than relying purely on a property setter to narrow type
463
471
# from ColormapInput to Colormap
464
472
if isinstance (newval , Colormap ) or newval is None :
465
473
self ._cmap = newval
466
474
elif isinstance (newval , str ):
467
475
self ._cmap = mpl .colormaps [newval ]
468
- elif is_sequence (newval ):
476
+ elif is_sequence (newval ): # type: ignore[unreachable]
469
477
# tuple colormaps are from palettable (or brewer2mpl)
470
478
self ._cmap = get_brewer_cmap (newval )
471
479
else :
@@ -475,14 +483,11 @@ def _set_cmap(self, newval: ColormapInput) -> None:
475
483
)
476
484
477
485
@property
478
- def background_color (self ) -> BackgroundColor :
486
+ def background_color (self ) -> "ColorType" :
479
487
return self ._background_color or "white"
480
488
481
489
@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 :
486
491
if newval is None :
487
492
self ._background_color = self .cmap (0 )
488
493
else :
0 commit comments