Skip to content

Commit 69607f9

Browse files
committed
Shorter property deprecation.
This lets us write deprecated properties with single-line lambdas, which is quite a bit more compact (as shown by the line diff).
1 parent 327cfcf commit 69607f9

File tree

12 files changed

+122
-211
lines changed

12 files changed

+122
-211
lines changed

lib/matplotlib/_mathtext.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -805,10 +805,7 @@ def __init__(self, default_font_prop, mathtext_backend=None):
805805
self.fonts['default'] = default_font
806806
self.fonts['regular'] = default_font
807807

808-
@cbook.deprecated("3.4")
809-
@property
810-
def pswriter(self):
811-
return StringIO()
808+
pswriter = cbook.deprecated("3.4")(property(lambda self: StringIO()))
812809

813810
def _get_font(self, font):
814811
if font in self.fontmap:
@@ -1562,10 +1559,7 @@ class Glue(Node):
15621559
it's easier to stick to what TeX does.)
15631560
"""
15641561

1565-
@cbook.deprecated("3.3")
1566-
@property
1567-
def glue_subtype(self):
1568-
return "normal"
1562+
glue_subtype = cbook.deprecated("3.3")(property(lambda self: "normal"))
15691563

15701564
@cbook._delete_parameter("3.3", "copy")
15711565
def __init__(self, glue_type, copy=False):

lib/matplotlib/animation.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -778,11 +778,8 @@ class HTMLWriter(FileMovieWriter):
778778
"""Writer for JavaScript-based HTML movies."""
779779

780780
supported_formats = ['png', 'jpeg', 'tiff', 'svg']
781-
782-
@cbook.deprecated("3.3")
783-
@property
784-
def args_key(self):
785-
return 'animation.html_args'
781+
args_key = cbook.deprecated("3.3")(property(
782+
lambda self: 'animation.html_args'))
786783

787784
@classmethod
788785
def isAvailable(cls):

lib/matplotlib/backend_bases.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -2709,6 +2709,9 @@ class FigureManagerBase:
27092709
figure.canvas.mpl_disconnect(
27102710
figure.canvas.manager.button_press_handler_id)
27112711
"""
2712+
2713+
statusbar = cbook.deprecated("3.3")(property(lambda self: None))
2714+
27122715
def __init__(self, canvas, num):
27132716
self.canvas = canvas
27142717
canvas.manager = self # store a pointer to parent
@@ -2735,11 +2738,6 @@ def notify_axes_change(fig):
27352738
if self.toolmanager is None and self.toolbar is not None:
27362739
self.toolbar.update()
27372740

