Skip to content

Commit b5456db

Browse files
committed
Remove default. usage outside the RcParams class
This is to make the interface such that the code other than RcParams doesn't have to deal with the `deafult.*` namespace anywhere. This also changes the keys returned by `.keys()` to not have the `default.*` in the key name.
1 parent 1cdb1df commit b5456db

File tree

6 files changed

+32
-35
lines changed

6 files changed

+32
-35
lines changed

lib/matplotlib/__init__.py

+16-19
Original file line numberDiff line numberDiff line change
@@ -776,12 +776,12 @@ def __setitem__(self, key, val):
776776
return
777777
elif key == 'backend' or key == "default.backend":
778778
if val is rcsetup._auto_backend_sentinel:
779-
if 'backend' in self:
779+
if 'backend' in self or 'default.backend' in self:
780780
return
781-
if key in self.single_key_set:
782-
key = f"default.{key}"
783781
try:
784782
cval = self.validate[key](val)
783+
if key in self.single_key_set:
784+
key = f"default.{key}"
785785
except ValueError as ve:
786786
raise ValueError(f"Key {key}: {ve}") from None
787787
self._set(key, cval)
@@ -846,7 +846,7 @@ def __contains__(self, key):
846846
def __iter__(self):
847847
"""Yield from sorted list of keys"""
848848
keys = (
849-
".".join((space, key))
849+
".".join((space, key)) if space != 'default' else key
850850
for space, mapping in self._namespace_maps.items()
851851
for key in mapping.keys()
852852
)
@@ -1018,8 +1018,6 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
10181018
config = RcParams()
10191019

10201020
for key, (val, line, line_no) in rc_temp.items():
1021-
if key in config.single_key_set:
1022-
key = f"default.{key}"
10231021
if key in rcsetup._validators:
10241022
if fail_on_error:
10251023
config[key] = val # try to convert to proper type or raise
@@ -1096,23 +1094,22 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
10961094
# in that case. However, packagers can set a different default backend
10971095
# (resulting in a normal `#backend: foo` line) in which case we should *not*
10981096
# fill in _auto_backend_sentinel.
1099-
rcParamsDefault.setdefault("default.backend", rcsetup._auto_backend_sentinel)
1100-
params_dict = RcParams()
1101-
params_dict.update(rcParamsDefault.items())
1102-
params_dict.update(_rc_params_in_file(matplotlib_fname()))
1103-
rcParamsOrig = params_dict.copy()
1097+
rcParamsDefault.setdefault("backend", rcsetup._auto_backend_sentinel)
1098+
rcParams = RcParams()
1099+
rcParams.update(rcParamsDefault.items())
1100+
rcParams.update(_rc_params_in_file(matplotlib_fname()))
1101+
rcParamsOrig = rcParams.copy()
11041102
with _api.suppress_matplotlib_deprecation_warning():
11051103
# This also checks that all rcParams are indeed listed in the template.
11061104
# Assigning to rcsetup.defaultParams is left only for backcompat.
11071105
defaultParams = rcsetup.defaultParams = {
11081106
# We want to resolve deprecated rcParams, but not backend...
1109-
key: [(rcsetup._auto_backend_sentinel if key == "default.backend" else
1107+
key: [(rcsetup._auto_backend_sentinel if key == "backend" else
11101108
rcParamsDefault[key]),
11111109
validator]
11121110
for key, validator in rcsetup._validators.items()}
1113-
if params_dict['axes.formatter.use_locale']:
1111+
if rcParams['axes.formatter.use_locale']:
11141112
locale.setlocale(locale.LC_ALL, '')
1115-
rcParams = RcParams(params_dict)
11161113

11171114

11181115
def rc(group, **kwargs):
@@ -1388,14 +1385,14 @@ def use(backend, *, force=True):
13881385
# value which will be respected when the user finally imports
13891386
# pyplot
13901387
else:
1391-
rcParams['default.backend'] = backend
1388+
rcParams['backend'] = backend
13921389
# if the user has asked for a given backend, do not helpfully
13931390
# fallback
1394-
rcParams['default.backend_fallback'] = False
1391+
rcParams['backend_fallback'] = False
13951392

13961393

13971394
if os.environ.get('MPLBACKEND'):
1398-
rcParams['default.backend'] = os.environ.get('MPLBACKEND')
1395+
rcParams['backend'] = os.environ.get('MPLBACKEND')
13991396

14001397

14011398
def get_backend():
@@ -1413,7 +1410,7 @@ def interactive(b):
14131410
"""
14141411
Set whether to redraw after every plotting command (e.g. `.pyplot.xlabel`).
14151412
"""
1416-
rcParams['default.interactive'] = b
1413+
rcParams['interactive'] = b
14171414

14181415

14191416
def is_interactive():
@@ -1425,7 +1422,7 @@ def is_interactive():
14251422
This function is only intended for use in backends. End users should
14261423
use `.pyplot.isinteractive` instead.
14271424
"""
1428-
return rcParams['default.interactive']
1425+
return rcParams['interactive']
14291426

14301427

14311428
def _init_tests():

lib/matplotlib/pyplot.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,16 @@ def switch_backend(newbackend):
302302
except ImportError:
303303
continue
304304
else:
305-
rcParamsOrig['default.backend'] = candidate
305+
rcParamsOrig['backend'] = candidate
306306
return
307307
else:
308308
# Switching to Agg should always succeed; if it doesn't, let the
309309
# exception propagate out.
310310
switch_backend("agg")
311-
rcParamsOrig["default.backend"] = "agg"
311+
rcParamsOrig["backend"] = "agg"
312312
return
313313
# have to escape the switch on access logic
314-
old_backend = rcParams['default.backend']
314+
old_backend = rcParams._get('backend')
315315

316316
backend_mod = importlib.import_module(
317317
cbook._backend_module_name(newbackend))
@@ -384,7 +384,7 @@ def draw_if_interactive():
384384
_log.debug("Loaded backend %s version %s.",
385385
newbackend, backend_mod.backend_version)
386386

387-
rcParams['default.backend'] = rcParamsDefault['default.backend'] = newbackend
387+
rcParams['backend'] = rcParamsDefault['backend'] = newbackend
388388
_backend_mod = backend_mod
389389
for func_name in ["new_figure_manager", "draw_if_interactive", "show"]:
390390
globals()[func_name].__signature__ = inspect.signature(
@@ -2379,11 +2379,11 @@ def polar(*args, **kwargs) -> list[Line2D]:
23792379
# If rcParams['backend_fallback'] is true, and an interactive backend is
23802380
# requested, ignore rcParams['backend'] and force selection of a backend that
23812381
# is compatible with the current running interactive framework.
2382-
if (rcParams["default.backend_fallback"]
2382+
if (rcParams["backend_fallback"]
23832383
and rcParams._get_backend_or_none() in ( # type: ignore
23842384
set(_interactive_bk) - {'WebAgg', 'nbAgg'})
23852385
and cbook._get_running_interactive_framework()): # type: ignore
2386-
rcParams._set("default.backend", rcsetup._auto_backend_sentinel) # type: ignore
2386+
rcParams._set("backend", rcsetup._auto_backend_sentinel) # type: ignore
23872387

23882388
# fmt: on
23892389

lib/matplotlib/rcsetup.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -811,12 +811,12 @@ def _convert_validator_spec(key, conv):
811811
# The rcParams defaults are defined in lib/matplotlib/mpl-data/matplotlibrc, which
812812
# gets copied to matplotlib/mpl-data/matplotlibrc by the setup script.
813813
_validators = {
814-
"default.backend": validate_backend,
815-
"default.backend_fallback": validate_bool,
814+
"backend": validate_backend,
815+
"backend_fallback": validate_bool,
816816
"figure.hooks": validate_stringlist,
817-
"default.toolbar": _validate_toolbar,
818-
"default.interactive": validate_bool,
819-
"default.timezone": validate_string,
817+
"toolbar": _validate_toolbar,
818+
"interactive": validate_bool,
819+
"timezone": validate_string,
820820

821821
"webagg.port": validate_int,
822822
"webagg.address": validate_string,

lib/matplotlib/style/core.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
STYLE_EXTENSION = 'mplstyle'
4040
# A list of rcParams that should not be applied from styles
4141
STYLE_BLACKLIST = {
42-
'default.interactive', 'default.backend', 'webagg.port', 'webagg.address',
43-
'webagg.port_retries', 'webagg.open_in_browser', 'default.backend_fallback',
44-
'default.toolbar', 'default.timezone', 'figure.max_open_warning',
42+
'interactive', 'backend', 'webagg.port', 'webagg.address',
43+
'webagg.port_retries', 'webagg.open_in_browser', 'backend_fallback',
44+
'toolbar', 'timezone', 'figure.max_open_warning',
4545
'figure.raise_window', 'savefig.directory', 'tk.window_focus',
4646
'docstring.hardcopy', 'date.epoch'}
4747

lib/matplotlib/tests/test_rcparams.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def test_axes_titlecolor_rcparams():
192192

193193
def test_Issue_1713(tmpdir):
194194
rcpath = Path(tmpdir) / 'test_rcparams.rc'
195-
rcpath.write_text('default.timezone: UTC', encoding='utf-8')
195+
rcpath.write_text('timezone: UTC', encoding='utf-8')
196196
with mock.patch('locale.getpreferredencoding', return_value='UTF-32-BE'):
197197
rc = mpl.rc_params_from_file(rcpath, True, False)
198198
assert rc.get('timezone') == 'UTC'

lib/matplotlib/tests/test_widgets.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ def test_CheckButtons(ax):
10111011
@pytest.mark.parametrize("toolbar", ["none", "toolbar2", "toolmanager"])
10121012
def test_TextBox(ax, toolbar):
10131013
# Avoid "toolmanager is provisional" warning.
1014-
plt.rcParams._set("default.toolbar", toolbar)
1014+
plt.rcParams._set("toolbar", toolbar)
10151015

10161016
submit_event = mock.Mock(spec=noop, return_value=None)
10171017
text_change_event = mock.Mock(spec=noop, return_value=None)

0 commit comments

Comments
 (0)