From 5bf9811715c793e1a35120074c040dd2e8d71cbc Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 10 Sep 2021 01:03:53 +0200 Subject: [PATCH] Make rcParams.copy() return a new RcParams instance. ... so that `rcParams.update()` doesn't emit DeprecationWarnings even if some entries are deprecated. (See comment in tests.) --- doc/api/next_api_changes/behavior/21042-AL.rst | 5 +++++ lib/matplotlib/__init__.py | 7 +++++-- lib/matplotlib/tests/test_rcparams.py | 5 +++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 doc/api/next_api_changes/behavior/21042-AL.rst diff --git a/doc/api/next_api_changes/behavior/21042-AL.rst b/doc/api/next_api_changes/behavior/21042-AL.rst new file mode 100644 index 000000000000..9edec6270ff0 --- /dev/null +++ b/doc/api/next_api_changes/behavior/21042-AL.rst @@ -0,0 +1,5 @@ +rcParams.copy() returns a new RcParams instance, rather than a dict +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +This makes the copied instance still validate inputs, and additionally avoids +emitting deprecation warnings when using a previously copied RcParams instance +to update the global instance (even if some entries are deprecated). diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 6f146b719751..76b51bc4931c 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -699,7 +699,10 @@ def find_all(self, pattern): if pattern_re.search(key)) def copy(self): - return {k: dict.__getitem__(self, k) for k in self} + rccopy = RcParams() + for k in self: # Skip deprecations and revalidation. + dict.__setitem__(rccopy, k, dict.__getitem__(self, k)) + return rccopy 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): rcParams = RcParams() # The global instance. dict.update(rcParams, dict.items(rcParamsDefault)) dict.update(rcParams, _rc_params_in_file(matplotlib_fname())) +rcParamsOrig = rcParams.copy() with _api.suppress_matplotlib_deprecation_warning(): - rcParamsOrig = RcParams(rcParams.copy()) # This also checks that all rcParams are indeed listed in the template. # Assigning to rcsetup.defaultParams is left only for backcompat. defaultParams = rcsetup.defaultParams = { diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index cb8bbc0b0338..b69eca2f78f7 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -546,3 +546,8 @@ def test_deprecation(monkeypatch): mpl.rcParams["svg.hashsalt"] = "foobar" assert mpl.rcParams["svg.hashsalt"] == "foobar" # Doesn't warn. mpl.rcParams["svg.hashsalt"] = None # Doesn't warn. + + mpl.rcParams.update(mpl.rcParams.copy()) # Doesn't warn. + # Note that the warning suppression actually arises from the + # iteration over the updater rcParams being protected by + # suppress_matplotlib_deprecation_warning, rather than any explicit check.