Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a0556f

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 a025315 commit 5a0556f

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
@@ -770,12 +770,12 @@ def __setitem__(self, key, val):
770770
return
771771
elif key == 'backend' or key == "default.backend":
772772
if val is rcsetup._auto_backend_sentinel:
773-
if 'backend' in self:
773+
if 'backend' in self or 'default.backend' in self:
774774
return
775-
if key in self.single_key_set:
776-
key = f"default.{key}"
777775
try:
778776
cval = self.validate[key](val)
777+
if key in self.single_key_set:
778+
key = f"default.{key}"
779779
except ValueError as ve:
780780
raise ValueError(f"Key {key}: {ve}") from None
781781
self._set(key, cval)
@@ -840,7 +840,7 @@ def __contains__(self, key):
840840
def __iter__(self):
841841
"""Yield from sorted list of keys"""
842842
keys = (
843-
".".join((space, key))
843+
".".join((space, key)) if space != 'default' else key
844844
for space, mapping in self._namespace_maps.items()
845845
for key in mapping.keys()
846846
)
@@ -1012,8 +1012,6 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
10121012
config = RcParams()
10131013

10141014
for key, (val, line, line_no) in rc_temp.items():
1015-
if key in config.single_key_set:
1016-
key = f"default.{key}"
10171015
if key in rcsetup._validators:
10181016
if fail_on_error:
10191017
config[key] = val # try to convert to proper type or raise
@@ -1090,23 +1088,22 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
10901088
# in that case. However, packagers can set a different default backend
10911089
# (resulting in a normal `#backend: foo` line) in which case we should *not*
10921090
# fill in _auto_backend_sentinel.
1093-
rcParamsDefault.setdefault("default.backend", rcsetup._auto_backend_sentinel)
1094-
params_dict = RcParams()
1095-
params_dict.update(rcParamsDefault.items())
1096-
params_dict.update(_rc_params_in_file(matplotlib_fname()))
1097-
rcParamsOrig = params_dict.copy()
1091+
rcParamsDefault.setdefault("backend", rcsetup._auto_backend_sentinel)
1092+
rcParams = RcParams()
1093+
rcParams.update(rcParamsDefault.items())
1094+
rcParams.update(_rc_params_in_file(matplotlib_fname()))
1095+
rcParamsOrig = rcParams.copy()
10981096
with _api.suppress_matplotlib_deprecation_warning():
10991097
# This also checks that all rcParams are indeed listed in the template.
11001098
# Assigning to rcsetup.defaultParams is left only for backcompat.
11011099
defaultParams = rcsetup.defaultParams = {
11021100
# We want to resolve deprecated rcParams, but not backend...
1103-
key: [(rcsetup._auto_backend_sentinel if key == "default.backend" else
1101+
key: [(rcsetup._auto_backend_sentinel if key == "backend" else
11041102
rcParamsDefault[key]),
11051103
validator]
11061104
for key, validator in rcsetup._validators.items()}
1107-
if params_dict['axes.formatter.use_locale']:
1105+
if rcParams['axes.formatter.use_locale']:
11081106
locale.setlocale(locale.LC_ALL, '')
1109-
rcParams = RcParams(params_dict)
11101107

11111108

11121109
def rc(group, **kwargs):
@@ -1382,14 +1379,14 @@ def use(backend, *, force=True):
13821379
# value which will be respected when the user finally imports
13831380
# pyplot
13841381
else:
1385-
rcParams['default.backend'] = backend
1382+
rcParams['backend'] = backend
13861383
# if the user has asked for a given backend, do not helpfully
13871384
# fallback
1388-
rcParams['default.backend_fallback'] = False
1385+
rcParams['backend_fallback'] = False
13891386

13901387

13911388
if os.environ.get('MPLBACKEND'):
1392-
rcParams['default.backend'] = os.environ.get('MPLBACKEND')
1389+
rcParams['backend'] = os.environ.get('MPLBACKEND')
13931390

13941391

13951392
def get_backend():
@@ -1407,7 +1404,7 @@ def interactive(b):
14071404
"""
14081405
Set whether to redraw after every plotting command (e.g. `.pyplot.xlabel`).
14091406
"""
1410-
rcParams['default.interactive'] = b
1407+
rcParams['interactive'] = b
14111408

14121409

14131410
def is_interactive():
@@ -1419,7 +1416,7 @@ def is_interactive():
14191416
This function is only intended for use in backends. End users should
14201417
use `.pyplot.isinteractive` instead.
14211418
"""
1422-
return rcParams['default.interactive']
1419+
return rcParams['interactive']
14231420

14241421

14251422
def _init_tests():

lib/matplotlib/pyplot.py

Lines changed: 6 additions & 6 deletions
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(
@@ -2378,11 +2378,11 @@ def polar(*args, **kwargs) -> list[Line2D]:
23782378
# If rcParams['backend_fallback'] is true, and an interactive backend is
23792379
# requested, ignore rcParams['backend'] and force selection of a backend that
23802380
# is compatible with the current running interactive framework.
2381-
if (rcParams["default.backend_fallback"]
2381+
if (rcParams["backend_fallback"]
23822382
and rcParams._get_backend_or_none() in ( # type: ignore
23832383
set(_interactive_bk) - {'WebAgg', 'nbAgg'})
23842384
and cbook._get_running_interactive_framework()): # type: ignore
2385-
rcParams._set("default.backend", rcsetup._auto_backend_sentinel) # type: ignore
2385+
rcParams._set("backend", rcsetup._auto_backend_sentinel) # type: ignore
23862386

23872387
# fmt: on
23882388

lib/matplotlib/rcsetup.py

Lines changed: 5 additions & 5 deletions
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 matplotlibrc.template, which gets copied
812812
# 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

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
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ def test_CheckButtons(ax):
10121012
@pytest.mark.parametrize("toolbar", ["none", "toolbar2", "toolmanager"])
10131013
def test_TextBox(ax, toolbar):
10141014
# Avoid "toolmanager is provisional" warning.
1015-
plt.rcParams._set("default.toolbar", toolbar)
1015+
plt.rcParams._set("toolbar", toolbar)
10161016

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

0 commit comments

Comments
 (0)