Skip to content

Commit 5bf9811

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 8a8dd90 commit 5bf9811

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
@@ -699,7 +699,10 @@ def find_all(self, pattern):
699699
if pattern_re.search(key))
700700

701701
def copy(self):
702-
return {k: dict.__getitem__(self, k) for k in self}
702+
rccopy = RcParams()
703+
for k in self: # Skip deprecations and revalidation.
704+
dict.__setitem__(rccopy, k, dict.__getitem__(self, k))
705+
return rccopy
703706

704707

705708
def rc_params(fail_on_error=False):
@@ -877,8 +880,8 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
877880
rcParams = RcParams() # The global instance.
878881
dict.update(rcParams, dict.items(rcParamsDefault))
879882
dict.update(rcParams, _rc_params_in_file(matplotlib_fname()))
883+
rcParamsOrig = rcParams.copy()
880884
with _api.suppress_matplotlib_deprecation_warning():
881-
rcParamsOrig = RcParams(rcParams.copy())
882885
# This also checks that all rcParams are indeed listed in the template.
883886
# Assigning to rcsetup.defaultParams is left only for backcompat.
884887
defaultParams = rcsetup.defaultParams = {

lib/matplotlib/tests/test_rcparams.py

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

0 commit comments

Comments
 (0)