Decorate a function to signify its deprecation This function wraps a method that will soon be removed and does two things: * The docstring of the method will be modified to include a notice about deprecation, e.g., "Deprecated since 0.9.11. Use foo instead." * Raises
(deprecated_in: str, removed_in=None, current_version=None,
details="")
| 113 | |
| 114 | |
| 115 | def deprecated(deprecated_in: str, removed_in=None, current_version=None, |
| 116 | details=""): |
| 117 | """Decorate a function to signify its deprecation |
| 118 | |
| 119 | This function wraps a method that will soon be removed and does two things: |
| 120 | |
| 121 | * The docstring of the method will be modified to include a notice |
| 122 | about deprecation, e.g., "Deprecated since 0.9.11. Use foo instead." |
| 123 | * Raises a :class:`~deprecation.DeprecatedWarning` |
| 124 | via the :mod:`warnings` module, which is a subclass of the built-in |
| 125 | :class:`DeprecationWarning`. Note that built-in |
| 126 | :class:`DeprecationWarning` are ignored by default, so for users |
| 127 | to be informed of said warnings they will need to enable them--see |
| 128 | the :mod:`warnings` module documentation for more details. |
| 129 | |
| 130 | :param deprecated_in: The version at which the decorated method is |
| 131 | considered deprecated. This will usually be the |
| 132 | next version to be released when the decorator is |
| 133 | added. |
| 134 | :param removed_in: The version or :class:`datetime.date` when the decorated |
| 135 | method will be removed. The default is **None**, |
| 136 | specifying that the function is not currently planned |
| 137 | to be removed. |
| 138 | :param current_version: The source of version information for the |
| 139 | currently running code. This will usually be |
| 140 | a `__version__` attribute on your library. |
| 141 | The default is `None`. |
| 142 | When `current_version=None` the automation to |
| 143 | determine if the wrapped function is actually |
| 144 | in a period of deprecation or time for removal |
| 145 | does not work, causing a |
| 146 | :class:`~deprecation.DeprecatedWarning` |
| 147 | to be raised in all cases. |
| 148 | :param details: Extra details to be added to the method docstring and |
| 149 | warning. For example, the details may point users to |
| 150 | a replacement method, such as "Use the foo_bar |
| 151 | method instead". By default there are no details. |
| 152 | """ |
| 153 | # You can't just jump to removal. It's weird, unfair, and also makes |
| 154 | # building up the docstring weird. |
| 155 | if deprecated_in is None and removed_in is not None: |
| 156 | raise TypeError("Cannot set removed_in to a value " |
| 157 | "without also setting deprecated_in") |
| 158 | |
| 159 | # Only warn when it's appropriate. There may be cases when it makes sense |
| 160 | # to add this decorator before a formal deprecation period begins. |
| 161 | # In CPython, PendingDeprecatedWarning gets used in that period, |
| 162 | # so perhaps mimick that at some point. |
| 163 | is_deprecated = False |
| 164 | is_unsupported = False |
| 165 | |
| 166 | # StrictVersion won't take a None or a "", so make whatever goes to it |
| 167 | # is at least *something*. Compare versions only if removed_in is not |
| 168 | # of type datetime.date |
| 169 | if isinstance(removed_in, date): |
| 170 | if date.today() >= removed_in: |
| 171 | is_unsupported = True |
| 172 | else: |
nothing calls this directly
no test coverage detected