8000 Merge pull request #30235 from QuLogic/private-style · matplotlib/matplotlib@b6777b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit b6777b1

Browse files
authored
Merge pull request #30235 from QuLogic/private-style
Don't expose private styles in style.available
2 parents 99b4ee3 + d231a25 commit b6777b1

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

lib/matplotlib/style/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
``context``
77
Context manager to use a style sheet temporarily.
88
``available``
9-
List available style sheets.
9+
List available style sheets. Underscore-prefixed names are considered private and
10+
not listed, though may still be accessed directly from ``library``.
1011
``library``
1112
A dictionary of style names and matplotlib settings.
1213
"""
@@ -245,8 +246,8 @@ def update_nested_dict(main_dict, new_dict):
245246
def reload_library():
246247
"""Reload the style library."""
247248
library.clear()
248-
library.update(_update_user_library(_base_library))
249-
available[:] = sorted(library.keys())
249+
library.update(_update_user_library(_base_library.copy()))
250+
available[:] = sorted(name for name in library if not name.startswith('_'))
250251

251252

252253
reload_library()

lib/matplotlib/tests/test_style.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def temp_style(style_name, settings=None):
2121
if not settings:
2222
settings = DUMMY_SETTINGS
2323
temp_file = f'{style_name}.mplstyle'
24+
orig_library_paths = style.USER_LIBRARY_PATHS
2425
try:
2526
with TemporaryDirectory() as tmpdir:
2627
# Write style settings to file in the tmpdir.
@@ -32,6 +33,7 @@ def temp_style(style_name, settings=None):
3233
style.reload_library()
3334
yield
3435
finally:
36+
style.USER_LIBRARY_PATHS = orig_library_paths
3537
style.reload_library()
3638

3739

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

4749

4850
def test_available():
49-
with temp_style('_test_', DUMMY_SETTINGS):
50-
assert '_test_' in style.available
51+
# Private name should not be listed in available but still usable.
52+
assert '_classic_test_patch' not in style.available
53+
assert '_classic_test_patch' in style.library
54+
55+
with temp_style('_test_', DUMMY_SETTINGS), temp_style('dummy', DUMMY_SETTINGS):
56+
assert 'dummy' in style.available
57+
assert 'dummy' in style.library
58+
assert '_test_' not in style.available
59+
assert '_test_' in style.library
60+
assert 'dummy' not in style.available
61+
assert '_test_' not in style.available
5162

5263

5364
def test_use():

0 commit comments

Comments
 (0)
0