mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-11 01:54:53 +00:00
Small changes in deprecation util.
This commit is contained in:
parent
c19890a7d2
commit
bc171ed168
1 changed files with 24 additions and 29 deletions
|
|
@ -24,25 +24,20 @@ def deprecated(reason):
|
||||||
# def old_function(x, y):
|
# def old_function(x, y):
|
||||||
# pass
|
# pass
|
||||||
|
|
||||||
def decorator(func1):
|
def decorator(func):
|
||||||
|
|
||||||
if inspect.isclass(func1):
|
if inspect.isclass(func):
|
||||||
fmt1 = "Call to deprecated class {name} ({reason})."
|
msg = "Call to deprecated class {name} ({reason})."
|
||||||
else:
|
else:
|
||||||
fmt1 = "Call to deprecated function {name} ({reason})."
|
msg = "Call to deprecated function {name} ({reason})."
|
||||||
|
|
||||||
@functools.wraps(func1)
|
@functools.wraps(func)
|
||||||
def new_func1(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
warnings.simplefilter('always', DeprecationWarning)
|
warn_deprecated(msg.format(name=func.__name__, reason=reason))
|
||||||
warnings.warn(
|
|
||||||
fmt1.format(name=func1.__name__, reason=reason),
|
|
||||||
category=DeprecationWarning,
|
|
||||||
stacklevel=2
|
|
||||||
)
|
|
||||||
warnings.simplefilter('default', DeprecationWarning)
|
warnings.simplefilter('default', DeprecationWarning)
|
||||||
return func1(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
return new_func1
|
return wrapper
|
||||||
|
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
@ -56,25 +51,25 @@ def deprecated(reason):
|
||||||
# def old_function(x, y):
|
# def old_function(x, y):
|
||||||
# pass
|
# pass
|
||||||
|
|
||||||
func2 = reason
|
func1 = reason
|
||||||
|
|
||||||
if inspect.isclass(func2):
|
if inspect.isclass(func1):
|
||||||
fmt2 = "Call to deprecated class {name}."
|
msg1 = "Call to deprecated class {name}."
|
||||||
else:
|
else:
|
||||||
fmt2 = "Call to deprecated function {name}."
|
msg1 = "Call to deprecated function {name}."
|
||||||
|
|
||||||
@functools.wraps(func2)
|
@functools.wraps(func1)
|
||||||
def new_func2(*args, **kwargs):
|
def wrapper1(*args, **kwargs):
|
||||||
warnings.simplefilter('always', DeprecationWarning)
|
warn_deprecated(msg1.format(name=func1.__name__))
|
||||||
warnings.warn(
|
return func1(*args, **kwargs)
|
||||||
fmt2.format(name=func2.__name__),
|
|
||||||
category=DeprecationWarning,
|
|
||||||
stacklevel=2
|
|
||||||
)
|
|
||||||
warnings.simplefilter('default', DeprecationWarning)
|
|
||||||
return func2(*args, **kwargs)
|
|
||||||
|
|
||||||
return new_func2
|
return wrapper1
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise TypeError(repr(type(reason)))
|
raise TypeError(repr(type(reason)))
|
||||||
|
|
||||||
|
|
||||||
|
def warn_deprecated(message, warning=DeprecationWarning):
|
||||||
|
warnings.simplefilter('always', warning)
|
||||||
|
warnings.warn(message, category=warning, stacklevel=2)
|
||||||
|
warnings.simplefilter('default', warning)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue