GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — MNT: Move "backend" from a regular rcParams dict entry to a class attribute by timhoffm · Pull Request #31792 · matplotlib/matplotlib

Conversation

@timhoffm

@timhoffm timhoffm commented May 31, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

PR summary

Working towards #31791.

rcParams should be only configuration, not state; i.e. wrt. backend, it
can hold the desired backend and backend priorities, but it should not
hold the currently active backend or trigger backend selection as a
side-effect. The latter causes a lot of complexity in rcParams.

This PR is the first step in the migration strategy outlined there:

Make the backend a RcParams class attribute and do not hold
"backend" as a regular dict entry. There can only be one backend,
so putting that value in a class attribute RcParams._backend makes
sense. This already reduces the complexity as we don't need special
handling to sync/copy the value when rcParams are bulk-replaced
(loading from file or rc_context). Instead add special logic to keep
behavioral backward compatibility with the dict API
(rcParams["backend"] / rcParams._get("backend") /
"rcParams._set("backend", val)).

Note on API compatibility: This PR limits itself to the known typical dict-API usages in the context of backend handling. I've refrained from trying to fully replicate dict behavior of a "virtual" "backend" parameter, i.e. len(rcParams) / rcParams.keys() / rcParams.values() / rcParams.items() do not contain "backend" anymore. I would be willing to accept this level of API breakage. If deemed critical, we could add compatibility shims for that too, but it's a bit more complicated as the style variants of RcParams must not claim to have "backend" as a key.

AI Disclosure

AI was used to analyze the code base and draft an implementation proposal. I've thoroughly reviewed and modified the result to the present form.

PR checklist

  • new and changed code is tested
  • New Features and API Changes are noted with a directive and release note

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@timhoffm timhoffm mentioned this pull request May 31, 2026
[MNT]: Remove backend state handling from rcParams #31791
Open
1 of 4 tasks
@timhoffm timhoffm added this to the v3.12.0 milestone May 31, 2026
@timhoffm
timhoffm force-pushed the mnt-rcParams-backend-class-attribute branch 3 times, most recently from 85159ce to 8b8563d Compare May 31, 2026 11:56
timhoffm
timhoffm commented May 31, 2026
View reviewed changes
Comment thread lib/matplotlib/__init__.py
Comment on lines -758 to -768
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)

@timhoffm timhoffm May 31, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

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 attribute RcParams._backend and thus always available.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
timhoffm
timhoffm commented May 31, 2026
View reviewed changes
Comment thread lib/matplotlib/__init__.py
Comment on lines -1204 to -1205
orig = dict(rcParams.copy())
del orig['backend']

@timhoffm timhoffm May 31, 2026

Copy link
Copy Markdown
Member Author

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: 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@melissawm melissawm added the Maintenance label Jun 17, 2026
@github-actions github-actions Bot added the status: needs rebase label Jun 25, 2026
@github-actions github-actions Bot removed the status: needs rebase label Jul 29, 2026
@timhoffm
timhoffm force-pushed the mnt-rcParams-backend-class-attribute branch from 9bd416e to 540d1a4 Compare July 29, 2026 22:02
@iccir
iccir self-requested a review October 15, 2026 19:57
QuLogic
QuLogic approved these changes Oct 15, 2026
View reviewed changes
Comment thread lib/matplotlib/__init__.py Outdated
Comment on lines +805 to +806
return RcParams._backend
return dict.__getitem__(self, key)

@QuLogic QuLogic Oct 15, 2026

Copy link
Copy Markdown
Member

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.

I suppose these two lines don't need to change, as _get does handle this case. It's a tiny bit more optimized this way, but I'm torn on whether to preserve the symmetry with __setitem__ ending with _set.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@timhoffm timhoffm Oct 16, 2026

Copy link
Copy Markdown
Member Author

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.

_get is the semi-public way to retrieve bare rcParams values without triggering any logic. It handles "backend" for backward-compatibility, but we will deprecate _get("backend") since "backend" will not be a regular rcParam entry. As such we should internally not rely on _get("backend") but always fetch the value from it's actual source RcParams._backend.

For the second line, yes let's semantically rely on _get here. One can argue either way : __getitem__ and _get being both the interface to the internal data structure - with and without checks. Or one can argue that _get is the only interface to the internal datastructure and __getitem__ builds logic on top of that.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@timhoffm
MNT: Move "backend" from a regular rcParams dict entry to a class att…
df49f80
…ribute

Working towards matplotlib#31791.

> rcParams should be only configuration, not state; i.e. wrt. backend, it
> can hold the desired backend and backend priorities, but it should not
> hold the currently active backend or trigger backend selection as a
> side-effect. The latter causes a lot of complexity in rcParams.

This PR is the first step in the migration strategy outlined there:

> Make the backend a RcParams class attribute and do not hold
> "backend" as a regular dict entry. There can only be one backend,
> so putting that value in a class attribute `RcParams._backend` makes
> sense. This already reduces the complexity as we don't need special
> handling to sync/copy the value when rcParams are bulk-replaced
> (loading from file or rc_context). Instead add special logic to keep
> behavioral backward compatibility with the dict API
> (`rcParams["backend"]` / `rcParams._get("backend")` /
> `"rcParams._set("backend", val)`).

Note on API compatibility: This PR limits itself to the known typical
dict-API usages in the context of backend handling. I've refrained from
trying to fully replicate dict behavior of a "virtual" "backend" parameter,
i.e. `len(rcParams)` / `rcParams.keys()` / `rcParams.values()` /
`rcParams.items()` do not contain "backend" anymore. I would be willing to
accept this level of API breakage. If deemed critical, we could add
compatibility shims for that too.
@timhoffm
timhoffm force-pushed the mnt-rcParams-backend-class-attribute branch from 540d1a4 to df49f80 Compare October 16, 2026 07:01
tacaswell
tacaswell approved these changes Oct 16, 2026
View reviewed changes
@tacaswell
tacaswell merged commit ed57abc into matplotlib:main Oct 16, 2026
40 of 41 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@timhoffm
timhoffm deleted the mnt-rcParams-backend-class-attribute branch October 17, 2026 04:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tacaswell tacaswell tacaswell approved these changes
@QuLogic QuLogic QuLogic approved these changes
@iccir iccir Awaiting requested review from iccir

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Maintenance

Projects

None yet

Milestone

v3.12.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@timhoffm @tacaswell @QuLogic @melissawm
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 are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.