8000 Use _api.caching_module_getattr for deprecated module-level attributes · ianthomas23/matplotlib@a0363a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit a0363a3

Browse files
committed
Use _api.caching_module_getattr for deprecated module-level attributes
1 parent f852721 commit a0363a3

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,25 @@
3939
BackendFilter.NON_INTERACTIVE)
4040
_deprecated_all_backends = backend_registry.list_builtin()
4141

42-
_deprecated_names_and_args = {
43-
"interactive_bk": "matplotlib.backends.registry.BackendFilter.INTERACTIVE",
44-
"non_interactive_bk": "matplotlib.backends.registry.BackendFilter.NON_INTERACTIVE",
45-
"all_backends": "",
46-
}
47-
48-
49-
def __getattr__(name):
50-
if name in _deprecated_names_and_args:
51-
arg = _deprecated_names_and_args[name]
52-
_api.warn_deprecated(
53-
"3.9.0",
54-
name=name,
55-
alternative="``matplotlib.backends.registry.backend_registry"
56-
f".list_builtin({arg})``",
57-
)
58-
return globals()[f"_deprecated_{name}"]
59-
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
60-
6142

62-
def __dir__():
63-
return sorted(globals().keys() | _deprecated_names_and_args.keys())
43+
@_api.caching_module_getattr
44+
class __getattr__:
45+
interactive_bk = _api.deprecated(
46+
"3.9",
47+
alternative="``matplotlib.backends.registry.backend_registry.list_builtin"
48+
"(matplotlib.backends.registry.BackendFilter.INTERACTIVE)``"
49+
)(property(lambda self: _deprecated_interactive_bk))
50+
51+
non_interactive_bk = _api.deprecated(
52+
"3.9",
53+
alternative="``matplotlib.backends.registry.backend_registry.list_builtin"
54+
"(matplotlib.backends.registry.BackendFilter.NON_INTERACTIVE)``"
55+
)(property(lambda self: _deprecated_non_interactive_bk))
56+
57+
all_backends = _api.deprecated(
58+
"3.9",
59+
alternative="``matplotlib.backends.registry.backend_registry.list_builtin()``"
60+
)(property(lambda self: _deprecated_all_backends))
6461

6562

6663
class ValidateInStrings:

lib/matplotlib/tests/test_backend_registry.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55

6+
import matplotlib as mpl
67
from matplotlib.backends.registry import BackendFilter, backend_registry
78

89

@@ -54,3 +55,13 @@ def test_list_builtin_with_filter(filter, expected):
5455
assert not has_duplicates(backends)
5556
# Compare using sets as order is not important
5657
assert {*backends} == {*expected}
58+
59+
60+
def test_deprecated_rcsetup_attributes():
61+
match = "was deprecated in Matplotlib 3.9"
62+
with pytest.warns(mpl.MatplotlibDeprecationWarning, match=match):
63+
mpl.rcsetup.interactive_bk
64+
with pytest.warns(mpl.MatplotlibDeprecationWarning, match=match):
65+
mpl.rcsetup.non_interactive_bk
66+
with pytest.warns(mpl.MatplotlibDeprecationWarning, match=match):
67+
mpl.rcsetup.all_backends

0 commit comments

Comments
 (0)
0