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

Skip to content

Commit c505b7b

Browse files
committed
MAINT: deprecate validCap, validJoin
1 parent 06514d2 commit c505b7b

File tree

6 files changed

+84
-34
lines changed

6 files changed

+84
-34
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Line2D and Patch no longer duplicate ``validJoin`` and ``validCap``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Validation of joinstyle and capstyles is now centralized in ``rcsetup``.

lib/matplotlib/cbook/__init__.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
_rename_parameter, _delete_parameter, _make_keyword_only,
3535
_deprecate_method_override, _deprecate_privatize_attribute,
3636
_suppress_matplotlib_deprecation_warning,
37-
MatplotlibDeprecationWarning, mplDeprecation)
37+
MatplotlibDeprecationWarning, mplDeprecation, _classproperty)
3838

3939

4040
def _get_running_interactive_framework():
@@ -2281,30 +2281,6 @@ def type_name(tp):
22812281
type_name(type(v))))
22822282

22832283

2284-
class _classproperty:
2285-
"""
2286-
Like `property`, but also triggers on access via the class, and it is the
2287-
*class* that's passed as argument.
2288-
2289-
Examples
2290-
--------
2291-
::
2292-
2293-
class C:
2294-
@classproperty
2295-
def foo(cls):
2296-
return cls.__name__
2297-
2298-
assert C.foo == "C"
2299-
"""
2300-
2301-
def __init__(self, fget):
2302-
self._fget = fget
2303-
2304-
def __get__(self, instance, owner):
2305-
return self._fget(owner)
2306-
2307-
23082284
def _backend_module_name(name):
23092285
"""
23102286
Convert a backend name (either a standard backend -- "Agg", "TkAgg", ... --

lib/matplotlib/cbook/deprecation.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,41 @@
44
import warnings
55

66

7+
# has to be here to prevent circular import, since it's used by `deprecated`
8+
class _classproperty:
9+
"""
10+
Like `property`, but also triggers on access via the class, and it is the
11+
*class* that's passed as argument.
12+
13+
Examples
14+
--------
15+
::
16+
17+
class C:
18+
@_classproperty
19+
def foo(cls):
20+
return cls.__name__
21+
22+
assert C.foo == "C"
23+
"""
24+
25+
def __init__(self, fget, fset=None, fdel=None, doc=None):
26+
self._fget = fget
27+
if fset is not None or fdel is not None:
28+
raise ValueError('_classproperty only implements fget.')
29+
self.fset = fset
30+
self.fdel = fdel
31+
# docs are ignored for now
32+
self._doc = doc
33+
34+
def __get__(self, instance, owner):
35+
return self._fget(owner)
36+
37+
@property
38+
def fget(self):
39+
return self._fget
40+
41+
742
class MatplotlibDeprecationWarning(UserWarning):
843
"""
944
A class for issuing deprecation warnings for Matplotlib users.
@@ -189,15 +224,15 @@ def finalize(wrapper, new_doc):
189224
obj.__init__ = functools.wraps(obj.__init__)(wrapper)
190225
return obj
191226

192-
elif isinstance(obj, property):
227+
elif isinstance(obj, (property, _classproperty)):
193228
obj_type = "attribute"
194229
func = None
195230
name = name or obj.fget.__name__
196231
old_doc = obj.__doc__
197232

198-
class _deprecated_property(property):
233+
class _deprecated_property(type(obj)):
199234
def __get__(self, instance, owner):
200-
if instance is not None:
235+
if instance is not None or owner is not None:
201236
emit_warning()
202237
return super().__get__(instance, owner)
203238

@@ -218,7 +253,8 @@ def __set_name__(self, owner, set_name):
218253

219254
def finalize(_, new_doc):
220255
return _deprecated_property(
221-
fget=obj.fget, fset=obj.fset, fdel=obj.fdel, doc=new_doc)
256+
fget=obj.fget, fset=obj.fset, fdel=obj.fdel,
257+
doc=new_doc)
222258

223259
else:
224260
if obj_type is None:

lib/matplotlib/lines.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from . import _api, artist, cbook, colors as mcolors, docstring, rcParams
1414
from .artist import Artist, allow_rasterization
1515
from .cbook import (
16-
_to_unmasked_float_array, ls_mapper, ls_mapper_r, STEP_LOOKUP_MAP)
16+
_to_unmasked_float_array, ls_mapper, ls_mapper_r, STEP_LOOKUP_MAP,
17+
deprecated, _classproperty)
1718
from .colors import is_color_like, get_named_colors_mapping
1819
from .markers import MarkerStyle
1920
from .path import Path
@@ -251,8 +252,16 @@ class Line2D(Artist):
251252
fillStyles = MarkerStyle.fillstyles
252253

253254
zorder = 2
254-
validCap = ('butt', 'round', 'projecting')
255-
validJoin = ('miter', 'round', 'bevel')
255+
256+
@deprecated("3.4.0")
257+
@_classproperty
258+
def validCap(cls):
259+
return ('butt', 'round', 'projecting')
260+
261+
@deprecated("3.4.0")
262+
@_classproperty
263+
def validJoin(cls):
264+
return ('miter', 'round', 'bevel')
256265

257266
def __str__(self):
258267
if self._label != "":

lib/matplotlib/patches.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from numbers import Number
66
import textwrap
77
from collections import namedtuple
8+
import warnings
89

910
import numpy as np
1011

@@ -33,8 +34,20 @@ class Patch(artist.Artist):
3334
are *None*, they default to their rc params setting.
3435
"""
3536
zorder = 1
36-
validCap = mlines.Line2D.validCap
37-
validJoin = mlines.Line2D.validJoin
37+
38+
@cbook.deprecated("3.4.0")
39+
@cbook._classproperty
40+
def validCap(cls):
41+
with warnings.catch_warnings():
42+
warnings.filterwarnings("ignore", category=DeprecationWarning)
43+
return mlines.Line2D.validCap
44+
45+
@cbook.deprecated("3.4.0")
46+
@cbook._classproperty
47+
def validJoin(cls):
48+
with warnings.catch_warnings():
49+
warnings.filterwarnings("ignore", category=DeprecationWarning)
50+
return mlines.Line2D.validJoin
3851

3952
# Whether to draw an edge by default. Set on a
4053
# subclass-by-subclass basis.

lib/matplotlib/tests/test_cbook.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,16 @@ def test_format_approx():
765765
assert f(0.0012345600001, 5) == '0.00123'
766766
assert f(-0.0012345600001, 5) == '-0.00123'
767767
assert f(0.0012345600001, 8) == f(0.0012345600001, 10) == '0.00123456'
768+
769+
770+
def test_classproperty_deprecation():
771+
class A:
772+
@cbook.deprecated("0.0.0")
773+
@cbook._classproperty
774+
def f(cls):
775+
pass
776+
with pytest.warns(cbook.MatplotlibDeprecationWarning):
777+
A.f
778+
with pytest.warns(cbook.MatplotlibDeprecationWarning):
779+
a = A()
780+
a.f

0 commit comments

Comments
 (0)