Skip to content

Commit 12e1e0a

Browse files
committedJan 24, 2025·
TYP: replace CMAP type alias with an explicit union
1 parent 3454a25 commit 12e1e0a

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed
 

‎src/cmasher/utils.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ def __gt__(self, other: Self, /) -> bool: ...
7979
Name = NewType("Name", str)
8080

8181
# Type aliases
82-
CMAP: TypeAlias = str | Colormap
8382
RED: TypeAlias = float
8483
GREEN: TypeAlias = float
8584
BLUE: TypeAlias = float
@@ -354,7 +353,7 @@ def combine_cmaps(
354353

355354
# This function creates a standalone module of a CMasher colormap
356355
def create_cmap_mod(
357-
cmap: str,
356+
cmap: Name,
358357
*,
359358
save_dir: str | os.PathLike[str] = ".",
360359
_copy_name: str | None = None,
@@ -978,7 +977,7 @@ def get_cmap_list(cmap_type: str = "all") -> list[str]:
978977

979978

980979
# This function determines the colormap type of a given colormap
981-
def get_cmap_type(cmap: CMAP) -> str:
980+
def get_cmap_type(cmap: Colormap | Name) -> str:
982981
"""
983982
Checks what the colormap type (sequential; diverging; cyclic; qualitative;
984983
misc) of the provided `cmap` is and returns it.
@@ -1060,7 +1059,9 @@ def get_cmap_type(cmap: CMAP) -> str:
10601059

10611060

10621061
# Function create a colormap using a subset of the colors in an existing one
1063-
def get_sub_cmap(cmap: CMAP, start: float, stop: float, *, N: int | None = None) -> LC:
1062+
def get_sub_cmap(
1063+
cmap: Colormap | Name, start: float, stop: float, *, N: int | None = None
1064+
) -> LC:
10641065
"""
10651066
Creates a :obj:`~matplotlib.cm.ListedColormap` object using the colors in
10661067
the range `[start, stop]` of the provided `cmap` and returns it.
@@ -1241,7 +1242,7 @@ def sort_key(name):
12411242
else:
12421243
seen.add(base_str)
12431244

1244-
cm_name = base_str[3:]
1245+
cm_name = Name(base_str.removeprefix("cm_"))
12451246

12461247
# Process colormap files
12471248
try:
@@ -1276,7 +1277,7 @@ def sort_key(name):
12761277

12771278
# Check if provided cmap is a cyclic colormap
12781279
# If so, obtain its shifted (reversed) versions as well
1279-
if get_cmap_type("cmr." + cm_name) == "cyclic":
1280+
if get_cmap_type(Name("cmr." + cm_name)) == "cyclic":
12801281
# Determine the central value index of the colormap
12811282
idx = len(rgb) // 2
12821283

@@ -1422,7 +1423,7 @@ def set_cmap_legend_entry(artist: Artist, label: str) -> None:
14221423

14231424
# Function to take N equally spaced colors from a colormap
14241425
def take_cmap_colors(
1425-
cmap: CMAP,
1426+
cmap: Colormap | Name,
14261427
N: int | None,
14271428
*,
14281429
cmap_range: tuple[float, float] = (0, 1),
@@ -1539,7 +1540,7 @@ def take_cmap_colors(
15391540

15401541
# Function to view what a colormap looks like
15411542
def view_cmap(
1542-
cmap: CMAP,
1543+
cmap: Colormap | Name,
15431544
*,
15441545
savefig: str | None = None,
15451546
show_test: bool = False,

0 commit comments

Comments
 (0)