8000 FIX: ensure type stability for missing cmaps in `set_cmap` by tacaswell · Pull Request #23785 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

FIX: ensure type stability for missing cmaps in set_cmap #23785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,8 @@ def _ensure_cmap(cmap):
"""
if isinstance(cmap, colors.Colormap):
return cmap
return mpl.colormaps[
cmap if cmap is not None else mpl.rcParams['image.cmap']
]
cmap_name = cmap if cmap is not None else mpl.rcParams["image.cmap"]
# use check_in_list to ensure type stability of the exception raised by
# the internal usage of this (ValueError vs KeyError)
_api.check_in_list(sorted(_colormaps), cmap=cmap_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to put a comment in saying you added this check for error raising type stability so we don't remove it later? Does it need to be sorted or can it be list(_colormaps)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input is used in formatting the error to tell the user what the allowed values are. Given the number of color maps I think sorting is a usability win / kindness for the user to discover what the correct spelling of the color map they tried to ask for is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Makes sense. Agreed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a comment about why we are doing extra work.

return mpl.colormaps[cmap_name]
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1543,3 +1543,11 @@ def test_color_sequences():
plt.color_sequences.unregister('rgb') # multiple unregisters are ok
with pytest.raises(ValueError, match="Cannot unregister builtin"):
plt.color_sequences.unregister('tab10')


def test_cm_set_cmap_error():
sm = cm.ScalarMappable()
# Pick a name we are pretty sure will never be a colormap name
bad_cmap = 'AardvarksAreAwkward'
with pytest.raises(ValueError, match=bad_cmap):
sm.set_cmap(bad_cmap)
0