2738-
@cbook.deprecated("3.3")
2739-
@property
2740-
def statusbar(self):
2741-
return None
2742-
27432741
def show(self):
27442742
"""
27452743
For GUI backends, show the figure window and redraw.

lib/matplotlib/backends/backend_cairo.py

+33-38
Original file line numberDiff line numberDiff line change
@@ -91,40 +91,40 @@ def attr(field):
9191
return name, slant, weight
9292

9393

94+
# Mappings used for deprecated properties in RendererCairo, see below.
95+
_fontweights = {
96+
100: cairo.FONT_WEIGHT_NORMAL,
97+
200: cairo.FONT_WEIGHT_NORMAL,
98+
300: cairo.FONT_WEIGHT_NORMAL,
99+
400: cairo.FONT_WEIGHT_NORMAL,
100+
500: cairo.FONT_WEIGHT_NORMAL,
101+
600: cairo.FONT_WEIGHT_BOLD,
102+
700: cairo.FONT_WEIGHT_BOLD,
103+
800: cairo.FONT_WEIGHT_BOLD,
104+
900: cairo.FONT_WEIGHT_BOLD,
105+
'ultralight': cairo.FONT_WEIGHT_NORMAL,
106+
'light': cairo.FONT_WEIGHT_NORMAL,
107+
'normal': cairo.FONT_WEIGHT_NORMAL,
108+
'medium': cairo.FONT_WEIGHT_NORMAL,
109+
'regular': cairo.FONT_WEIGHT_NORMAL,
110+
'semibold': cairo.FONT_WEIGHT_BOLD,
111+
'bold': cairo.FONT_WEIGHT_BOLD,
112+
'heavy': cairo.FONT_WEIGHT_BOLD,
113+
'ultrabold': cairo.FONT_WEIGHT_BOLD,
114+
'black': cairo.FONT_WEIGHT_BOLD,
115+
}
116+
_fontangles = {
117+
'italic': cairo.FONT_SLANT_ITALIC,
118+
'normal': cairo.FONT_SLANT_NORMAL,
119+
'oblique': cairo.FONT_SLANT_OBLIQUE,
120+
}
121+
122+
94123
class RendererCairo(RendererBase):
95-
@cbook.deprecated("3.3")
96-
@property
97-
def fontweights(self):
98-
return {
99-
100: cairo.FONT_WEIGHT_NORMAL,
100-
200: cairo.FONT_WEIGHT_NORMAL,
101-
300: cairo.FONT_WEIGHT_NORMAL,
102-
400: cairo.FONT_WEIGHT_NORMAL,
103-
500: cairo.FONT_WEIGHT_NORMAL,
104-
600: cairo.FONT_WEIGHT_BOLD,
105-
700: cairo.FONT_WEIGHT_BOLD,
106-
800: cairo.FONT_WEIGHT_BOLD,
107-
900: cairo.FONT_WEIGHT_BOLD,
108-
'ultralight': cairo.FONT_WEIGHT_NORMAL,
109-
'light': cairo.FONT_WEIGHT_NORMAL,
110-
'normal': cairo.FONT_WEIGHT_NORMAL,
111-
'medium': cairo.FONT_WEIGHT_NORMAL,
112-
'regular': cairo.FONT_WEIGHT_NORMAL,
113-
'semibold': cairo.FONT_WEIGHT_BOLD,
114-
'bold': cairo.FONT_WEIGHT_BOLD,
115-
'heavy': cairo.FONT_WEIGHT_BOLD,
116-
'ultrabold': cairo.FONT_WEIGHT_BOLD,
117-
'black': cairo.FONT_WEIGHT_BOLD,
118-
}
119-
120-
@cbook.deprecated("3.3")
121-
@property
122-
def fontangles(self):
123-
return {
124-
'italic': cairo.FONT_SLANT_ITALIC,
125-
'normal': cairo.FONT_SLANT_NORMAL,
126-
'oblique': cairo.FONT_SLANT_OBLIQUE,
127-
}
124+
fontweights = cbook.deprecated("3.3")(property(lambda self: _fontweights))
125+
fontangles = cbook.deprecated("3.3")(property(lambda self: _fontangles))
126+
mathtext_parser = cbook.deprecated("3.4")(
127+
property(lambda self: MathTextParser('Cairo')))
128128

129129
def __init__(self, dpi):
130130
self.dpi = dpi
@@ -133,11 +133,6 @@ def __init__(self, dpi):
133133
cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1))
134134
super().__init__()
135135

136-
@cbook.deprecated("3.4")
137-
@property
138-
def mathtext_parser(self):
139-
return MathTextParser('Cairo')
140-
141136
def set_ctx_from_surface(self, surface):
142137
self.gc.ctx = cairo.Context(surface)
143138
# Although it may appear natural to automatically call

lib/matplotlib/backends/backend_gtk3.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,9 @@ def resize(self, width, height):
489489

490490

491491
class NavigationToolbar2GTK3(NavigationToolbar2, Gtk.Toolbar):
492+
ctx = cbook.deprecated("3.3")(property(
493+
lambda self: self.canvas.get_property("window").cairo_create()))
494+
492495
def __init__(self, canvas, window):
493496
self.win = window
494497
GObject.GObject.__init__(self)
@@ -541,11 +544,6 @@ def __init__(self, canvas, window):
541544

542545
NavigationToolbar2.__init__(self, canvas)
543546

544-
@cbook.deprecated("3.3")
545-
@property
546-
def ctx(self):
547-
return self.canvas.get_property("window").cairo_create()
548-
549547
def set_message(self, s):
550548
escaped = GLib.markup_escape_text(s)
551549
self.message.set_markup(f'<small>{escaped}</small>')

lib/matplotlib/backends/backend_pgf.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,7 @@ class PdfPages:
961961
'_info_dict',
962962
'_metadata',
963963
)
964+
metadata = cbook.deprecated('3.3')(property(lambda self: self._metadata))
964965

965966
def __init__(self, filename, *, keep_empty=True, metadata=None):
966967
"""
@@ -1006,11 +1007,6 @@ def __init__(self, filename, *, keep_empty=True, metadata=None):
10061007
self._info_dict = _create_pdf_info_dict('pgf', self._metadata)
10071008
self._file = BytesIO()
10081009

1009-
@cbook.deprecated('3.3')
1010-
@property
1011-
def metadata(self):
1012-
return self._metadata
1013-
10141010
def _write_header(self, width_inches, height_inches):
10151011
hyperref_options = ','.join(
10161012
_metadata_to_str(k, v) for k, v in self._info_dict.items())

lib/matplotlib/backends/backend_ps.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ class RendererPS(_backend_pdf_ps.RendererPDFPSBase):
223223
_afm_font_dir = cbook._get_data_path("fonts/afm")
224224
_use_afm_rc_name = "ps.useafm"
225225

226+
mathtext_parser = cbook.deprecated("3.4")(property(
227+
lambda self: MathTextParser("PS")))
228+
used_characters = cbook.deprecated("3.3")(property(
229+
lambda self: self._character_tracker.used_characters))
230+
226231
def __init__(self, width, height, pswriter, imagedpi=72):
227232
# Although postscript itself is dpi independent, we need to inform the
228233
# image code about a requested dpi to generate high resolution images
@@ -249,16 +254,6 @@ def __init__(self, width, height, pswriter, imagedpi=72):
249254

250255
self._character_tracker = _backend_pdf_ps.CharacterTracker()
251256

252-
@cbook.deprecated("3.3")
253-
@property
254-
def mathtext_parser(self):
255-
return MathTextParser("PS")
256-
257-
@cbook.deprecated("3.3")
258-
@property
259-
def used_characters(self):
260-
return self._character_tracker.used_characters
261-
262257
@cbook.deprecated("3.3")
263258
def track_characters(self, *args, **kwargs):
264259
"""Keep track of which characters are required from each font."""

lib/matplotlib/cbook/deprecation.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -192,22 +192,24 @@ def finalize(wrapper, new_doc):
192192
class _deprecated_property(property):
193193
def __get__(self, instance, owner):
194194
if instance is not None:
195-
from . import _warn_external
196-
_warn_external(warning)
195+
emit_warning()
197196
return super().__get__(instance, owner)
198197

199198
def __set__(self, instance, value):
200199
if instance is not None:
201-
from . import _warn_external
202-
_warn_external(warning)
200+
emit_warning()
203201
return super().__set__(instance, value)
204202

205203
def __delete__(self, instance):
206204
if instance is not None:
207-
from . import _warn_external
208-
_warn_external(warning)
205+
emit_warning()
209206
return super().__delete__(instance)
210207

208+
def __set_name__(self, owner, set_name):
209+
nonlocal name
210+
if name == "<lambda>":
211+
name = set_name
212+
211213
def finalize(_, new_doc):
212214
return _deprecated_property(
213215
fget=obj.fget, fset=obj.fset, fdel=obj.fdel, doc=new_doc)
@@ -224,13 +226,14 @@ def finalize(wrapper, new_doc):
224226
wrapper.__doc__ = new_doc
225227
return wrapper
226228

227-
warning = _generate_deprecation_warning(
228-
since, message, name, alternative, pending, obj_type, addendum,
229-
removal=removal)
229+
def emit_warning():
230+
warn_deprecated(
231+
since, message=message, name=name, alternative=alternative,
232+
pending=pending, obj_type=obj_type, addendum=addendum,
233+
removal=removal)
230234

