|
| 1 | +import argparse |
1 | 2 | import functools |
2 | 3 | import warnings |
3 | 4 |
|
@@ -134,3 +135,73 @@ def build_deprecation_message( |
134 | 135 | f"version {removal_version}. Please use '{new_base}.{new_name}' " |
135 | 136 | f"instead." |
136 | 137 | ) |
| 138 | + |
| 139 | + |
| 140 | +def generate_deprecation_docstring(new_name, removal_version, new_base=None): |
| 141 | + """Generate a docstring for copy-pasting into a deprecated function. |
| 142 | +
|
| 143 | + This function will print the text to the terminal for copy-pasting. |
| 144 | +
|
| 145 | + Usage |
| 146 | + ----- |
| 147 | + >>> from diffpy.utils._deprecator import generate_deprecation_docstring |
| 148 | + >>> generate_deprecation_docstring("new_name", "4.0.0") |
| 149 | +
|
| 150 | + The message looks like: |
| 151 | + This function has been deprecated and will be removed in version |
| 152 | + {removal_version}. Please use {new_base}.{new_name} instead. |
| 153 | +
|
| 154 | + Parameters |
| 155 | + ---------- |
| 156 | + new_name : str |
| 157 | + The name of the new function or class to replace the existing one. |
| 158 | + removal_version : str |
| 159 | + The version when the deprecated item is targeted for removal, |
| 160 | + e.g., 4.0.0. |
| 161 | + new_base : str, optional |
| 162 | + The new base for importing. The new import statement would look like |
| 163 | + "from new_base import new_name". Defaults to None. |
| 164 | +
|
| 165 | + Returns |
| 166 | + ------- |
| 167 | + None |
| 168 | + """ |
| 169 | + print( |
| 170 | + f"This function has been deprecated and will be removed in version " |
| 171 | + f"{removal_version}.\n" |
| 172 | + f"Please use {new_base}.{new_name} instead." |
| 173 | + ) |
| 174 | + return |
| 175 | + |
| 176 | + |
| 177 | +def main(): |
| 178 | + parser = argparse.ArgumentParser( |
| 179 | + description="""\ |
| 180 | +Print a docstring for copy-pasting into a deprecated function. |
| 181 | +
|
| 182 | +Example usage |
| 183 | +------------- |
| 184 | + python -m diffpy.utils._deprecator load_data 4.0.0 -n diffpy.utils.parsers |
| 185 | +""", |
| 186 | + formatter_class=argparse.RawTextHelpFormatter, |
| 187 | + ) |
| 188 | + parser.add_argument("new_name", help="Name of the new function.") |
| 189 | + parser.add_argument( |
| 190 | + "removal_version", |
| 191 | + help="Version when the deprecated item will be removed.", |
| 192 | + ) |
| 193 | + parser.add_argument( |
| 194 | + "-n", "--new-base", default=None, help="New import base." |
| 195 | + ) |
| 196 | + |
| 197 | + args = parser.parse_args() |
| 198 | + |
| 199 | + generate_deprecation_docstring( |
| 200 | + new_name=args.new_name, |
| 201 | + removal_version=args.removal_version, |
| 202 | + new_base=args.new_base, |
| 203 | + ) |
| 204 | + |
| 205 | + |
| 206 | +if __name__ == "__main__": |
| 207 | + main() |
0 commit comments