Skip to content

Commit 67735b0

Browse files
committed
Add way to access default params
1 parent c101fec commit 67735b0

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

lib/matplotlib/__init__.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -681,17 +681,22 @@ class RcParams(MutableMapping):
681681
def __init__(self, *args, **kwargs):
682682
self._namespace_maps = {name: ChainMap({}) for name in self.namespaces}
683683
self.update(*args, **kwargs)
684-
self._namespace_maps = {
685-
name: mapping.new_child()
686-
for name, mapping in self._namespace_maps.items()
687-
}
684+
self._new_child()
688685

689686
@staticmethod
690687
@functools.lru_cache
691688
def _split_key(key, sep="."):
692689
keys = key.split(sep, maxsplit=1)
693690
return keys, len(keys)
694691

692+
def _new_child(self):
693+
for space in self._namespace_maps.keys():
694+
self._namespace_maps[space] = self._namespace_maps[space].new_child()
695+
696+
def _parents(self):
697+
for space in self._namespace_maps.keys():
698+
self._namespace_maps[space] = self._namespace_maps[space].parents
699+
695700
def _set(self, key, val):
696701
"""
697702
Directly write data bypassing deprecation and validation logic.
@@ -818,6 +823,36 @@ def __getitem__(self, key):
818823

819824
return self._get(key)
820825

826+
def _get_default(self, key):
827+
keys, depth = self._split_key(key)
828+
if depth == 1:
829+
if key in self.single_key_set:
830+
return self._namespace_maps["default"].get(key)
831+
# Uncomment the following line and remove the raise statement
832+
# to enable getting namespace parameters.
833+
# return self._namespace_maps[key]
834+
else:
835+
raise KeyError(
836+
f"{key} is not a valid rc parameter (see rcParams.keys() for "
837+
f"a list of valid parameters)")
838+
elif depth == 2:
839+
return self._namespace_maps[keys[0]].maps[-1].get(keys[1])
840+
841+
def getdefault(self, key):
842+
if key in _deprecated_map:
843+
version, alt_key, alt_val, inverse_alt = _deprecated_map[key]
844+
_api.warn_deprecated(
845+
version, name=key, obj_type="rcparam", alternative=alt_key)
846+
return inverse_alt(self._get(alt_key))
847+
848+
elif key in _deprecated_ignore_map:
849+
version, alt_key = _deprecated_ignore_map[key]
850+
_api.warn_deprecated(
851+
version, name=key, obj_type="rcparam", alternative=alt_key)
852+
return self._get_default(alt_key) if alt_key else None
853+
854+
return self._get_default(key)
855+
821856
def _get_backend_or_none(self):
822857
"""Get the requested backend, if any, without triggering resolution."""
823858
backend = self._get("backend")
@@ -1021,6 +1056,7 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
10211056
raise
10221057

10231058
config = RcParams()
1059+
config._parents()
10241060

10251061
for key, (val, line, line_no) in rc_temp.items():
10261062
if key in rcsetup._validators:
@@ -1049,6 +1085,7 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
10491085
or from the matplotlib source distribution""",
10501086
dict(key=key, fname=fname, line_no=line_no,
10511087
line=line.rstrip('\n'), version=version))
1088+
config._new_child()
10521089
return config
10531090

10541091

@@ -1101,6 +1138,7 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
11011138
# fill in _auto_backend_sentinel.
11021139
rcParamsDefault.setdefault("backend", rcsetup._auto_backend_sentinel)
11031140
rcParams = RcParams()
1141+
rcParams._parents()
11041142
rcParams.update(rcParamsDefault.items())
11051143
rcParams.update(_rc_params_in_file(matplotlib_fname()))
11061144
rcParamsOrig = rcParams.copy()
@@ -1115,6 +1153,7 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
11151153
for key, validator in rcsetup._validators.items()}
11161154
if rcParams['axes.formatter.use_locale']:
11171155
locale.setlocale(locale.LC_ALL, '')
1156+
rcParams._new_child()
11181157

11191158

11201159
def rc(group, **kwargs):
@@ -1318,10 +1357,7 @@ def rc_context(rc=None, fname=None):
13181357
finally:
13191358
# Revert to the original rcs.
13201359
backend = rcParams["backend"]
1321-
for space in rcParams._namespace_maps.keys():
1322-
rcParams._namespace_maps[space] = rcParams._namespace_maps[
1323-
space
1324-
].parents
1360+
rcParams._parents()
13251361
rcParams["backend"] = backend
13261362

13271363

lib/matplotlib/tests/test_rcparams.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,3 +652,8 @@ def test_rcparams_path_sketch_from_file(tmpdir, value):
652652
rc_path.write(f"path.sketch: {value}")
653653
with mpl.rc_context(fname=rc_path):
654654
assert mpl.rcParams["path.sketch"] == (1, 2, 3)
655+
656+
657+
def test_rcparams_getdefault():
658+
with mpl.rc_context({"image.lut": 128}):
659+
assert mpl.rcParams.getdefault("image.lut") == 256

0 commit comments

Comments
 (0)