231235
def wrapper(*args, **kwargs):
232-
from . import _warn_external
233-
_warn_external(warning)
236+
emit_warning()
234237
return func(*args, **kwargs)
235238

236239
old_doc = inspect.cleandoc(old_doc or '').strip('\n')

lib/matplotlib/contour.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,8 @@ class ContourSet(cm.ScalarMappable, ContourLabeler):
709709
%(contour_set_attributes)s
710710
"""
711711

712+
ax = cbook.deprecated("3.3")(property(lambda self: self.axes))
713+
712714
def __init__(self, ax, *args,
713715
levels=None, filled=False, linewidths=None, linestyles=None,
714716
hatches=(None,), alpha=None, origin=None, extent=None,
@@ -907,11 +909,6 @@ def __init__(self, ax, *args,
907909
cbook._warn_external('The following kwargs were not used by '
908910
'contour: ' + s)
909911

910-
@cbook.deprecated("3.3")
911-
@property
912-
def ax(self):
913-
return self.axes
914-
915912
def get_transform(self):
916913
"""
917914
Return the :class:`~matplotlib.transforms.Transform`

lib/matplotlib/texmanager.py

+14-31
Original file line numberDiff line numberDiff line change
@@ -80,43 +80,26 @@ class TexManager:
8080
'computer modern sans serif': ('cmss', r'\usepackage{type1ec}'),
8181
'computer modern typewriter': ('cmtt', r'\usepackage{type1ec}')}
8282

83-
@cbook.deprecated("3.3", alternative="matplotlib.get_cachedir()")
84-
@property
85-
def cachedir(self):
86-
return mpl.get_cachedir()
87-
88-
@cbook.deprecated("3.3")
89-
@property
90-
def rgba_arrayd(self):
91-
return {}
83+
cachedir = cbook.deprecated(
84+
"3.3", alternative="matplotlib.get_cachedir()")(
85+
property(lambda self: mpl.get_cachedir()))
86+
rgba_arrayd = cbook.deprecated("3.3")(property(lambda self: {}))
87+
_fonts = {} # Only for deprecation period.
88+
serif = cbook.deprecated("3.3")(property(
89+
lambda self: self._fonts.get("serif", ('cmr', ''))))
90+
sans_serif = cbook.deprecated("3.3")(property(
91+
lambda self: self._fonts.get("sans-serif", ('cmss', ''))))
92+
cursive = cbook.deprecated("3.3")(property(
93+
lambda self:
94+
self._fonts.get("cursive", ('pzc', r'\usepackage{chancery}'))))
95+
monospace = cbook.deprecated("3.3")(property(
96+
lambda self: self._fonts.get("monospace", ('cmtt', ''))))
9297

9398
@functools.lru_cache() # Always return the same instance.
9499
def __new__(cls):
95100
Path(cls.texcache).mkdir(parents=True, exist_ok=True)
96101
return object.__new__(cls)
97102

98-
_fonts = {} # Only for deprecation period.
99-
100-
@cbook.deprecated("3.3")
101-
@property
102-
def serif(self):
103-
return self._fonts.get("serif", ('cmr', ''))
104-
105-
@cbook.deprecated("3.3")
106-
@property
107-
def sans_serif(self):
108-
return self._fonts.get("sans-serif", ('cmss', ''))
109-
110-
@cbook.deprecated("3.3")
111-
@property
112-
def cursive(self):
113-
return self._fonts.get("cursive", ('pzc', r'\usepackage{chancery}'))
114-
115-
@cbook.deprecated("3.3")
116-
@property
117-
def monospace(self):
118-
return self._fonts.get("monospace", ('cmtt', ''))
119-
120103
def get_font_config(self):
121104
ff = rcParams['font.family']
122105
if len(ff) == 1 and ff[0].lower() in self.font_families:

0 commit comments

Comments
 (0)