-
Notifications
You must be signed in to change notification settings - Fork 977
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
Open
yhna940
wants to merge
12
commits into
huggingface:main
Choose a base branch
from
yhna940:feature/deprecation-warning
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1cc0193
Add deprecate decorator
yhna940 eaa0ab2
Fix minor
yhna940 1c5b43e
Refactor test var name
yhna940 174425c
Fix minor
yhna940 558e11e
Del cmt
yhna940 c0fb0ab
Del prev warninigs
yhna940 9b64bbf
Merge main
yhna940 835c9c9
Apply style
yhna940 6a6251d
Merge remote-tracking branch 'origin/main' into feature/deprecation-w…
yhna940 a7a43aa
Rename instruction arg
yhna940 2cb4648
Del pls msg
yhna940 89b1974
Add cls test case
yhna940 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, instructions: 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. | ||
instructions (`str`): | ||
The action users should take. | ||
|
||
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}. Please {instructions}.", | ||
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}. Please {instructions}. | ||
""" | ||
) | ||
|
||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just a nit: I'd find it more intuitive if the
instructions
was renamedinstruction
and if the"Please "
was not added in front, as I would assume as a caller that I need to pass a full sentence here.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.
Done in a7a43aa and 2cb4648 Thank you :)