Skip to content

Make rcParams.copy() return a new RcParams instance. #21042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/behavior/21042-AL.rst
Original file line number Diff line number Diff line change
@@ -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).
7 changes: 5 additions & 2 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 = {
Expand Down
5 changes: 5 additions & 0 deletions lib/matplotlib/tests/test_rcparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.