diff options
author | Laurent Peuch <cortex@worlddomination.be> | 2020-05-06 14:55:37 +0200 |
---|---|---|
committer | Laurent Peuch <cortex@worlddomination.be> | 2020-05-06 14:55:37 +0200 |
commit | 1ca9be028235dfc29c4ec16e0912d86e5ad00367 (patch) | |
tree | dfe947c36ce80b94369ea5eb53e8a711e1b96c18 /logilab/common | |
parent | 324419e23b73f2269fff4d44e1457d7d3bfbf66d (diff) | |
download | logilab-common-1ca9be028235dfc29c4ec16e0912d86e5ad00367.tar.gz |
black && flake8
Diffstat (limited to 'logilab/common')
-rw-r--r-- | logilab/common/deprecation.py | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/logilab/common/deprecation.py b/logilab/common/deprecation.py index a57ae33..b5f40dd 100644 --- a/logilab/common/deprecation.py +++ b/logilab/common/deprecation.py @@ -28,6 +28,7 @@ from functools import wraps class DeprecationWrapper(object): """proxy to print a warning on access to any attribute of the wrapped object """ + def __init__(self, proxied, msg=None, version=None): self._proxied = proxied self._msg = msg @@ -77,11 +78,11 @@ def send_warning(reason, version=None, stacklevel=2, module_name=None): compatible version. """ if module_name and version: - reason = '[%s %s] %s' % (module_name, version, reason) + reason = "[%s %s] %s" % (module_name, version, reason) elif module_name: - reason = '[%s] %s' % (module_name, reason) + reason = "[%s] %s" % (module_name, reason) elif version: - reason = '[%s] %s' % (version, reason) + reason = "[%s] %s" % (version, reason) warn(reason, DeprecationWarning, stacklevel=stacklevel) @@ -94,10 +95,11 @@ def callable_renamed(old_name, new_function, version=None): >>> old_function = renamed('old_function', new_function) >>> old_function() - sample.py:57: DeprecationWarning: old_function has been renamed and is deprecated, uses new_function instead - old_function() + sample.py:57: DeprecationWarning: old_function has been renamed and is deprecated, uses + new_function instead old_function() >>> """ + @wraps(new_function) def wrapped(*args, **kwargs): send_warning( @@ -110,6 +112,7 @@ def callable_renamed(old_name, new_function, version=None): module_name=new_function.__module__, ) return new_function(*args, **kwargs) + return wrapped @@ -124,10 +127,11 @@ def argument_removed(old_argument_name, version=None): ... def some_function(new): ... return new >>> some_function(old=42) - sample.py:15: DeprecationWarning: argument old of callable some_function has been renamed and is deprecated, use keyword argument new instead - some_function(old=42) + sample.py:15: DeprecationWarning: argument old of callable some_function has been renamed and + is deprecated, use keyword argument new instead some_function(old=42) 42 """ + def _wrap(func): @wraps(func) def check_kwargs(*args, **kwargs): @@ -154,15 +158,17 @@ def callable_deprecated(reason=None, version=None, stacklevel=2): """Display a deprecation message only if the version is older than the compatible version. """ + def decorator(func): message = reason or 'The function "%s" is deprecated' - if '%s' in message: + if "%s" in message: message %= func.__name__ @wraps(func) def wrapped(*args, **kwargs): send_warning(message, version, stacklevel + 1, module_name=func.__module__) return func(*args, **kwargs) + return wrapped return decorator @@ -198,15 +204,18 @@ def attribute_renamed(old_name, new_name, version=None): >>> some_class = SomeClass() >>> print(some_class.old) - sample.py:15: DeprecationWarning: SomeClass.old has been renamed and is deprecated, use SomeClass.new instead + sample.py:15: DeprecationWarning: SomeClass.old has been renamed and is deprecated, use + SomeClass.new instead print(some_class.old) 42 >>> some_class.old = 43 - sample.py:16: DeprecationWarning: SomeClass.old has been renamed and is deprecated, use SomeClass.new instead + sample.py:16: DeprecationWarning: SomeClass.old has been renamed and is deprecated, use + SomeClass.new instead some_class.old = 43 >>> some_class.old == some_class.new True """ + def _class_wrap(klass): reason = ( f"{klass.__name__}.{old_name} has been renamed and is deprecated, use " @@ -240,17 +249,21 @@ def argument_renamed(old_name, new_name, version=None): ... def some_function(new): ... return new >>> some_function(old=42) - sample.py:15: DeprecationWarning: argument old of callable some_function has been renamed and is deprecated, use keyword argument new instead + sample.py:15: DeprecationWarning: argument old of callable some_function has been renamed and + is deprecated, use keyword argument new instead some_function(old=42) 42 """ + def _wrap(func): @wraps(func) def check_kwargs(*args, **kwargs): if old_name in kwargs and new_name in kwargs: - raise ValueError(f"argument {old_name} of callable {func.__name__} has been " - f"renamed to {new_name} but you are both using {old_name} and " - f"{new_name} has keyword arguments, only uses {new_name}") + raise ValueError( + f"argument {old_name} of callable {func.__name__} has been " + f"renamed to {new_name} but you are both using {old_name} and " + f"{new_name} has keyword arguments, only uses {new_name}" + ) if old_name in kwargs: send_warning( |