Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: use new deprecate function in deprecated_alias #14

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 45 additions & 7 deletions src/autora/utils/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,48 @@
_logger = logging.getLogger(__name__)


def deprecate(
f: Callable,
message: str,
callback: Callable = _logger.warning,
):
"""
Wrapper to make function aliases which print a warning that a name is an alias.

Args:
f: the function to be aliased
message: the message to be emitted when the deprecated code is used
callback: a function to call to handle the warning message

Examples:
>>> def original():
... return 1
>>> deprecated = deprecate(original, "`original` is deprecated.")

The original function is unaffected:
>>> original()
1

The aliased function works the same way, but also emits a warning.
>>> deprecated() # doctest: +SKIP
`original` is deprecated.
1

You can also set a custom callback instead of the default "warning":
>>> a0 = deprecate(original, "`original` is deprecated.", callback=print)
>>> a0()
`original` is deprecated.
1
"""

@wraps(f)
def wrapper(*args, **kwds):
callback(message)
return f(*args, **kwds)

return wrapper


def deprecated_alias(
f: Callable, alias_name: str, callback: Callable = _logger.warning
):
Expand Down Expand Up @@ -44,10 +86,6 @@ def deprecated_alias(
alternative message
1
"""

@wraps(f)
def wrapper(*args, **kwds):
callback("Use `%s` instead. `%s` is deprecated." % (f.__name__, alias_name))
return f(*args, **kwds)

return wrapper
message = "Use `%s` instead. `%s` is deprecated." % (f.__name__, alias_name)
wrapped = deprecate(f=f, message=message, callback=callback)
return wrapped