Skip to content

Commit 0db2da1

Browse files
authored
Merge pull request #366 from cadenmyers13/deprecated_update
fix: fix deprecator to exactly match python 3.13 API
2 parents 775250f + 3b7b13b commit 0db2da1

File tree

2 files changed

+64
-46
lines changed

2 files changed

+64
-46
lines changed

news/deprecated_update.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* No news needed.
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

src/diffpy/utils/_deprecator.py

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,79 +10,74 @@
1010
_builtin_deprecated = None
1111

1212

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.
3016
3117
Examples
3218
--------
19+
Basic usage with a deprecated function:
20+
3321
.. code-block:: python
3422
3523
from diffpy._deprecations import deprecated
24+
import warnings
3625
37-
# ------------------------------
38-
# Deprecated function
39-
# ------------------------------
40-
@deprecated(alt_name="new_function")
26+
@deprecated("old_function is deprecated; use new_function instead")
4127
def old_function(x, y):
4228
return x + y
4329
4430
def new_function(x, y):
4531
return x + y
4632
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)
5045
51-
# ------------------------------
52-
# Deprecated class
53-
# ------------------------------
54-
@deprecated(alt_name="NewAtom")
46+
@deprecated("OldAtom is deprecated; use NewAtom instead")
5547
class OldAtom:
5648
def __init__(self, symbol):
5749
self.symbol = symbol
5850
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
6554
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}"
7266
)
7367

68+
def decorator(obj):
69+
setattr(obj, "__deprecated__", message)
7470
if callable(obj):
7571

7672
@functools.wraps(obj)
7773
def wrapper(*args, **kwargs):
78-
warnings.warn(msg, DeprecationWarning, stacklevel=2)
74+
warnings.warn(message, category, stacklevel=stacklevel + 1)
7975
return obj(*args, **kwargs)
8076

8177
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+
)
8782

8883
return decorator

0 commit comments

Comments
 (0)