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