-
Notifications
You must be signed in to change notification settings - Fork 829
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace @deprecated decorator with upcoming native support …
…(via typing-extensions)
- Loading branch information
Showing
5 changed files
with
6 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,5 @@ | ||
import functools | ||
import inspect | ||
import warnings | ||
from warnings import warn | ||
|
||
string_types = (type(b""), type("")) | ||
|
||
|
||
def warn_deprecation(text): | ||
warnings.warn(text, category=DeprecationWarning, stacklevel=2) | ||
|
||
|
||
def deprecated(reason): | ||
""" | ||
This is a decorator which can be used to mark functions | ||
as deprecated. It will result in a warning being emitted | ||
when the function is used. | ||
""" | ||
|
||
if isinstance(reason, string_types): | ||
# The @deprecated is used with a 'reason'. | ||
# | ||
# .. code-block:: python | ||
# | ||
# @deprecated("please, use another function") | ||
# def old_function(x, y): | ||
# pass | ||
|
||
def decorator(func1): | ||
if inspect.isclass(func1): | ||
fmt1 = f"Call to deprecated class {func1.__name__} ({reason})." | ||
else: | ||
fmt1 = f"Call to deprecated function {func1.__name__} ({reason})." | ||
|
||
@functools.wraps(func1) | ||
def new_func1(*args, **kwargs): | ||
warn_deprecation(fmt1) | ||
return func1(*args, **kwargs) | ||
|
||
return new_func1 | ||
|
||
return decorator | ||
|
||
elif inspect.isclass(reason) or inspect.isfunction(reason): | ||
# The @deprecated is used without any 'reason'. | ||
# | ||
# .. code-block:: python | ||
# | ||
# @deprecated | ||
# def old_function(x, y): | ||
# pass | ||
|
||
func2 = reason | ||
|
||
if inspect.isclass(func2): | ||
fmt2 = f"Call to deprecated class {func2.__name__}." | ||
else: | ||
fmt2 = f"Call to deprecated function {func2.__name__}." | ||
|
||
@functools.wraps(func2) | ||
def new_func2(*args, **kwargs): | ||
warn_deprecation(fmt2) | ||
return func2(*args, **kwargs) | ||
|
||
return new_func2 | ||
|
||
else: | ||
raise TypeError(repr(type(reason))) | ||
def warn_deprecation(text: str): | ||
warn(text, category=DeprecationWarning, stacklevel=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters