|
16 | 16 | import warnings
|
17 | 17 |
|
18 | 18 |
|
| 19 | +# has to be here (instead of cbook) to prevent circular import, since |
| 20 | +# deprecation.deprecated is *used* during cbook's import |
| 21 | +class _classproperty: |
| 22 | + """ |
| 23 | + Like `property`, but also triggers on access via the class, and it is the |
| 24 | + *class* that's passed as argument. |
| 25 | +
|
| 26 | + Examples |
| 27 | + -------- |
| 28 | + :: |
| 29 | +
|
| 30 | + class C: |
| 31 | + @_classproperty |
| 32 | + def foo(cls): |
| 33 | + return cls.__name__ |
| 34 | +
|
| 35 | + assert C.foo == "C" |
| 36 | + """ |
| 37 | + |
| 38 | + def __init__(self, fget, fset=None, fdel=None, doc=None): |
| 39 | + self._fget = fget |
| 40 | + if fset is not None or fdel is not None: |
| 41 | + raise ValueError('_classproperty only implements fget.') |
| 42 | + self.fset = fset |
| 43 | + self.fdel = fdel |
| 44 | + # docs are ignored for now |
| 45 | + self._doc = doc |
| 46 | + |
| 47 | + def __get__(self, instance, owner): |
| 48 | + return self._fget(owner) |
| 49 | + |
| 50 | + @property |
| 51 | + def fget(self): |
| 52 | + return self._fget |
| 53 | + |
| 54 | + |
19 | 55 | class MatplotlibDeprecationWarning(UserWarning):
|
20 | 56 | """
|
21 | 57 | A class for issuing deprecation warnings for Matplotlib users.
|
@@ -201,15 +237,16 @@ def finalize(wrapper, new_doc):
|
201 | 237 | obj.__init__ = functools.wraps(obj.__init__)(wrapper)
|
202 | 238 | return obj
|
203 | 239 |
|
204 |
| - elif isinstance(obj, property): |
| 240 | + elif isinstance(obj, (property, _classproperty)): |
205 | 241 | obj_type = "attribute"
|
206 | 242 | func = None
|
207 | 243 | name = name or obj.fget.__name__
|
208 | 244 | old_doc = obj.__doc__
|
209 | 245 |
|
210 |
| - class _deprecated_property(property): |
| 246 | + class _deprecated_property(type(obj)): |
211 | 247 | def __get__(self, instance, owner):
|
212 |
| - if instance is not None: |
| 248 | + if instance is not None or owner is not None \ |
| 249 | + and isinstance(self, _classproperty): |
213 | 250 | emit_warning()
|
214 | 251 | return super().__get__(instance, owner)
|
215 | 252 |
|
|
0 commit comments