Skip to content

Commit 045c88a

Browse files
committed
Make rcParams.copy() return a new RcParams instance.
... so that `rcParams.update(<previously-copied-rcparams-instance>)` doesn't emit DeprecationWarnings even if some entries are deprecated. (See comment in tests.)
1 parent b9ea8ce commit 045c88a

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed
Lines changed: 5 additions & 0 deletions
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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,10 @@ def find_all(self, pattern):
697697
if pattern_re.search(key))
698698

699699
def copy(self):
700-
return {k: dict.__getitem__(self, k) for k in self}
700+
copy = RcParams()
701+
for k in self: # Skip deprecations and revalidation.
702+
dict.__setitem__(copy, k, dict.__getitem__(self, k))
703+
return copy
701704

702705

703706
def rc_params(fail_on_error=False):
@@ -875,8 +878,8 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
875878
rcParams = RcParams() # The global instance.
876879
dict.update(rcParams, dict.items(rcParamsDefault))
877880
dict.update(rcParams, _rc_params_in_file(matplotlib_fname()))
881+
rcParamsOrig = rcParams.copy()
878882
with _api.suppress_matplotlib_deprecation_warning():
879-
rcParamsOrig = RcParams(rcParams.copy())
880883
# This also checks that all rcParams are indeed listed in the template.
881884
# Assigning to rcsetup.defaultParams is left only for backcompat.
882885
defaultParams = rcsetup.defaultParams = {

lib/matplotlib/tests/test_rcparams.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,3 +544,8 @@ def test_deprecation(monkeypatch):
544544
mpl.rcParams["svg.hashsalt"] = "foobar"
545545
assert mpl.rcParams["svg.hashsalt"] == "foobar" # Doesn't warn.
546546
mpl.rcParams["svg.hashsalt"] = None # Doesn't warn.
547+
548+
mpl.rcParams.update(mpl.rcParams.copy()) # Doesn't warn.
549+
# Note that the warning suppression actually arises from the
550+
# iteration over the updater rcParams being protected by
551+
# suppress_matplotlib_deprecation_warning, rather than any explicit check.

0 commit comments

Comments
 (0)