|
10 | 10 | _builtin_deprecated = None |
11 | 11 |
|
12 | 12 |
|
13 | | -def deprecated(*, alt_name=None, message=None): |
14 | | - """Marks a function or class as deprecated. |
15 | | -
|
16 | | - Emits a DeprecationWarning whenever the decorated function is called |
17 | | - or the decorated class is instantiated. |
18 | | -
|
19 | | - Parameters |
20 | | - ---------- |
21 | | - alt_name : str, optional |
22 | | - Name of the recommended alternative. |
23 | | - message : str, optional |
24 | | - Custom deprecation message. If None, a default message is generated. |
25 | | -
|
26 | | - Returns |
27 | | - ------- |
28 | | - decorator : function |
29 | | - Decorator that wraps the deprecated object. |
| 13 | +def deprecated(message, *, category=DeprecationWarning, stacklevel=1): |
| 14 | + """Deprecation decorator for functions and classes that is compatible with |
| 15 | + Python versions prior to 3.13. |
30 | 16 |
|
31 | 17 | Examples |
32 | 18 | -------- |
| 19 | + Basic usage with a deprecated function: |
| 20 | +
|
33 | 21 | .. code-block:: python |
34 | 22 |
|
35 | 23 | from diffpy._deprecations import deprecated |
| 24 | + import warnings |
36 | 25 |
|
37 | | - # ------------------------------ |
38 | | - # Deprecated function |
39 | | - # ------------------------------ |
40 | | - @deprecated(alt_name="new_function") |
| 26 | + @deprecated("old_function is deprecated; use new_function instead") |
41 | 27 | def old_function(x, y): |
42 | 28 | return x + y |
43 | 29 |
|
44 | 30 | def new_function(x, y): |
45 | 31 | return x + y |
46 | 32 |
|
47 | | - # Usage |
48 | | - old_function(1, 2) # Emits DeprecationWarning |
49 | | - new_function(1, 2) # No warning |
| 33 | + old_function(1, 2) # Emits DeprecationWarning |
| 34 | + new_function(1, 2) # No warning |
| 35 | +
|
| 36 | +
|
| 37 | + Deprecating a class: |
| 38 | +
|
| 39 | + .. code-block:: python |
| 40 | +
|
| 41 | + from diffpy._deprecations import deprecated |
| 42 | + import warnings |
| 43 | +
|
| 44 | + warnings.simplefilter("always", DeprecationWarning) |
50 | 45 |
|
51 | | - # ------------------------------ |
52 | | - # Deprecated class |
53 | | - # ------------------------------ |
54 | | - @deprecated(alt_name="NewAtom") |
| 46 | + @deprecated("OldAtom is deprecated; use NewAtom instead") |
55 | 47 | class OldAtom: |
56 | 48 | def __init__(self, symbol): |
57 | 49 | self.symbol = symbol |
58 | 50 |
|
59 | | - # Usage |
60 | | - a = OldAtom("C") # Emits DeprecationWarning |
61 | | - atom = NewAtom("C") # No warning |
62 | | - """ |
63 | | - if _builtin_deprecated: |
64 | | - return _builtin_deprecated |
| 51 | + class NewAtom: |
| 52 | + def __init__(self, symbol): |
| 53 | + self.symbol = symbol |
65 | 54 |
|
66 | | - def decorator(obj): |
67 | | - name = getattr(obj, "__name__", repr(obj)) |
68 | | - msg = message or ( |
69 | | - f"'{name}' is deprecated. Use '{alt_name}' instead." |
70 | | - if alt_name |
71 | | - else f"'{name}' is deprecated." |
| 55 | + a = OldAtom("C") # Emits DeprecationWarning |
| 56 | + b = NewAtom("C") # No warning |
| 57 | + """ |
| 58 | + if _builtin_deprecated is not None: |
| 59 | + return _builtin_deprecated( |
| 60 | + message, category=category, stacklevel=stacklevel |
| 61 | + ) |
| 62 | + if not isinstance(message, str): |
| 63 | + raise TypeError( |
| 64 | + f"Expected an object of type str for 'message', not " |
| 65 | + f"{type(message).__name__!r}" |
72 | 66 | ) |
73 | 67 |
|
| 68 | + def decorator(obj): |
| 69 | + setattr(obj, "__deprecated__", message) |
74 | 70 | if callable(obj): |
75 | 71 |
|
76 | 72 | @functools.wraps(obj) |
77 | 73 | def wrapper(*args, **kwargs): |
78 | | - warnings.warn(msg, DeprecationWarning, stacklevel=2) |
| 74 | + warnings.warn(message, category, stacklevel=stacklevel + 1) |
79 | 75 | return obj(*args, **kwargs) |
80 | 76 |
|
81 | 77 | return wrapper |
82 | | - else: |
83 | | - raise TypeError( |
84 | | - "deprecated decorator can only be applied to functions or " |
85 | | - "classes" |
86 | | - ) |
| 78 | + |
| 79 | + raise TypeError( |
| 80 | + "deprecated decorator can only be applied to functions or classes" |
| 81 | + ) |
87 | 82 |
|
88 | 83 | return decorator |
0 commit comments