Skip to content
Navigation Menu
{{ message }}
-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
MNT: Move "backend" from a regular rcParams dict entry to a class attribute #31792
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
Open
timhoffm
wants to merge
1
commit into
matplotlib:main
Choose a base branch
from
timhoffm:mnt-rcParams-backend-class-attribute
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+51
−27
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -693,6 +693,12 @@ class RcParams(MutableMapping, dict): | |
|
|
||
| validate = rcsetup._validators | ||
|
|
||
| # Class-level backend state: shared across all RcParams instances because | ||
| # there can only be one active backend at a time. | ||
| # rcParams["backend"] remains valid API for now, but stores the values here | ||
| # and not as regular key in the underlying dict. | ||
| _backend = rcsetup._auto_backend_sentinel | ||
|
|
||
| # validate values on the way in | ||
| def __init__(self, *args, **kwargs): | ||
| self.update(*args, **kwargs) | ||
|
|
@@ -715,6 +721,9 @@ def _set(self, key, val): | |
|
|
||
| :meta public: | ||
| """ | ||
| if key == "backend": | ||
| RcParams._backend = val | ||
| return | ||
| dict.__setitem__(self, key, val) | ||
|
|
||
| def _get(self, key): | ||
|
|
@@ -736,6 +745,8 @@ def _get(self, key): | |
|
|
||
| :meta public: | ||
| """ | ||
| if key == "backend": | ||
| return RcParams._backend | ||
| return dict.__getitem__(self, key) | ||
|
|
||
| def _update_raw(self, other_params): | ||
|
|
@@ -753,24 +764,24 @@ def _update_raw(self, other_params): | |
| """ | ||
| if isinstance(other_params, RcParams): | ||
| other_params = dict.items(other_params) | ||
| else: | ||
| if "backend" in other_params: | ||
| # should not happen because we aim to not use "backend" as a regular | ||
| # key anymore, but keep to ensure we have not overlooked a code path. | ||
| raise RuntimeError("'backend' must not be passed to _update_raw()") | ||
| dict.update(self, other_params) | ||
|
|
||
| def _ensure_has_backend(self): | ||
| """ | ||
| Ensure that a "backend" entry exists. | ||
|
|
||
| Normally, the default matplotlibrc file contains *no* entry for "backend" (the | ||
| corresponding line starts with ##, not #; we fill in _auto_backend_sentinel | ||
| in that case. However, packagers can set a different default backend | ||
| (resulting in a normal `#backend: foo` line) in which case we should *not* | ||
| fill in _auto_backend_sentinel. | ||
| """ | ||
| dict.setdefault(self, "backend", rcsetup._auto_backend_sentinel) | ||
|
|
||
| def __setitem__(self, key, val): | ||
| if (key == "backend" | ||
| and val is rcsetup._auto_backend_sentinel | ||
| and "backend" in self): | ||
| if (key == "backend" and val is rcsetup._auto_backend_sentinel): | ||
| # Don't let caller silently overwrite a real backend with the auto-sentinel | ||
| # (only internal code via _set() may do that). | ||
| # | ||
| # The primary reason for existence was covering internal rcParams logic | ||
| # since end-users do not have direct access to | ||
| # rcsetup._auto_backend_sentinel. It is likely that this is not needed | ||
| # anymore due to removing "backend" from the dict and making it | ||
| # a class attribute RcParams._backend`. But to be on the safe side, we | ||
| # keep this as long as "backend" is a valid key for rcParams. | ||
| return | ||
| valid_key = _api.getitem_checked( | ||
| self.validate, rcParam=key, _error_cls=KeyError | ||
|
|
@@ -784,18 +795,19 @@ def __setitem__(self, key, val): | |
| self._set(key, cval) | ||
|
|
||
| def __getitem__(self, key): | ||
| # In theory, this should only ever be used after the global rcParams | ||
| # has been set up, but better be safe e.g. in presence of breakpoints. | ||
| if key == "backend" and self is globals().get("rcParams"): | ||
| val = self._get(key) | ||
| if val is rcsetup._auto_backend_sentinel: | ||
| if key == "backend": | ||
| # In theory, this should only ever be used after the global rcParams | ||
| # has been set up, but better be safe e.g. in presence of breakpoints. | ||
| if (self is globals().get("rcParams") | ||
| and RcParams._backend is rcsetup._auto_backend_sentinel): | ||
| from matplotlib import pyplot as plt | ||
| plt.switch_backend(rcsetup._auto_backend_sentinel) | ||
| return self._get(key) | ||
| return RcParams._backend | ||
| return dict.__getitem__(self, key) | ||
|
|
||
| def _get_backend_or_none(self): | ||
| """Get the requested backend, if any, without triggering resolution.""" | ||
| backend = self._get("backend") | ||
| backend = RcParams._backend | ||
| return None if backend is rcsetup._auto_backend_sentinel else backend | ||
|
|
||
| def __repr__(self): | ||
|
|
@@ -992,7 +1004,6 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True): | |
| transform=lambda line: line[1:] if line.startswith("#") else line, | ||
| fail_on_error=True) | ||
| rcParamsDefault._update_raw(rcsetup._hardcoded_defaults) | ||
| rcParamsDefault._ensure_has_backend() | ||
|
|
||
| rcParams = RcParams() # The global instance. | ||
| rcParams._update_raw(rcParamsDefault) | ||
|
|
@@ -1201,8 +1212,7 @@ def rc_context(rc=None, fname=None): | |
| plt.plot(x, y) | ||
|
|
||
| """ | ||
| orig = dict(rcParams.copy()) | ||
| del orig['backend'] | ||
|
Comment on lines
-1204
to
-1205
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: The dict conversion was only needed to be able to drop the backend entry. Since that doesn't exist anymore, we don't need the dict conversion. |
||
| orig = rcParams.copy() | ||
| try: | ||
| if fname: | ||
| rc_file(fname) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
You can’t perform that action at this time.

Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note:
_ensure_has_backend()is not needed anymore, because the backend is stored in the class attributeRcParams._backendand thus always available.