-
Notifications
You must be signed in to change notification settings - Fork 999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🚀 [Feature] Add deprecated Decorator #3161
base: main
Are you sure you want to change the base?
Changes from all commits
1cc0193
eaa0ab2
1c5b43e
174425c
558e11e
c0fb0ab
9b64bbf
835c9c9
6a6251d
a7a43aa
2cb4648
89b1974
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright 2024 The HuggingFace Team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import functools | ||
import textwrap | ||
import warnings | ||
from typing import Callable, TypeVar | ||
|
||
from typing_extensions import ParamSpec | ||
|
||
|
||
_T = TypeVar("_T") | ||
_P = ParamSpec("_P") | ||
|
||
|
||
def deprecated(since: str, removed_in: str, instruction: str) -> Callable[[Callable[_P, _T]], Callable[_P, _T]]: | ||
"""Marks functions as deprecated. | ||
|
||
It will result in a warning when the function is called and a note in the docstring. | ||
|
||
Args: | ||
since (`str`): | ||
The version when the function was first deprecated. | ||
removed_in (`str`): | ||
The version when the function will be removed. | ||
instruction (`str`): | ||
The action users should take. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a nit: I'd find it more intuitive if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
Returns: | ||
`Callable`: A decorator that will mark the function as deprecated. | ||
""" | ||
|
||
def decorator(function: Callable[_P, _T]) -> Callable[_P, _T]: | ||
@functools.wraps(function) | ||
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _T: | ||
warnings.warn( | ||
f"'{function.__module__}.{function.__name__}' " | ||
f"is deprecated in version {since} and will be " | ||
f"removed in {removed_in}. {instruction}.", | ||
category=FutureWarning, | ||
stacklevel=2, | ||
) | ||
return function(*args, **kwargs) | ||
|
||
# Add a deprecation note to the docstring. | ||
docstring = function.__doc__ or "" | ||
|
||
deprecation_note = textwrap.dedent( | ||
f"""\ | ||
.. deprecated:: {since} | ||
Deprecated and will be removed in version {removed_in}. {instruction}. | ||
""" | ||
) | ||
|
||
# Split docstring at first occurrence of newline | ||
summary_and_body = docstring.split("\n\n", 1) | ||
if len(summary_and_body) > 1: | ||
summary, body = summary_and_body | ||
body = textwrap.dedent(body) | ||
new_docstring_parts = [deprecation_note, "\n\n", summary, body] | ||
else: | ||
summary = summary_and_body[0] | ||
new_docstring_parts = [deprecation_note, "\n\n", summary] | ||
|
||
wrapper.__doc__ = "".join(new_docstring_parts) | ||
|
||
return wrapper | ||
|
||
return decorator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can implement the decorator based on
warnings.deprecated
(since Python 3.13) ortyping_extensions.deprecated
. It has better linter and IDE support.