| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,8 +20,7 @@ def deprecated(message, *, category=DeprecationWarning, stacklevel=1): | |||
| 20 | 20 | ||
| 21 | 21 | .. code-block:: python | |
| 22 | 22 | ||
| 23 | - from diffpy._deprecations import deprecated | ||
| 24 | - import warnings | ||
| 23 | + from diffpy.utils._deprecator import deprecated, d | ||
| 25 | 24 | ||
| 26 | 25 | @deprecated("old_function is deprecated; use new_function instead") | |
| 27 | 26 | def old_function(x, y): | |
@@ -38,12 +37,17 @@ def new_function(x, y): | |||
| 38 | 37 | ||
| 39 | 38 | .. code-block:: python | |
| 40 | 39 | ||
| 41 | - from diffpy._deprecations import deprecated | ||
| 40 | + from diffpy._deprecations import deprecated, deprecation_message | ||
| 42 | 41 | import warnings | |
| 43 | 42 | ||
| 44 | 43 | warnings.simplefilter("always", DeprecationWarning) | |
| 45 | 44 | ||
| 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) | ||
| 47 | 51 | class OldAtom: | |
| 48 | 52 | def __init__(self, symbol): | |
| 49 | 53 | self.symbol = symbol | |
@@ -52,8 +56,8 @@ class NewAtom: | |||
| 52 | 56 | def __init__(self, symbol): | |
| 53 | 57 | self.symbol = symbol | |
| 54 | 58 | ||
| 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 | ||
| 57 | 61 | """ | |
| 58 | 62 | if _builtin_deprecated is not None: | |
| 59 | 63 | return _builtin_deprecated( | |
@@ -75,34 +79,78 @@ def wrapper(*args, **kwargs): | |||
| 75 | 79 | return obj(*args, **kwargs) | |
| 76 | 80 | ||
| 77 | 81 | return wrapper | |
| 78 | - | ||
| 79 | 82 | raise TypeError( | |
| 80 | 83 | "deprecated decorator can only be applied to functions or classes" | |
| 81 | 84 | ) | |
| 82 | 85 | ||
| 83 | 86 | return decorator | |
| 84 | 87 | ||
| 85 | 88 | ||
| 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 | + ): | ||
| 87 | 92 | """Generate a deprecation message. | |
| 88 | 93 | ||
| 89 | 94 | Parameters | |
| 90 | 95 | ---------- | |
| 91 | - base : str | ||
| 96 | + old_base : str | ||
| 92 | 97 | The base module or class where the deprecated item resides. | |
| 98 | + This will look like the import statement used in the code | ||
| 99 | + currently | ||
| 93 | 100 | old_name : str | |
| 94 | 101 | The name of the deprecated item. | |
| 95 | 102 | new_name : str | |
| 96 | 103 | The name of the new item to use. | |
| 97 | 104 | removal_version : str | |
| 98 | 105 | 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. | ||
| 99 | 111 | ||
| 100 | 112 | Returns | |
| 101 | 113 | ------- | |
| 102 | 114 | str | |
| 103 | 115 | A formatted deprecation message. | |
| 104 | 116 | """ | |
| 117 | + if new_base is None: | ||
| 118 | + new_base = old_base | ||
| 105 | 119 | 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." | ||
| 108 | 155 | ) | |
| 156 | + return | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments