Skip to content

MNT: Remove internal usage of the 'backend' key in rcParams - #32495

Open
timhoffm wants to merge 1 commit into
matplotlib:mainfrom
timhoffm:rcparams-remove-backend-key-usage
Open

timhoffm wants to merge 1 commit into
matplotlib:mainfrom
timhoffm:rcparams-remove-backend-key-usage

Conversation

@timhoffm

@timhoffm timhoffm commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

PR summary

Working towards #31791.

This removes most internal usages of the 'backend' key (some usage in tests is still in place because I have to decide there whether we still want to test the key API).

It's conceptually a schematic replacement

  • rcParams._get('backend') -> rcParams._backend - obtain raw backend value
  • x = rcParams['backend'] -> x = rcParams._get_backend_resolved() - obtain resoved backend
  • rcParams['backend'] = x -> rcParams._set_backend(x) - store the value. Note that this write has no specific logic attatched. It's only be used to store the new backend value. We nevertheless use the class method becuase in some existing contexts we have an rcParams instance, in some we only have the RcParams class. The method can be called on both.

Note: There have been some places writing rcParamsDefault['backend'] and rcParamsOrig['backend']. These were needed to synchronize and persist backend information even after resetting rcParams. That's not needed anymore and thus removed now that the backend information only exists once as class attribute on RcParams.

Note2: In theory it would even be better to use RcParams._backend rather than access via instance rcParams._backend backend. I've not done that for now, because it would complicate the PR. We may do this in a followup.

AI Disclosure

No AI.

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • N/A Plotting related features are demonstrated in an example
  • N/A New features and API changes have release notes
  • N/A Documentation complies with general and docstring guidelines

@timhoffm
timhoffm force-pushed the rcparams-remove-backend-key-usage branch 3 times, most recently from 271de55 to 9409506 Compare October 17, 2026 08:36
@timhoffm
timhoffm marked this pull request as draft October 18, 2026 02:29
@timhoffm
timhoffm force-pushed the rcparams-remove-backend-key-usage branch from 9409506 to 16b40f1 Compare October 18, 2026 20:17
@timhoffm
timhoffm force-pushed the rcparams-remove-backend-key-usage branch 3 times, most recently from e6dad3d to e9a85a1 Compare November 5, 2026 00:51
We use an internal helper class method RcParams._set_backend()
to make it easy to set backends in contexts where we do and
do not have an rcParams instance at hand.
@timhoffm
timhoffm force-pushed the rcparams-remove-backend-key-usage branch from e9a85a1 to 01976cc Compare November 5, 2026 07:50
@timhoffm
timhoffm marked this pull request as ready for review November 5, 2026 08:48
@jklymak

jklymak commented Nov 19, 2026

Copy link
Copy Markdown
Member

x = rcParams._get_backend_resolved()

I'm not clear why this new private function is on rcParams? Are we still carrying backend state on rcParams? Is that where we think we should keep this info longterm?

@timhoffm

Copy link
Copy Markdown
Member Author

x = rcParams._get_backend_resolved()

I'm not clear why this new private function is on rcParams? Are we still carrying backend state on rcParams?

Yes, as an intermediate solution, the backend state is held in the class variable RcParams._backend. The issue #31791 describes the overall goal and why we go there in multiple steps.

Is that where we think we should keep this info longterm?

No. See step (4) in the issue.

@jklymak

jklymak commented Nov 20, 2026

Copy link
Copy Markdown
Member

Can we decide and move to the permanent location first so people can start using it, and put a temporary shim in rcParams?

@timhoffm

timhoffm commented Nov 20, 2026 •

Copy link
Copy Markdown
Member Author

What exactly do you mean by“the permanent location, that people should use”? There are a couple of aspects to it:

  • The storage location is an implementation detail - we can choose whether that’s RcParams._backend or matplotlib._backend or pyplot._backend at any time, and which one mainly depends on where it’s most convenient for implementation. This is not user-facing.

  • The public API is currently

    The rcParams[“backend”] API will be deprecated. Prerequisites for that are

    1. that we internally don’t use that API anymore, because filtering out deprecation warnings for internal use has proven too difficult (MNT: Deprecate rcParams._get("backend") #30963) - that’s what this PR is about
    2. Be clear what the public API should be. - Canonically, that would be the existing functions, but you could argue for a new different API if that makes sense.

    Anyway 1 and 2 are independent, so I don’t understand why we need to do something other than this PR first.

@jklymak

jklymak commented Nov 20, 2026

Copy link
Copy Markdown
Member

OK I see, so the public API that we want people to use is already there, so we are just discussing where that info should live.

It still seems strange to create a new local private function on rcParams for this, particularly if there is a public API. But no doubt I am not following.

@timhoffm

timhoffm commented Nov 20, 2026 •

Copy link
Copy Markdown
Member Author

I eat the elephant one bite at a time. This one is replacing all internal rcParams shenanigans by a clear semantic internal
API.

Note e.g. that

    if auto_select:
        return rcParams['backend']
    else:
        backend = rcParams._get('backend')
        if backend is rcsetup._auto_backend_sentinel:
            return None
        else:
            return backend

is now much more concisely written as

    if auto_select:
        return rcParams._get_backend_resolved()
    else:
        return rcParams._get_backend_or_none()

How that relates to the existing public API is a later bite - could be that we can just use the public API and throw away the internal API, or we could want to change the existing public API (matplotlib.use, matplotlib.get_backend, pyplot.switch_backend is not the most consistent design either), or we might want to keep both an internal API and a different public API (could be that there is functionality/behavior that we want internally but not on the Public API - the sentinel is currently such a thing, but maybe not essential). These are things that need careful consideration and that I don't want to fold into cleaning up the internal logic.

@timhoffm timhoffm added this to the v3.12.0 milestone Nov 21, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants