@@ -20,8 +20,7 @@ def deprecated(message, *, category=DeprecationWarning, stacklevel=1):
2020
2121 .. code-block:: python
2222
23- from diffpy._deprecations import deprecated
24- import warnings
23+ from diffpy.utils._deprecator import deprecated, d
2524
2625 @deprecated("old_function is deprecated; use new_function instead")
2726 def old_function(x, y):
@@ -38,12 +37,17 @@ def new_function(x, y):
3837
3938 .. code-block:: python
4039
41- from diffpy._deprecations import deprecated
40+ from diffpy._deprecations import deprecated, deprecation_message
4241 import warnings
4342
4443 warnings.simplefilter("always", DeprecationWarning)
4544
46- @deprecated("OldAtom is deprecated; use NewAtom instead")
45+ deprecation_warning = build_deprecation_message("diffpy.utils",
46+ "OldAtom",
47+ "NewAtom",
48+ "4.0.0")
49+
50+ @deprecated(deprecation_warning)
4751 class OldAtom:
4852 def __init__(self, symbol):
4953 self.symbol = symbol
@@ -52,8 +56,8 @@ class NewAtom:
5256 def __init__(self, symbol):
5357 self.symbol = symbol
5458
55- a = OldAtom("C") # Emits DeprecationWarning
56- b = NewAtom("C") # No warning
59+ a = OldAtom("C") # Works but emits DeprecationWarning
60+ b = NewAtom("C") # Works with no warning
5761 """
5862 if _builtin_deprecated is not None :
5963 return _builtin_deprecated (
@@ -75,34 +79,78 @@ def wrapper(*args, **kwargs):
7579 return obj (* args , ** kwargs )
7680
7781 return wrapper
78-
7982 raise TypeError (
8083 "deprecated decorator can only be applied to functions or classes"
8184 )
8285
8386 return decorator
8487
8588
86- def deprecation_message (base , old_name , new_name , removal_version ):
89+ def build_deprecation_message (
90+ old_base , old_name , new_name , removal_version , new_base = None
91+ ):
8792 """Generate a deprecation message.
8893
8994 Parameters
9095 ----------
91- base : str
96+ old_base : str
9297 The base module or class where the deprecated item resides.
98+ This will look like the import statement used in the code
99+ currently
93100 old_name : str
94101 The name of the deprecated item.
95102 new_name : str
96103 The name of the new item to use.
97104 removal_version : str
98105 The version when the deprecated item will be removed.
106+ new_base : str Optional. Defaults to old_base.
107+ The base module or class where the new item resides.
108+ This will look like the import statement that
109+ will be used in the code moving forward. If not specified,
110+ the new base defaults to the old one.
99111
100112 Returns
101113 -------
102114 str
103115 A formatted deprecation message.
104116 """
117+ if new_base is None :
118+ new_base = old_base
105119 return (
106- f"'{ base } .{ old_name } ' is deprecated and will be removed in "
107- f"version { removal_version } . Please use '{ base } .{ new_name } ' instead."
120+ f"'{ old_base } .{ old_name } ' is deprecated and will be removed in "
121+ f"version { removal_version } . Please use '{ new_base } .{ new_name } ' "
122+ f"instead."
123+ )
124+
125+
126+ def generate_deprecation_docstring (new_name , removal_version , new_base = None ):
127+ """Generate a docstring for copy-pasting into a deprecated function.
128+
129+ this function will print the text to the terminal for copy-pasting
130+
131+ usage:
132+ python
133+ >>> import diffpy.utils._deprecator.generate_deprecation_docstring as gdd
134+ >>> gdd("new_name", "4.0.0")
135+
136+ Parameters
137+ ----------
138+ new_name: str
139+ The name of the new function or class to replace the existing one
140+ removal_version: str
141+ The version when the deprecated item is targeted for removal,
142+ e.g., 4.0.0
143+ new_base: str Optional. Defaults to old_base.
144+ The new base for importing. The new import statement would look like
145+ "from new_base import new_name"
146+
147+ Returns
148+ -------
149+ None
150+ """
151+ print (
152+ f"This function has been deprecated and will be "
153+ f"removed in version { removal_version } . Please use"
154+ f"{ new_base } .{ new_name } instead."
108155 )
156+ return
0 commit comments