Skip to content

Commit

Permalink
TYP: replace CMAP type alias with an explicit union
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Jan 24, 2025
1 parent 3454a25 commit 12e1e0a
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/cmasher/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def __gt__(self, other: Self, /) -> bool: ...
Name = NewType("Name", str)

# Type aliases
CMAP: TypeAlias = str | Colormap
RED: TypeAlias = float
GREEN: TypeAlias = float
BLUE: TypeAlias = float
Expand Down Expand Up @@ -354,7 +353,7 @@ def combine_cmaps(

# This function creates a standalone module of a CMasher colormap
def create_cmap_mod(
cmap: str,
cmap: Name,
*,
save_dir: str | os.PathLike[str] = ".",
_copy_name: str | None = None,
Expand Down Expand Up @@ -978,7 +977,7 @@ def get_cmap_list(cmap_type: str = "all") -> list[str]:


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


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

cm_name = base_str[3:]
cm_name = Name(base_str.removeprefix("cm_"))

# Process colormap files
try:
Expand Down Expand Up @@ -1276,7 +1277,7 @@ def sort_key(name):

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

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

# Function to take N equally spaced colors from a colormap
def take_cmap_colors(
cmap: CMAP,
cmap: Colormap | Name,
N: int | None,
*,
cmap_range: tuple[float, float] = (0, 1),
Expand Down Expand Up @@ -1539,7 +1540,7 @@ def take_cmap_colors(

# Function to view what a colormap looks like
def view_cmap(
cmap: CMAP,
cmap: Colormap | Name,
*,
savefig: str | None = None,
show_test: bool = False,
Expand Down

0 comments on commit 12e1e0a

Please sign in to comment.