mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-09 09:22:03 +00:00
Implement decorator of deprecation
This commit is contained in:
parent
fa1ab25603
commit
065804ff99
1 changed files with 80 additions and 0 deletions
80
aiogram/utils/deprecated.py
Normal file
80
aiogram/utils/deprecated.py
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
"""
|
||||||
|
Source: https://stackoverflow.com/questions/2536307/decorators-in-the-python-standard-lib-deprecated-specifically
|
||||||
|
"""
|
||||||
|
|
||||||
|
import functools
|
||||||
|
import inspect
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
|
def deprecated(reason):
|
||||||
|
"""
|
||||||
|
This is a decorator which can be used to mark functions
|
||||||
|
as deprecated. It will result in a warning being emitted
|
||||||
|
when the function is used.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if isinstance(reason, str):
|
||||||
|
|
||||||
|
# The @deprecated is used with a 'reason'.
|
||||||
|
#
|
||||||
|
# .. code-block:: python
|
||||||
|
#
|
||||||
|
# @deprecated("please, use another function")
|
||||||
|
# def old_function(x, y):
|
||||||
|
# pass
|
||||||
|
|
||||||
|
def decorator(func1):
|
||||||
|
|
||||||
|
if inspect.isclass(func1):
|
||||||
|
fmt1 = "Call to deprecated class {name} ({reason})."
|
||||||
|
else:
|
||||||
|
fmt1 = "Call to deprecated function {name} ({reason})."
|
||||||
|
|
||||||
|
@functools.wraps(func1)
|
||||||
|
def new_func1(*args, **kwargs):
|
||||||
|
warnings.simplefilter('always', DeprecationWarning)
|
||||||
|
warnings.warn(
|
||||||
|
fmt1.format(name=func1.__name__, reason=reason),
|
||||||
|
category=DeprecationWarning,
|
||||||
|
stacklevel=2
|
||||||
|
)
|
||||||
|
warnings.simplefilter('default', DeprecationWarning)
|
||||||
|
return func1(*args, **kwargs)
|
||||||
|
|
||||||
|
return new_func1
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
elif inspect.isclass(reason) or inspect.isfunction(reason):
|
||||||
|
|
||||||
|
# The @deprecated is used without any 'reason'.
|
||||||
|
#
|
||||||
|
# .. code-block:: python
|
||||||
|
#
|
||||||
|
# @deprecated
|
||||||
|
# def old_function(x, y):
|
||||||
|
# pass
|
||||||
|
|
||||||
|
func2 = reason
|
||||||
|
|
||||||
|
if inspect.isclass(func2):
|
||||||
|
fmt2 = "Call to deprecated class {name}."
|
||||||
|
else:
|
||||||
|
fmt2 = "Call to deprecated function {name}."
|
||||||
|
|
||||||
|
@functools.wraps(func2)
|
||||||
|
def new_func2(*args, **kwargs):
|
||||||
|
warnings.simplefilter('always', DeprecationWarning)
|
||||||
|
warnings.warn(
|
||||||
|
fmt2.format(name=func2.__name__),
|
||||||
|
category=DeprecationWarning,
|
||||||
|
stacklevel=2
|
||||||
|
)
|
||||||
|
warnings.simplefilter('default', DeprecationWarning)
|
||||||
|
return func2(*args, **kwargs)
|
||||||
|
|
||||||
|
return new_func2
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise TypeError(repr(type(reason)))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue