Skip to content

Commit a35921c

Browse files
authored
Merge pull request #21042 from anntzer/rccopy2
Make rcParams.copy() return a new RcParams instance.
2 parents 70957d8 + 5bf9811 commit a35921c

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
rcParams.copy() returns a new RcParams instance, rather than a dict
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
This makes the copied instance still validate inputs, and additionally avoids
4+
emitting deprecation warnings when using a previously copied RcParams instance
5+
to update the global instance (even if some entries are deprecated).

lib/matplotlib/__init__.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,10 @@ def find_all(self, pattern):
708708
if pattern_re.search(key))
709709

710710
def copy(self):
711-
return {k: dict.__getitem__(self, k) for k in self}
711+
rccopy = RcParams()
712+
for k in self: # Skip deprecations and revalidation.
713+
dict.__setitem__(rccopy, k, dict.__getitem__(self, k))
714+
return rccopy
712715

713716

714717
def rc_params(fail_on_error=False):
@@ -886,8 +889,8 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
886889
rcParams = RcParams() # The global instance.
887890
dict.update(rcParams, dict.items(rcParamsDefault))
888891
dict.update(rcParams, _rc_params_in_file(matplotlib_fname()))
892+
rcParamsOrig = rcParams.copy()
889893
with _api.suppress_matplotlib_deprecation_warning():
890-
rcParamsOrig = RcParams(rcParams.copy())
891894
# This also checks that all rcParams are indeed listed in the template.
892895
# Assigning to rcsetup.defaultParams is left only for backcompat.
893896
defaultParams = rcsetup.defaultParams = {

lib/matplotlib/tests/test_rcparams.py

+5
Original file line numberDiff line numberDiff line change
@@ -557,3 +557,8 @@ def test_deprecation(monkeypatch):
557557
mpl.rcParams["svg.hashsalt"] = "foobar"
558558
assert mpl.rcParams["svg.hashsalt"] == "foobar" # Doesn't warn.
559559
mpl.rcParams["svg.hashsalt"] = None # Doesn't warn.
560+
561+
mpl.rcParams.update(mpl.rcParams.copy()) # Doesn't warn.
562+
# Note that the warning suppression actually arises from the
563+
# iteration over the updater rcParams being protected by
564+
# suppress_matplotlib_deprecation_warning, rather than any explicit check.

0 commit comments

Comments
 (0)