-
-
Notifications
You must be signed in to change notification settings - Fork 970
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
Add typehint for deprecated and experimental #3575
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7e51dcf
Add typehint for deprecated and experimental
himkt 995ae13
Use Type and setattr instead of direct assignment
himkt 716c08c
Remove unintentionally added files
himkt 32b4567
Fix isort
himkt 03c81e4
Fix annotation
himkt f98a6de
Type puzzling
himkt 0e8249e
Divide deprecated into deprecated_class and deprecated_func
himkt 9e2584f
Divide experimental into experimental_class and experimental_func
himkt af98fd3
Update tests
himkt 5b0e11e
Black fix
himkt 488a560
Fix decoration
himkt 056104d
Merge branch 'master' into decorator-typehint
himkt cfbdfe5
Fix broken reference
himkt f7e7098
Run isort
himkt 131e7bc
Merge branch 'master' into decorator-typehint
himkt d928cde
Merge branch 'master' into decorator-typehint
himkt 983779a
Merge branch 'master' into decorator-typehint
himkt 7fd74ca
Update optuna/_deprecated.py
himkt 616dff2
Fix docstrings
himkt eb87f3d
Merge branch 'decorator-typehint' of ssh://github.com/himkt/optuna in…
himkt File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
@@ -1,17 +1,23 @@ | ||
import functools | ||
import inspect | ||
import textwrap | ||
from typing import Any | ||
from typing import Callable | ||
from typing import Optional | ||
from typing import TypeVar | ||
import warnings | ||
|
||
from packaging import version | ||
from typing_extensions import ParamSpec | ||
|
||
from optuna._experimental import _get_docstring_indent | ||
from optuna._experimental import _validate_version | ||
|
||
|
||
FT = TypeVar("FT") | ||
FP = ParamSpec("FP") | ||
CT = TypeVar("CT") | ||
|
||
|
||
_DEPRECATION_NOTE_TEMPLATE = """ | ||
|
||
.. warning:: | ||
|
@@ -41,21 +47,21 @@ def _format_text(text: str) -> str: | |
return "\n\n" + textwrap.indent(text.strip(), " ") + "\n" | ||
|
||
|
||
def deprecated( | ||
def deprecated_func( | ||
deprecated_version: str, | ||
removed_version: str, | ||
name: Optional[str] = None, | ||
text: Optional[str] = None, | ||
) -> Any: | ||
"""Decorate class or function as deprecated. | ||
) -> Callable[[Callable[FP, FT]], Callable[FP, FT]]: | ||
"""Decorate function as deprecated. | ||
|
||
Args: | ||
deprecated_version: | ||
The version in which the target feature is deprecated. | ||
removed_version: | ||
The version in which the target feature will be removed. | ||
name: | ||
The name of the feature. Defaults to the function or class name. Optional. | ||
The name of the feature. Defaults to the function name. Optional. | ||
text: | ||
The additional text for the deprecation note. The default note is build using specified | ||
``deprecated_version`` and ``removed_version``. If you want to provide additional | ||
|
@@ -75,52 +81,86 @@ def deprecated( | |
_validate_version(removed_version) | ||
_validate_two_version(deprecated_version, removed_version) | ||
|
||
def _deprecated_wrapper(f: Any) -> Any: | ||
# f is either func or class. | ||
def decorator(func: Callable[FP, FT]) -> Callable[FP, FT]: | ||
if func.__doc__ is None: | ||
func.__doc__ = "" | ||
|
||
note = _DEPRECATION_NOTE_TEMPLATE.format(d_ver=deprecated_version, r_ver=removed_version) | ||
if text is not None: | ||
note += _format_text(text) | ||
indent = _get_docstring_indent(func.__doc__) | ||
func.__doc__ = func.__doc__.strip() + textwrap.indent(note, indent) + indent | ||
|
||
def _deprecated_func(func: Callable[[Any], Any]) -> Callable[[Any], Any]: | ||
@functools.wraps(func) | ||
def wrapper(*args: Any, **kwargs: Any) -> FT: | ||
"""Decorates a function as deprecated. | ||
|
||
This decorator is supposed to be applied to the deprecated function. | ||
""" | ||
if func.__doc__ is None: | ||
func.__doc__ = "" | ||
|
||
note = _DEPRECATION_NOTE_TEMPLATE.format( | ||
d_ver=deprecated_version, r_ver=removed_version | ||
message = _DEPRECATION_WARNING_TEMPLATE.format( | ||
name=(name if name is not None else func.__name__), | ||
d_ver=deprecated_version, | ||
r_ver=removed_version, | ||
) | ||
if text is not None: | ||
note += _format_text(text) | ||
indent = _get_docstring_indent(func.__doc__) | ||
func.__doc__ = func.__doc__.strip() + textwrap.indent(note, indent) + indent | ||
message += " " + text | ||
warnings.warn(message, FutureWarning, stacklevel=2) | ||
|
||
# TODO(mamu): Annotate this correctly. | ||
@functools.wraps(func) | ||
def new_func(*args: Any, **kwargs: Any) -> Any: | ||
message = _DEPRECATION_WARNING_TEMPLATE.format( | ||
name=(name if name is not None else func.__name__), | ||
d_ver=deprecated_version, | ||
r_ver=removed_version, | ||
) | ||
if text is not None: | ||
message += " " + text | ||
warnings.warn(message, FutureWarning, stacklevel=2) | ||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorator | ||
|
||
return func(*args, **kwargs) # type: ignore | ||
|
||
return new_func | ||
def deprecated_class( | ||
deprecated_version: str, | ||
removed_version: str, | ||
name: Optional[str] = None, | ||
text: Optional[str] = None, | ||
) -> Callable[[CT], CT]: | ||
"""Decorate class as deprecated. | ||
|
||
Args: | ||
deprecated_version: | ||
The version in which the target feature is deprecated. | ||
removed_version: | ||
The version in which the target feature will be removed. | ||
name: | ||
The name of the feature. Defaults to the class name. Optional. | ||
text: | ||
The additional text for the deprecation note. The default note is build using specified | ||
``deprecated_version`` and ``removed_version``. If you want to provide additional | ||
information, please specify this argument yourself. | ||
|
||
.. note:: | ||
The default deprecation note is as follows: "Deprecated in v{d_ver}. This feature | ||
will be removed in the future. The removal of this feature is currently scheduled | ||
for v{r_ver}, but this schedule is subject to change. See | ||
https://github.com/optuna/optuna/releases/tag/v{d_ver}." | ||
|
||
.. note:: | ||
The specified text is concatenated after the default deprecation note. | ||
""" | ||
|
||
_validate_version(deprecated_version) | ||
_validate_version(removed_version) | ||
_validate_two_version(deprecated_version, removed_version) | ||
|
||
def _deprecated_class(cls: Any) -> Any: | ||
def decorator(cls: CT) -> CT: | ||
def wrapper(cls: CT) -> CT: | ||
"""Decorates a class as deprecated. | ||
|
||
This decorator is supposed to be applied to the deprecated class. | ||
""" | ||
_original_init = cls.__init__ | ||
_original_init = getattr(cls, "__init__") | ||
_original_name = getattr(cls, "__name__") | ||
|
||
@functools.wraps(_original_init) | ||
def wrapped_init(self, *args, **kwargs) -> None: # type: ignore | ||
message = _DEPRECATION_WARNING_TEMPLATE.format( | ||
name=(name if name is not None else cls.__name__), | ||
name=(name if name is not None else _original_name), | ||
d_ver=deprecated_version, | ||
r_ver=removed_version, | ||
) | ||
|
@@ -134,7 +174,7 @@ def wrapped_init(self, *args, **kwargs) -> None: # type: ignore | |
|
||
_original_init(self, *args, **kwargs) | ||
|
||
cls.__init__ = wrapped_init | ||
setattr(cls, "__init__", wrapped_init) | ||
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. python/mypy#2427 (comment) |
||
|
||
if cls.__doc__ is None: | ||
cls.__doc__ = "" | ||
|
@@ -149,6 +189,6 @@ def wrapped_init(self, *args, **kwargs) -> None: # type: ignore | |
|
||
return cls | ||
|
||
return _deprecated_class(f) if inspect.isclass(f) else _deprecated_func(f) | ||
return wrapper(cls) | ||
|
||
return _deprecated_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
Oops, something went wrong.
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.
does this break for older versions of typing_extensions, see https://stackoverflow.com/questions/71944041/using-python-code-typed-with-paramspec-on-older-versions-of-python @himkt