Skip to content

Commit 3b7b13b

Browse files
committed
examples in docstring
1 parent d3c6e87 commit 3b7b13b

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

src/diffpy/utils/_deprecator.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,49 @@
1212

1313
def deprecated(message, *, category=DeprecationWarning, stacklevel=1):
1414
"""Deprecation decorator for functions and classes that is compatible with
15-
Python versions prior to 3.13."""
15+
Python versions prior to 3.13.
16+
17+
Examples
18+
--------
19+
Basic usage with a deprecated function:
20+
21+
.. code-block:: python
22+
23+
from diffpy._deprecations import deprecated
24+
import warnings
25+
26+
@deprecated("old_function is deprecated; use new_function instead")
27+
def old_function(x, y):
28+
return x + y
29+
30+
def new_function(x, y):
31+
return x + y
32+
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)
45+
46+
@deprecated("OldAtom is deprecated; use NewAtom instead")
47+
class OldAtom:
48+
def __init__(self, symbol):
49+
self.symbol = symbol
50+
51+
class NewAtom:
52+
def __init__(self, symbol):
53+
self.symbol = symbol
54+
55+
a = OldAtom("C") # Emits DeprecationWarning
56+
b = NewAtom("C") # No warning
57+
"""
1658
if _builtin_deprecated is not None:
1759
return _builtin_deprecated(
1860
message, category=category, stacklevel=stacklevel

0 commit comments

Comments
 (0)