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

Skip to content

Commit 3e32cc7

Browse files
authored
Merge pull request #20733 from anntzer/dg
Deprecate globals using module-level `__getattr__`.
2 parents a0bcd8c + 014df7f commit 3e32cc7

File tree

6 files changed

+86
-49
lines changed

6 files changed

+86
-49
lines changed

lib/matplotlib/__init__.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,15 @@ def _get_version():
176176
return _version.version
177177

178178

179+
@functools.lru_cache(None)
179180
def __getattr__(name):
180-
if name in ("__version__", "__version_info__"):
181-
global __version__ # cache it.
182-
__version__ = _get_version()
183-
global __version__info__ # cache it.
184-
__version_info__ = _parse_to_version_info(__version__)
185-
return __version__ if name == "__version__" else __version_info__
181+
if name == "__version__":
182+
return _get_version()
183+
elif name == "__version_info__":
184+
return _parse_to_version_info(__getattr__("__version__"))
185+
elif name == "URL_REGEX": # module-level deprecation.
186+
_api.warn_deprecated("3.5", name=name)
187+
return re.compile(r'^http://|^https://|^ftp://|^file:')
186188
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
187189

188190

@@ -714,14 +716,10 @@ def rc_params(fail_on_error=False):
714716
return rc_params_from_file(matplotlib_fname(), fail_on_error)
715717

716718

717-
# Deprecated in Matplotlib 3.5.
718-
URL_REGEX = re.compile(r'^http://|^https://|^ftp://|^file:')
719-
720-
721719
@_api.deprecated("3.5")
722720
def is_url(filename):
723721
"""Return whether *filename* is an http, https, ftp, or file URL path."""
724-
return URL_REGEX.match(filename) is not None
722+
return __getattr__("URL_REGEX").match(filename) is not None
725723

726724

727725
@functools.lru_cache()

lib/matplotlib/_api/deprecation.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ def _generate_deprecation_warning(
3737
removal = f"in {removal}" if removal else "two minor releases later"
3838
if not message:
3939
message = (
40-
"\nThe %(name)s %(obj_type)s"
40+
("\nThe %(name)s %(obj_type)s" if obj_type else "%(name)s")
4141
+ (" will be deprecated in a future version"
4242
if pending else
4343
(" was deprecated in Matplotlib %(since)s"
44-
+ (" and will be removed %(removal)s"
45-
if removal else
46-
"")))
44+
+ (" and will be removed %(removal)s" if removal else "")))
4745
+ "."
4846
+ (" Use %(alternative)s instead." if alternative else "")
4947
+ (" %(addendum)s" if addendum else ""))

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,27 @@
3636
backend_version = "%s.%s.%s" % (
3737
Gtk.get_major_version(), Gtk.get_minor_version(), Gtk.get_micro_version())
3838

39-
try:
40-
_display = Gdk.Display.get_default()
41-
cursord = { # deprecated in Matplotlib 3.5.
42-
Cursors.MOVE: Gdk.Cursor.new_from_name(_display, "move"),
43-
Cursors.HAND: Gdk.Cursor.new_from_name(_display, "pointer"),
44-
Cursors.POINTER: Gdk.Cursor.new_from_name(_display, "default"),
45-
Cursors.SELECT_REGION: Gdk.Cursor.new_from_name(_display, "crosshair"),
46-
Cursors.WAIT: Gdk.Cursor.new_from_name(_display, "wait"),
47-
}
48-
except TypeError as exc:
49-
cursord = {} # deprecated in Matplotlib 3.5.
39+
40+
# module-level deprecations.
41+
@functools.lru_cache(None)
42+
def __getattr__(name):
43+
if name == "cursord":
44+
_api.warn_deprecated("3.5", name=name)
45+
try:
46+
new_cursor = functools.partial(
47+
Gdk.Cursor.new_from_name, Gdk.Display.get_default())
48+
return {
49+
Cursors.MOVE: new_cursor("move"),
50+
Cursors.HAND: new_cursor("pointer"),
51+
Cursors.POINTER: new_cursor("default"),
52+
Cursors.SELECT_REGION: new_cursor("crosshair"),
53+
Cursors.WAIT: new_cursor("wait"),
54+
}
55+
except TypeError as exc:
56+
return {}
57+
else:
58+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
59+
5060

5161
# Placeholder
5262
_application = None

lib/matplotlib/backends/backend_wx.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,26 @@
4040
# for some info about screen dpi
4141
PIXELS_PER_INCH = 75
4242

43-
# Delay time for idle checks
44-
IDLE_DELAY = 5 # Documented as deprecated as of Matplotlib 3.1.
43+
44+
# module-level deprecations.
45+
@functools.lru_cache(None)
46+
def __getattr__(name):
47+
if name == "IDLE_DELAY":
48+
_api.warn_deprecated("3.1", name=name)
49+
return 5
50+
elif name == "cursord":
51+
_api.warn_deprecated("3.5", name=name)
52+
return { # deprecated in Matplotlib 3.5.
53+
cursors.MOVE: wx.CURSOR_HAND,
54+
cursors.HAND: wx.CURSOR_HAND,
55+
cursors.POINTER: wx.CURSOR_ARROW,
56+
cursors.SELECT_REGION: wx.CURSOR_CROSS,
57+
cursors.WAIT: wx.CURSOR_WAIT,
58+
cursors.RESIZE_HORIZONTAL: wx.CURSOR_SIZEWE,
59+
cursors.RESIZE_VERTICAL: wx.CURSOR_SIZENS,
60+
}
61+
else:
62+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
4563

