-
Notifications
You must be signed in to change notification settings - Fork 0
add a deprecation decorator #5
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c1323f8
add a deprecation decorator
ilia-kats 794ef34
typecheck-compatible version
ilia-kats e19dfc0
apply suggestions from code review
ilia-kats b918670
fix for Python 3.10
ilia-kats c772029
switch to inspect.getdoc instead of processing docstrings manually
ilia-kats 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 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
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| from ._deprecated import Deprecation, deprecated | ||
| from ._extensions import ExtensionNamespace, make_register_namespace_decorator | ||
| from ._version import __version__, __version_tuple__ |
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,79 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
| from inspect import getdoc | ||
| from typing import TYPE_CHECKING, TypeVar | ||
|
|
||
| if sys.version_info >= (3, 11): | ||
| from typing import LiteralString | ||
| else: | ||
| from typing_extensions import LiteralString | ||
|
|
||
| if sys.version_info >= (3, 13): | ||
| from warnings import deprecated as _deprecated | ||
| else: | ||
| from typing_extensions import deprecated as _deprecated | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Callable | ||
|
|
||
| F = TypeVar("F", bound=Callable) | ||
|
|
||
|
|
||
| class Deprecation(str): | ||
| """Utility class storing information on deprecated functionality. | ||
|
|
||
| Args: | ||
| version_deprecated: The version of the package where the functionality was deprecated. | ||
| msg: The deprecation message. | ||
| """ | ||
|
|
||
| def __new__(cls, version_deprecated: LiteralString, msg: LiteralString = "") -> LiteralString: | ||
| obj = super().__new__(cls, msg) | ||
| obj.version_deprecated = version_deprecated | ||
| return obj | ||
|
|
||
|
|
||
| def _deprecated_at(msg: Deprecation, *, category=FutureWarning, stacklevel=1) -> Callable[[F], F]: | ||
| """Decorator to indicate that a class, function, or overload is deprecated. | ||
|
|
||
| Wraps :func:`warnings.deprecated` and additionally modifies the docstring to include a deprecation notice. | ||
|
|
||
| Args: | ||
| msg: The deprecation message. | ||
| category: The category of the warning that will be emitted at runtime. | ||
| stacklevel: The stack level of the warning. | ||
|
|
||
| Examples: | ||
| >>> @deprecated(Deprecation("0.2", "Use bar() instead.")) | ||
| ... def foo(baz): | ||
| ... pass | ||
| """ | ||
|
|
||
| def decorate(func: F) -> F: | ||
| kind = "function" if func.__name__ == func.__qualname__ else "method" | ||
| warnmsg = f"The {kind} {func.__name__} is deprecated and will be removed in the future." | ||
|
|
||
| doc = getdoc(func) | ||
| docmsg = f".. version-deprecated:: {msg.version_deprecated}" | ||
| if len(msg) is not None: | ||
| docmsg += f"\n {msg}" | ||
| warnmsg += f" {msg}" | ||
|
|
||
| if doc is None: | ||
| doc = docmsg | ||
| else: | ||
| lines = doc.splitlines() | ||
| body = "\n".join(lines[1:]) | ||
| doc = f"{lines[0]}\n\n{docmsg}\n{body}" | ||
| func.__doc__ = doc | ||
|
|
||
| return _deprecated(warnmsg, category=category, stacklevel=stacklevel)(func) | ||
|
|
||
| return decorate | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| deprecated = _deprecated | ||
| else: | ||
| deprecated = _deprecated_at |
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,54 @@ | ||
| import pytest | ||
|
|
||
| from scverse_misc import Deprecation, deprecated | ||
|
|
||
|
|
||
| @pytest.fixture(params=[None, "Test message."]) | ||
| def msg(request: pytest.FixtureRequest): | ||
| return request.param | ||
|
|
||
|
|
||
| @pytest.fixture( | ||
| params=[ | ||
| None, | ||
| "Test function", | ||
| """Test function | ||
|
|
||
| This is a test. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| foo | ||
| bar | ||
| bar | ||
| baz | ||
| """, | ||
| ] | ||
| ) | ||
| def docstring(request): | ||
| return request.param | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def deprecated_func(msg, docstring): | ||
| def func(foo, bar): | ||
| return 42 | ||
|
|
||
| func.__doc__ = docstring | ||
| return deprecated(Deprecation("foo", msg))(func) | ||
|
|
||
|
|
||
| def test_deprecation_decorator(deprecated_func, docstring, msg): | ||
| with pytest.warns(FutureWarning, match="deprecated"): | ||
| assert deprecated_func(1, 2) == 42 | ||
|
|
||
| lines = deprecated_func.__doc__.expandtabs().splitlines() | ||
| if docstring is None: | ||
| assert lines[0].startswith(".. version-deprecated::") | ||
| else: | ||
| lines_orig = docstring.expandtabs().splitlines() | ||
| assert lines[0] == lines_orig[0] | ||
| assert len(lines[1].strip()) == 0 | ||
| assert lines[2].startswith(".. version-deprecated") | ||
| if msg is not None: | ||
| assert lines[3] == f" {msg}" |
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.
No need for that. We're following spec-0 so we're more or less already at Python 3.12+
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.
but mudata 0.3.x is still on 3.10+. I'll bump it for 0.4, but for now I'd prefer to keep it.
Uh oh!
There was an error while loading. Please reload this page.
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.
I think MuData received so many changes, it's fine to just release 0.4.0 next. The current MuData main branch can already be 3.12+.
But this is not a big problem (unless you have to adapt it again - see Phil's Generics suggestion) so whatever you think makes sense.
Thanks!
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.
Everything so far has been backwards compatible, so my plan currently is to release 0.3.4 after the deprecation PR has been merged, this will be the last 0.3.x release.