Skip to content

Don't expose private styles in style.available #30235

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions lib/matplotlib/style/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
``context``
Context manager to use a style sheet temporarily.
``available``
List available style sheets.
List available style sheets. Underscore-prefixed names are considered private and
not listed, though may still be accessed directly from ``library``.
``library``
A dictionary of style names and matplotlib settings.
"""
Expand Down Expand Up @@ -245,8 +246,8 @@ def update_nested_dict(main_dict, new_dict):
def reload_library():
"""Reload the style library."""
library.clear()
library.update(_update_user_library(_base_library))
available[:] = sorted(library.keys())
library.update(_update_user_library(_base_library.copy()))
available[:] = sorted(name for name in library if not name.startswith('_'))


reload_library()
15 changes: 13 additions & 2 deletions lib/matplotlib/tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def temp_style(style_name, settings=None):
if not settings:
settings = DUMMY_SETTINGS
temp_file = f'{style_name}.mplstyle'
orig_library_paths = style.USER_LIBRARY_PATHS
try:
with TemporaryDirectory() as tmpdir:
# Write style settings to file in the tmpdir.
Expand All @@ -32,6 +33,7 @@ def temp_style(style_name, settings=None):
style.reload_library()
yield
finally:
style.USER_LIBRARY_PATHS = orig_library_paths
style.reload_library()


Expand All @@ -46,8 +48,17 @@ def test_invalid_rc_warning_includes_filename(caplog):


def test_available():
with temp_style('_test_', DUMMY_SETTINGS):
assert '_test_' in style.available
# Private name should not be listed in available but still usable.
assert '_classic_test_patch' not in style.available
assert '_classic_test_patch' in style.library

with temp_style('_test_', DUMMY_SETTINGS), temp_style('dummy', DUMMY_SETTINGS):
assert 'dummy' in style.available
assert 'dummy' in style.library
assert '_test_' not in style.available
assert '_test_' in style.library
assert 'dummy' not in style.available
assert '_test_' not in style.available


def test_use():
Expand Down
Loading