-
-
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 3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,21 @@ | |
from typing import Any | ||
from typing import Callable | ||
from typing import Optional | ||
from typing import overload | ||
from typing import Type | ||
from typing import TypeVar | ||
from typing import Union | ||
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 = """ | ||
|
||
|
@@ -46,7 +54,7 @@ def deprecated( | |
removed_version: str, | ||
name: Optional[str] = None, | ||
text: Optional[str] = None, | ||
) -> Any: | ||
) -> Union[Callable[[CT], CT], Callable[FP, FT]]: | ||
"""Decorate class or function as deprecated. | ||
|
||
Args: | ||
|
@@ -75,10 +83,18 @@ def deprecated( | |
_validate_version(removed_version) | ||
_validate_two_version(deprecated_version, removed_version) | ||
|
||
@overload | ||
def _deprecated_wrapper(f: Callable[FP, FT]) -> Callable[FP, FT]: | ||
... | ||
|
||
@overload | ||
def _deprecated_wrapper(f: CT) -> CT: | ||
... | ||
|
||
def _deprecated_wrapper(f: Any) -> Any: | ||
# f is either func or class. | ||
|
||
def _deprecated_func(func: Callable[[Any], Any]) -> Callable[[Any], Any]: | ||
def _deprecated_func(func: Callable[FP, FT]) -> Callable[FP, FT]: | ||
"""Decorates a function as deprecated. | ||
|
||
This decorator is supposed to be applied to the deprecated function. | ||
|
@@ -106,11 +122,11 @@ def new_func(*args: Any, **kwargs: Any) -> Any: | |
message += " " + text | ||
warnings.warn(message, FutureWarning, stacklevel=2) | ||
|
||
return func(*args, **kwargs) # type: ignore | ||
return func(*args, **kwargs) | ||
|
||
return new_func | ||
|
||
def _deprecated_class(cls: Any) -> Any: | ||
def _deprecated_class(cls: Type[CT]) -> Type[CT]: | ||
"""Decorates a class as deprecated. | ||
|
||
This decorator is supposed to be applied to the deprecated class. | ||
|
@@ -134,7 +150,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__ = "" | ||
|
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.
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