| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 202a277 commit 3e363f2
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1296,15 +1296,37 @@ def use(backend, *, force=True): | |||
| 1296 | 1296 | rcParams['backend'] = os.environ.get('MPLBACKEND') | |
| 1297 | 1297 | ||
| 1298 | 1298 | ||
| 1299 | - def get_backend(): | ||
| 1299 | + def get_backend(resolve: bool = True): | ||
| 1300 | 1300 | """ | |
| 1301 | 1301 | Return the name of the current backend. | |
| 1302 | 1302 | ||
| 1303 | + Parameters | ||
| 1304 | + ---------- | ||
| 1305 | + resolve : bool, default: True | ||
| 1306 | + Whether to trigger backend resolution if no backend has been | ||
| 1307 | + selected so far. If True, this ensures that a valid backend | ||
| 1308 | + is returned. If False, this returns None if no backend has been | ||
| 1309 | + selected so far. | ||
| 1310 | + | ||
| 1311 | + .. admonition:: Provisional resolve flag | ||
| 1312 | + | ||
| 1313 | + The *resolve* flag is introduced provisionally in the anticipation | ||
| 1314 | + of backend resolution refactoring. We don't guarantee API stability | ||
| 1315 | + for now, but if all plays out well, the provisional availability | ||
| 1316 | + will prolong the range of supported releases for the migration period. | ||
| 1317 | + | ||
| 1303 | 1318 | See Also | |
| 1304 | 1319 | -------- | |
| 1305 | 1320 | matplotlib.use | |
| 1306 | 1321 | """ | |
| 1307 | - return rcParams['backend'] | ||
| 1322 | + if resolve: | ||
| 1323 | + return rcParams['backend'] | ||
| 1324 | + else: | ||
| 1325 | + backend = rcParams._get('backend') | ||
| 1326 | + if backend is rcsetup._auto_backend_sentinel(): | ||
| 1327 | + return None | ||
| 1328 | + else: | ||
| 1329 | + return backend | ||
| 1308 | 1330 | ||
| 1309 | 1331 | ||
| 1310 | 1332 | def interactive(b): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,7 +37,7 @@ import contextlib | |||
| 37 | 37 | from packaging.version import Version | |
| 38 | 38 | ||
| 39 | 39 | from matplotlib._api import MatplotlibDeprecationWarning | |
| 40 | - from typing import Any, NamedTuple | ||
| 40 | + from typing import Any, Literal, NamedTuple, overload | ||
| 41 | 41 | ||
| 42 | 42 | class _VersionInfo(NamedTuple): | |
| 43 | 43 | major: int | |
@@ -104,7 +104,9 @@ def rc_context( | |||
| 104 | 104 | rc: dict[str, Any] | None = ..., fname: str | Path | os.PathLike | None = ... | |
| 105 | 105 | ) -> Generator[None, None, None]: ... | |
| 106 | 106 | def use(backend: str, *, force: bool = ...) -> None: ... | |
| 107 | - def get_backend() -> str: ... | ||
| 107 | + @overload | ||
| 108 | + def get_backend(*, resolve: Literal[True] = True) -> str: ... | ||
| 109 | + def get_backend(*, resolve: bool = True) -> str | None: ... | ||
| 108 | 110 | def interactive(b: bool) -> None: ... | |
| 109 | 111 | def is_interactive() -> bool: ... | |
| 110 | 112 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -554,6 +554,7 @@ def test_backend_fallback_headful(tmp_path): | |||
| 554 | 554 | # Check that access on another instance does not resolve the sentinel. | |
| 555 | 555 | "assert mpl.RcParams({'backend': sentinel})['backend'] == sentinel; " | |
| 556 | 556 | "assert mpl.rcParams._get('backend') == sentinel; " | |
| 557 | + "assert matplotlib.get_backend(resolve=False) is None; " | ||
| 557 | 558 | "import matplotlib.pyplot; " | |
| 558 | 559 | "print(matplotlib.get_backend())"], | |
| 559 | 560 | env=env, text=True, check=True, capture_output=True).stdout | |
| Back | FazBrowse Home | New Git URL |
0 commit comments