4664

4765
def error_msg_wx(msg, parent=None):
@@ -721,7 +739,15 @@ def _onKeyUp(self, event):
721739

722740
def set_cursor(self, cursor):
723741
# docstring inherited
724-
cursor = wx.Cursor(_api.check_getitem(cursord, cursor=cursor))
742+
cursor = wx.Cursor(_api.check_getitem({
743+
cursors.MOVE: wx.CURSOR_HAND,
744+
cursors.HAND: wx.CURSOR_HAND,
745+
cursors.POINTER: wx.CURSOR_ARROW,
746+
cursors.SELECT_REGION: wx.CURSOR_CROSS,
747+
cursors.WAIT: wx.CURSOR_WAIT,
748+
cursors.RESIZE_HORIZONTAL: wx.CURSOR_SIZEWE,
749+
cursors.RESIZE_VERTICAL: wx.CURSOR_SIZENS,
750+
}, cursor=cursor))
725751
self.SetCursor(cursor)
726752
self.Update()
727753

@@ -1049,17 +1075,6 @@ def _set_frame_icon(frame):
10491075
frame.SetIcons(bundle)
10501076

10511077

1052-
cursord = { # deprecated in Matplotlib 3.5.
1053-
cursors.MOVE: wx.CURSOR_HAND,
1054-
cursors.HAND: wx.CURSOR_HAND,
1055-
cursors.POINTER: wx.CURSOR_ARROW,
1056-
cursors.SELECT_REGION: wx.CURSOR_CROSS,
1057-
cursors.WAIT: wx.CURSOR_WAIT,
1058-
cursors.RESIZE_HORIZONTAL: wx.CURSOR_SIZEWE,
1059-
cursors.RESIZE_VERTICAL: wx.CURSOR_SIZENS,
1060-
}
1061-
1062-
10631078
class NavigationToolbar2Wx(NavigationToolbar2, wx.ToolBar):
10641079
def __init__(self, canvas, coordinates=True):
10651080
wx.ToolBar.__init__(self, canvas.GetParent(), -1)

lib/matplotlib/colorbar.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"""
1313

1414
import copy
15+
import functools
1516
import logging
1617
import textwrap
1718

@@ -193,10 +194,21 @@
193194
textwrap.indent(_make_axes_other_param_doc, " "),
194195
_colormap_kw_doc))
195196

196-
# Deprecated since 3.4.
197-
colorbar_doc = docstring.interpd.params["colorbar_doc"]
198-
colormap_kw_doc = _colormap_kw_doc
199-
make_axes_kw_doc = _make_axes_param_doc + _make_axes_other_param_doc
197+
198+
# module-level deprecations.
199+
@functools.lru_cache(None)
200+
def __getattr__(name):
201+
if name == "colorbar_doc":
202+
_api.warn_deprecated("3.4", name=name)
203+
return docstring.interpd.params["colorbar_doc"]
204+
elif name == "colormap_kw_doc":
205+
_api.warn_deprecated("3.4", name=name)
206+
return _colormap_kw_doc
207+
elif name == "make_axes_kw_doc":
208+
_api.warn_deprecated("3.4", name=name)
209+
return _make_axes_param_doc + _make_axes_other_param_doc
210+
else:
211+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
200212

201213

202214
def _set_ticks_on_axis_warn(*args, **kw):

lib/matplotlib/style/core.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"""
1313

1414
import contextlib
15+
import functools
1516
import logging
1617
import os
1718
from pathlib import Path
@@ -26,15 +27,18 @@
2627
__all__ = ['use', 'context', 'available', 'library', 'reload_library']
2728

2829

30+
# module-level deprecations.
31+
@functools.lru_cache(None)
32+
def __getattr__(name):
33+
if name == "STYLE_FILE_PATTERN":
34+
_api.warn_deprecated("3.5", name=name)
35+
return re.compile(r'([\S]+).%s$' % STYLE_EXTENSION)
36+
37+
2938
BASE_LIBRARY_PATH = os.path.join(mpl.get_data_path(), 'stylelib')
3039
# Users may want multiple library paths, so store a list of paths.
3140
USER_LIBRARY_PATHS = [os.path.join(mpl.get_configdir(), 'stylelib')]
3241
STYLE_EXTENSION = 'mplstyle'
33-
34-
# Deprecated in Matplotlib 3.5.
35-
STYLE_FILE_PATTERN = re.compile(r'([\S]+).%s$' % STYLE_EXTENSION)
36-
37-
3842
# A list of rcParams that should not be applied from styles
3943
STYLE_BLACKLIST = {
4044
'interactive', 'backend', 'webagg.port', 'webagg.address',

0 commit comments

Comments
 (0)