|
2 | 2 | import warnings |
3 | 3 |
|
4 | 4 | # Deprecated decorator is available for Python 3.13+, once |
5 | | -# Support for earlier versions is dropped, this custom implementation can be removed. |
| 5 | +# Support for earlier versions is dropped, this custom |
| 6 | +# implementation can be removed. |
6 | 7 | try: |
7 | 8 | from warnings import deprecated as _builtin_deprecated |
8 | 9 | except ImportError: |
9 | 10 | _builtin_deprecated = None |
10 | 11 |
|
| 12 | + |
11 | 13 | def deprecated(*, alt_name=None, message=None): |
12 | | - """ |
13 | | - Marks a function or class as deprecated. |
| 14 | + """Marks a function or class as deprecated. |
14 | 15 |
|
15 | 16 | Emits a DeprecationWarning whenever the decorated function is called |
16 | 17 | or the decorated class is instantiated. |
@@ -64,15 +65,24 @@ def __init__(self, symbol): |
64 | 65 |
|
65 | 66 | def decorator(obj): |
66 | 67 | name = getattr(obj, "__name__", repr(obj)) |
67 | | - msg = message or (f"'{name}' is deprecated. Use '{alt_name}' instead." |
68 | | - if alt_name else f"'{name}' is deprecated.") |
| 68 | + msg = message or ( |
| 69 | + f"'{name}' is deprecated. Use '{alt_name}' instead." |
| 70 | + if alt_name |
| 71 | + else f"'{name}' is deprecated." |
| 72 | + ) |
69 | 73 |
|
70 | 74 | if callable(obj): |
| 75 | + |
71 | 76 | @functools.wraps(obj) |
72 | 77 | def wrapper(*args, **kwargs): |
73 | 78 | warnings.warn(msg, DeprecationWarning, stacklevel=2) |
74 | 79 | return obj(*args, **kwargs) |
| 80 | + |
75 | 81 | return wrapper |
76 | 82 | else: |
77 | | - raise TypeError("deprecated decorator can only be applied to functions or classes") |
| 83 | + raise TypeError( |
| 84 | + "deprecated decorator can only be applied to functions or " |
| 85 | + "classes" |
| 86 | + ) |
| 87 | + |
78 | 88 | return decorator |
0 commit comments