|
12 | 12 |
|
13 | 13 | def deprecated(message, *, category=DeprecationWarning, stacklevel=1): |
14 | 14 | """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 | + """ |
16 | 58 | if _builtin_deprecated is not None: |
17 | 59 | return _builtin_deprecated( |
18 | 60 | message, category=category, stacklevel=stacklevel |
|
0 commit comments