Skip to content

Commit

Permalink
Merge pull request #14 from AutoResearch/feat/add-general-deprecate-f…
Browse files Browse the repository at this point in the history
…unction

docs: use new `deprecate` function in deprecated_alias
  • Loading branch information
hollandjg authored Jul 3, 2023
2 parents b4a1e55 + 150b597 commit 0942a1d
Showing 1 changed file with 45 additions and 7 deletions.
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

0 comments on commit 0942a1d

Please sign in to comment.