Spaces:
Running
Running
File size: 1,785 Bytes
b72ab63 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import warnings
import functools
class AltairDeprecationWarning(UserWarning):
pass
def deprecated(message=None):
"""Decorator to deprecate a function or class.
Parameters
----------
message : string (optional)
The deprecation message
"""
def wrapper(obj):
return _deprecate(obj, message=message)
return wrapper
def _deprecate(obj, name=None, message=None):
"""Return a version of a class or function that raises a deprecation warning.
Parameters
----------
obj : class or function
The object to create a deprecated version of.
name : string (optional)
The name of the deprecated object
message : string (optional)
The deprecation message
Returns
-------
deprecated_obj :
The deprecated version of obj
Examples
--------
>>> class Foo: pass
>>> OldFoo = _deprecate(Foo, "OldFoo")
>>> f = OldFoo() # doctest: +SKIP
AltairDeprecationWarning: alt.OldFoo is deprecated. Use alt.Foo instead.
"""
if message is None:
message = "alt.{} is deprecated. Use alt.{} instead." "".format(
name, obj.__name__
)
if isinstance(obj, type):
return type(
name,
(obj,),
{
"__doc__": obj.__doc__,
"__init__": _deprecate(obj.__init__, "__init__", message),
},
)
elif callable(obj):
@functools.wraps(obj)
def new_obj(*args, **kwargs):
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
return obj(*args, **kwargs)
new_obj._deprecated = True
return new_obj
else:
raise ValueError("Cannot deprecate object of type {}".format(type(obj)))
|