-
Notifications
You must be signed in to change notification settings - Fork 71
feat: added a way to mark funcs as deprecated (compatible py 3.9-3.14) #153
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
Merged
Merged
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,39 @@ | ||
| """ | ||
| This module provides a deprecated decorator for marking functions or classes as deprecated. | ||
| It first attempts to import the deprecated decorator from the warnings module, available in Python 3.13 and later. | ||
| If the import fails (indicating an earlier Python version), it defines a custom deprecated decorator. | ||
| The decorator from warnings issues a DeprecationWarning when the decorated object is used during runtime, | ||
| and triggers static linters to flag the usage as deprecated. | ||
| The custom decorator also issues a DeprecationWarning when the decorated object is used, but does not trigger static linters. | ||
| """ | ||
|
|
||
| try: | ||
| from warnings import deprecated # type: ignore [attr-defined] | ||
| except ImportError: | ||
| import functools | ||
| from typing import Any, Callable, Optional, Type, TypeVar, cast | ||
| from warnings import warn | ||
|
|
||
| F = TypeVar("F", bound=Callable[..., Any]) | ||
|
|
||
| def deprecated( | ||
| message: str, | ||
| /, | ||
| *, | ||
| category: Optional[Type[Warning]] = DeprecationWarning, | ||
| stacklevel: int = 1, | ||
| ) -> Callable[[F], F]: | ||
| """Indicate that a function is deprecated.""" | ||
|
|
||
| def decorator(func: F) -> F: | ||
| @functools.wraps(func) | ||
| def wrapper(*args: Any, **kwargs: Any) -> Any: | ||
| warn(message, category=category, stacklevel=stacklevel) | ||
| return func(*args, **kwargs) | ||
|
|
||
| return cast(F, wrapper) | ||
|
|
||
| return decorator | ||
|
|
||
|
|
||
| __all__ = ["deprecated"] |
This file contains hidden or 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 hidden or 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,89 @@ | ||
| """Tests for the deprecated decorator.""" | ||
|
|
||
| import warnings | ||
| from typing import Dict, Optional, Tuple | ||
|
|
||
| from language_tool_python._deprecated import deprecated | ||
|
|
||
|
|
||
| def test_deprecated_emits_warning() -> None: | ||
| """Test that the deprecated decorator emits a DeprecationWarning.""" | ||
|
|
||
| @deprecated("This function is deprecated") # type: ignore | ||
| def old_function() -> str: | ||
| return "result" | ||
|
|
||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| result = old_function() | ||
|
|
||
| assert len(w) == 1 | ||
| assert issubclass(w[0].category, DeprecationWarning) | ||
| assert "This function is deprecated" in str(w[0].message) | ||
| assert result == "result" | ||
|
|
||
|
|
||
| def test_deprecated_with_custom_category() -> None: | ||
| """Test that the deprecated decorator can use a custom warning category.""" | ||
|
|
||
| @deprecated("This is a user warning", category=UserWarning) # type: ignore | ||
| def old_function() -> int: | ||
| return 42 | ||
|
|
||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| result = old_function() | ||
|
|
||
| assert len(w) == 1 | ||
| assert issubclass(w[0].category, UserWarning) | ||
| assert "This is a user warning" in str(w[0].message) | ||
| assert result == 42 | ||
|
|
||
|
|
||
| def test_deprecated_preserves_function_signature() -> None: | ||
| """Test that the deprecated decorator preserves function metadata.""" | ||
|
|
||
| @deprecated("Old function") # type: ignore | ||
| def my_function(x: int, y: int) -> int: | ||
| """Add two numbers.""" | ||
| return x + y | ||
|
|
||
| with warnings.catch_warnings(record=True): | ||
| assert my_function.__name__ == "my_function" | ||
| assert my_function.__doc__ is not None | ||
| assert "Add two numbers" in my_function.__doc__ | ||
| assert my_function(2, 3) == 5 | ||
|
|
||
|
|
||
| def test_deprecated_with_multiple_calls() -> None: | ||
| """Test that warning is emitted on each call.""" | ||
|
|
||
| @deprecated("Deprecated function") # type: ignore | ||
| def func() -> str: | ||
| return "value" | ||
|
|
||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| func() | ||
| func() | ||
| func() | ||
|
|
||
| assert len(w) == 3 | ||
| assert all(issubclass(warning.category, DeprecationWarning) for warning in w) | ||
|
|
||
|
|
||
| def test_deprecated_with_args_and_kwargs() -> None: | ||
| """Test that deprecated decorator works with functions that have args and kwargs.""" | ||
|
|
||
| @deprecated("This function is obsolete") # type: ignore | ||
| def complex_function( | ||
| a: int, b: int, *args: int, c: Optional[int] = None, **kwargs: int | ||
| ) -> Tuple[int, int, Tuple[int, ...], Optional[int], Dict[str, int]]: | ||
| return (a, b, args, c, kwargs) | ||
|
|
||
| with warnings.catch_warnings(record=True) as w: | ||
| warnings.simplefilter("always") | ||
| result = complex_function(1, 2, 3, 4, c=5, d=6, e=7) | ||
|
|
||
| assert len(w) == 1 | ||
| assert result == (1, 2, (3, 4), 5, {"d": 6, "e": 7}) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.