Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MNT: Add provisional get_backend(resolve=False) flag #29039

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
Oct 30, 2024

Conversation

timhoffm
Copy link
Member

@timhoffm timhoffm commented Oct 29, 2024

The default is resolve=True for now, so that it is completely backward-compatible.

The provisional introduction anticipates planned changes for the backend resolution (#26406 (comment)).

If all plays out as intended, this prolongs the range of releases for the migration: If we start deprecating rcParams._get("backend") say in 3.11, people can immediately switch to get_backend(resolve=False) and their code still runs on 3.10 without version gating.
The worst that can happen is that the introduced flag was not helpful and we remove it again, which is easy because it's provisional.

On a side-note: This also helps with a possible future default change from resolve=True to resolve=False. By making the flag available, users can choose the behavior explicitly, again without (or with lesser version gating) if they don't want to be affected by a default change.

@timhoffm timhoffm added this to the v3.10.0 milestone Oct 29, 2024
@timhoffm timhoffm force-pushed the get_backend branch 6 times, most recently from 25482d5 to 9640a95 Compare October 29, 2024 16:38
@@ -1296,15 +1296,37 @@ def use(backend, *, force=True):
rcParams['backend'] = os.environ.get('MPLBACKEND')


def get_backend():
def get_backend(*, resolve=True):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To bikeshed the name a bit, auto_resolve or force_resolution? "resolve" is not fully obvious to me that this means "should I try to guess the best available back end or not".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been thinking on the name a bit. How do we complete the sentence "If no backend has been selected yet, ..."?

  • "trigger backend resolution" / "resolve the backend": this is the "resolve" namespace. I feel "auto" and "force" do not add information here - "automatically resolve the backend", "force backend resolution" both basically state "do the resolution", there's nothing "automatic" about it other than it selects what's available, but that's already the resolution, there's also no "force" it's just the operation to be done.
    Maybe the "resolution" terminology is a bit heavy?
  • "auto-select a suitable backend": get_backend(auto_select=True)?
  • "activate a suitable backend": get_backend(activate=True)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like auto_select the best as it also implies that "one was not set, we did not pick, therefor you get back None"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "resolve" makes sense here? get_backend means "tell me what backend I'm using". "resolve" means choose one so it is not unresolved. That this is the default behaviour of get_backend is the design decision we are trying to reverse?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That this is the default behaviour of get_backend is the design decision we are trying to reverse?

This is not a complete sentence (or rather 1.5 sentences 😄). Not sure I understand what you're asking, but I'll try to answer anyway. The first goal is to provide a high-level API for "get the current backend, but don't resolve if there is no current backend". One currently has to rely on the semi-public rcParams._get('backend'), but #26406 (comment) plans to get away from handling backend logic in rcParams. Therefore, get_backend(resolve=False) will be the replacement for rcParams._get('backend').
Whether or not the default should eventually change or not is a separate discussion that can be had later. It does not interfere with this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point was just that "resolve" makes sense as the kwarg.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still collecting opinions on the parameter name. Prime candidates are resolve and auto_select.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the argument against resolve is that unless you know the jargon we use about the auto-select backend, it is not clear what "get the backend, but do not resolve" means, where as "get the backend, but do not run the auto-select logic" is relatively clear.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tacaswell that convinces me. While resolve is slightly more concise, auto_select is easier to understand for people without prior knowledge. -> Changed to auto_select.

@timhoffm timhoffm force-pushed the get_backend branch 2 times, most recently from fcdf522 to d29e8b5 Compare October 29, 2024 21:28
The default is `resolve=True` for now, so that this introduction
is completely backward-compatible.

The provisional introduction anticipates planned changes for the
backend resolution (matplotlib#26406 (comment)).

If all plays out as intended, this prolongs the range of releases
for the migration: If we start deprecating `rcParams._get("backend")` say in 3.11, people can immediately switch to
`get_backend(resolve=False)` and their code still runs on 3.10
without version gating.
The worst that can happen is that the introduced flag was not
helpful and we remove it again, which is easy because it's provisional.
@tacaswell tacaswell merged commit 218a42b into matplotlib:main Oct 30, 2024
43 checks passed
@timhoffm timhoffm deleted the get_backend branch October 30, 2024 15:46
@timhoffm
Copy link
Member Author

Note: I've intentionally not added a what's new note. People can start using this, but it's not my ambition to already push them this way by advertising the new API. As written above, this is mainly in 3.10 so that there are less version-gating issues when the API gets formally introduced in 3.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants