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
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
26 changes: 24 additions & 2 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,15 +1296,37 @@
rcParams['backend'] = os.environ.get('MPLBACKEND')


def get_backend():
def get_backend(*, auto_select=True):
"""
Return the name of the current backend.

Parameters
----------
auto_select : bool, default: True
Whether to trigger backend resolution if no backend has been
selected so far. If True, this ensures that a valid backend
is returned. If False, this returns None if no backend has been
selected so far.

.. versionadded:: 3.10

.. admonition:: Provisional

The *auto_select* flag is provisional. It may be changed or removed
without prior warning.

See Also
--------
matplotlib.use
"""
return rcParams['backend']
if auto_select:
return rcParams['backend']
else:
backend = rcParams._get('backend')
if backend is rcsetup._auto_backend_sentinel:
return None
else:
return backend

Check warning on line 1329 in lib/matplotlib/__init__.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/__init__.py#L1329

Added line #L1329 was not covered by tests


def interactive(b):
Expand Down
7 changes: 5 additions & 2 deletions lib/matplotlib/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import contextlib
from packaging.version import Version

from matplotlib._api import MatplotlibDeprecationWarning
from typing import Any, NamedTuple
from typing import Any, Literal, NamedTuple, overload

class _VersionInfo(NamedTuple):
major: int
Expand Down Expand Up @@ -104,7 +104,10 @@ def rc_context(
rc: dict[str, Any] | None = ..., fname: str | Path | os.PathLike | None = ...
) -> Generator[None, None, None]: ...
def use(backend: str, *, force: bool = ...) -> None: ...
def get_backend() -> str: ...
@overload
def get_backend(*, auto_select: Literal[True] = True) -> str: ...
@overload
def get_backend(*, auto_select: Literal[False]) -> str | None: ...
def interactive(b: bool) -> None: ...
def is_interactive() -> bool: ...

Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/tests/test_rcparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ def test_backend_fallback_headful(tmp_path):
# Check that access on another instance does not resolve the sentinel.
"assert mpl.RcParams({'backend': sentinel})['backend'] == sentinel; "
"assert mpl.rcParams._get('backend') == sentinel; "
"assert mpl.get_backend(auto_select=False) is None; "
"import matplotlib.pyplot; "
"print(matplotlib.get_backend())"],
env=env, text=True, check=True, capture_output=True).stdout
Expand Down
Loading