Skip to content

Commit 54a1a11

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 4f9d8b6 commit 54a1a11

File tree

6 files changed

+32
-35
lines changed

6 files changed

+32
-35
lines changed

lib/matplotlib/__init__.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -781,12 +781,12 @@ def __setitem__(self, key, val):
781781
return
782782
elif key == 'backend' or key == "default.backend":
783783
if val is rcsetup._auto_backend_sentinel:
784-
if 'backend' in self:
784+
if 'backend' in self or 'default.backend' in self:
785785
return
786-
if key in self.single_key_set:
787-
key = f"default.{key}"
788786
try:
789787
cval = self.validate[key](val)
788+
if key in self.single_key_set:
789+
key = f"default.{key}"
790790
except ValueError as ve:
791791
raise ValueError(f"Key {key}: {ve}") from None
792792
self._set(key, cval)
@@ -851,7 +851,7 @@ def __contains__(self, key):
851851
def __iter__(self):
852852
"""Yield from sorted list of keys"""
853853
keys = (
854-
".".join((space, key))
854+
".".join((space, key)) if space != 'default' else key
855855
for space, mapping in self._namespace_maps.items()
856856
for key in mapping.keys()
857857
)
@@ -1023,8 +1023,6 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
10231023
config = RcParams()
10241024

10251025
for key, (val, line, line_no) in rc_temp.items():
1026-
if key in config.single_key_set:
1027-
key = f"default.{key}"
10281026
if key in rcsetup._validators:
10291027
if fail_on_error:
10301028
config[key] = val # try to convert to proper type or raise
@@ -1101,23 +1099,22 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
11011099
# in that case. However, packagers can set a different default backend
11021100
# (resulting in a normal `#backend: foo` line) in which case we should *not*
11031101
# fill in _auto_backend_sentinel.
1104-
rcParamsDefault.setdefault("default.backend", rcsetup._auto_backend_sentinel)
1105-
params_dict = RcParams()
1106-
params_dict.update(rcParamsDefault.items())
1107-
params_dict.update(_rc_params_in_file(matplotlib_fname()))
1108-
rcParamsOrig = params_dict.copy()
1102+
rcParamsDefault.setdefault("backend", rcsetup._auto_backend_sentinel)
1103+
rcParams = RcParams()
1104+
rcParams.update(rcParamsDefault.items())
1105+
rcParams.update(_rc_params_in_file(matplotlib_fname()))
1106+
rcParamsOrig = rcParams.copy()
11091107
with _api.suppress_matplotlib_deprecation_warning():
11101108
# This also checks that all rcParams are indeed listed in the template.
11111109
# Assigning to rcsetup.defaultParams is left only for backcompat.
11121110
defaultParams = rcsetup.defaultParams = {
11131111
# We want to resolve deprecated rcParams, but not backend...
1114-
key: [(rcsetup._auto_backend_sentinel if key == "default.backend" else
1112+
key: [(rcsetup._auto_backend_sentinel if key == "backend" else
11151113
rcParamsDefault[key]),
11161114
validator]
11171115
for key, validator in rcsetup._validators.items()}
1118-
if params_dict['axes.formatter.use_locale']:
1116+
if rcParams['axes.formatter.use_locale']:
11191117
locale.setlocale(locale.LC_ALL, '')
1120-
rcParams = RcParams(params_dict)
11211118

11221119

11231120
def rc(group, **kwargs):
@@ -1393,14 +1390,14 @@ def use(backend, *, force=True):
13931390
# value which will be respected when the user finally imports
13941391
# pyplot
13951392
else:
1396-
rcParams['default.backend'] = backend
1393+
rcParams['backend'] = backend
13971394
# if the user has asked for a given backend, do not helpfully
13981395
# fallback
1399-
rcParams['default.backend_fallback'] = False
1396+
rcParams['backend_fallback'] = False
14001397

14011398

14021399
if os.environ.get('MPLBACKEND'):
1403-
rcParams['default.backend'] = os.environ.get('MPLBACKEND')
1400+
rcParams['backend'] = os.environ.get('MPLBACKEND')
14041401

14051402

14061403
def get_backend():
@@ -1418,7 +1415,7 @@ def interactive(b):
14181415
"""
14191416
Set whether to redraw after every plotting command (e.g. `.pyplot.xlabel`).
14201417
"""
1421-
rcParams['default.interactive'] = b
1418+
rcParams['interactive'] = b
14221419

14231420

14241421
def is_interactive():
@@ -1430,7 +1427,7 @@ def is_interactive():
14301427
This function is only intended for use in backends. End users should
14311428
use `.pyplot.isinteractive` instead.
14321429
"""
1433-
return rcParams['default.interactive']
1430+
return rcParams['interactive']
14341431

14351432

14361433
def _val_or_rc(val, rc_name):

lib/matplotlib/pyplot.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,16 +327,16 @@ def switch_backend(newbackend: str) -> None:
327327
except ImportError:
328328
continue
329329
else:
330-
rcParamsOrig['default.backend'] = candidate
330+
rcParamsOrig['backend'] = candidate
331331
return
332332
else:
333333
# Switching to Agg should always succeed; if it doesn't, let the
334334
# exception propagate out.
335335
switch_backend("agg")
336-
rcParamsOrig["default.backend"] = "agg"
336+
rcParamsOrig["backend"] = "agg"
337337
return
338338
# have to escape the switch on access logic
339-
old_backend = rcParams['default.backend']
339+
old_backend = rcParams._get('backend')
340340

341341
module = importlib.import_module(cbook._backend_module_name(newbackend))
342342
canvas_class = module.FigureCanvas
@@ -413,7 +413,7 @@ def draw_if_interactive() -> None:
413413
_log.debug("Loaded backend %s version %s.",
414414
newbackend, backend_mod.backend_version)
415415

416-
rcParams['default.backend'] = rcParamsDefault['default.backend'] = newbackend
416+
rcParams['backend'] = rcParamsDefault['backend'] = newbackend
417417
_backend_mod = backend_mod
418418
for func_name in ["new_figure_manager", "draw_if_interactive", "show"]:
419419
globals()[func_name].__signature__ = inspect.signature(
@@ -2472,11 +2472,11 @@ def polar(*args, **kwargs) -> list[Line2D]:
24722472
# If rcParams['backend_fallback'] is true, and an interactive backend is
24732473
# requested, ignore rcParams['backend'] and force selection of a backend that
24742474
# is compatible with the current running interactive framework.
2475-
if (rcParams["default.backend_fallback"]
2475+
if (rcParams["backend_fallback"]
24762476
and rcParams._get_backend_or_none() in ( # type: ignore
24772477
set(rcsetup.interactive_bk) - {'WebAgg', 'nbAgg'})
24782478
and cbook._get_running_interactive_framework()): # type: ignore
2479-
rcParams._set("default.backend", rcsetup._auto_backend_sentinel) # type: ignore
2479+
rcParams._set("backend", rcsetup._auto_backend_sentinel) # type: ignore
24802480

24812481
# fmt: on
24822482

lib/matplotlib/rcsetup.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -892,12 +892,12 @@ def _convert_validator_spec(key, conv):
892892
# The rcParams defaults are defined in lib/matplotlib/mpl-data/matplotlibrc, which
893893
# gets copied to matplotlib/mpl-data/matplotlibrc by the setup script.
894894
_validators = {
895-
"default.backend": validate_backend,
896-
"default.backend_fallback": validate_bool,
895+
"backend": validate_backend,
896+
"backend_fallback": validate_bool,
897897
"figure.hooks": validate_stringlist,
898-
"default.toolbar": _validate_toolbar,
899-
"default.interactive": validate_bool,
900-
"default.timezone": validate_string,
898+
"toolbar": _validate_toolbar,
899+
"interactive": validate_bool,
900+
"timezone": validate_string,
901901

902902
"webagg.port": validate_int,
903903
"webagg.address": validate_string,

lib/matplotlib/style/core.py

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def test_axes_titlecolor_rcparams():
199199

200200
def test_Issue_1713(tmpdir):
201201
rcpath = Path(tmpdir) / 'test_rcparams.rc'
202-
rcpath.write_text('default.timezone: UTC', encoding='utf-8')
202+
rcpath.write_text('timezone: UTC', encoding='utf-8')
203203
with mock.patch('locale.getpreferredencoding', return_value='UTF-32-BE'):
204204
rc = mpl.rc_params_from_file(rcpath, True, False)
205205
assert rc.get('timezone') == 'UTC'

lib/matplotlib/tests/test_widgets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ def test_CheckButtons(ax):
10291029
@pytest.mark.parametrize("toolbar", ["none", "toolbar2", "toolmanager"])
10301030
def test_TextBox(ax, toolbar):
10311031
# Avoid "toolmanager is provisional" warning.
1032-
plt.rcParams._set("default.toolbar", toolbar)
1032+
plt.rcParams._set("toolbar", toolbar)
10331033

10341034
submit_event = mock.Mock(spec=noop, return_value=None)
10351035
text_change_event = mock.Mock(spec=noop, return_value=None)

0 commit comments

Comments
 (0)