Small changes in deprecation util.

This commit is contained in:
Alex Root Junior 2017-08-01 00:46:20 +03:00
parent c19890a7d2
commit bc171ed168

View file

@ -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)