diff options
Diffstat (limited to 'logilab/common/deprecation.py')
-rw-r--r-- | logilab/common/deprecation.py | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/logilab/common/deprecation.py b/logilab/common/deprecation.py index af23f07..18027d7 100644 --- a/logilab/common/deprecation.py +++ b/logilab/common/deprecation.py @@ -25,6 +25,24 @@ from warnings import warn from functools import WRAPPER_ASSIGNMENTS, WRAPPER_UPDATES +def get_real__name__(some_callable): + """ + This is another super edge magic case which is needed because we uses + lazy_wraps because of logilab.common.modutils.LazyObject and because + __name__ has special behavior and doesn't work like a normal attribute and + that __getattribute__ of lazy_wraps is bypassed. + + Therefor, to get the real callable name when several lazy_wrapped + decorator are used we need to travers the __wrapped__ attributes chain. + """ + + targeted_callable = some_callable + while hasattr(targeted_callable, "__wrapped__"): + targeted_callable = targeted_callable.__wrapped__ # type: ignore + + return targeted_callable.__name__ + + def lazy_wraps(wrapped): """ This is the equivalent of the @wraps decorator of functools except it won't @@ -142,8 +160,8 @@ def callable_renamed(old_name, new_function, version=None): def wrapped(*args, **kwargs): send_warning( ( - f"{old_name} has been renamed and is deprecated, uses {new_function.__name__} " - f"instead" + f"{old_name} has been renamed and is deprecated, uses " + f"{get_real__name__(new_function)} instead" ), stacklevel=3, version=version, @@ -175,7 +193,7 @@ def argument_removed(old_argument_name, version=None): def check_kwargs(*args, **kwargs): if old_argument_name in kwargs: send_warning( - f"argument {old_argument_name} of callable {func.__name__} has been " + f"argument {old_argument_name} of callable {get_real__name__(func)} has been " f"removed and is deprecated", stacklevel=3, version=version, @@ -202,7 +220,7 @@ def callable_deprecated(reason=None, version=None, stacklevel=2): def wrapped(*args, **kwargs): message = reason or 'The function "%s" is deprecated' if "%s" in message: - message %= func.__name__ + message %= get_real__name__(func) send_warning(message, version, stacklevel + 1, module_name=func.__module__) return func(*args, **kwargs) @@ -306,14 +324,14 @@ def argument_renamed(old_name, new_name, version=None): 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"argument {old_name} of callable {get_real__name__(func)} 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( - f"argument {old_name} of callable {func.__name__} has been renamed " + f"argument {old_name} of callable {get_real__name__(func)} has been renamed " f"and is deprecated, use keyword argument {new_name} instead", stacklevel=3, version=version, |