Skip to content
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

deprecation attr #15310

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions astropy/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def deprecated_func(*args, **kwargs):
deprecated_func = functools.wraps(func)(deprecated_func)

deprecated_func.__doc__ = deprecate_doc(deprecated_func.__doc__, message)
deprecated_func.__deprecated__ = message

return func_wrapper(deprecated_func)

Expand All @@ -160,6 +161,7 @@ def deprecate_class(cls, message, warning_type=warning_type):
with pickle and will look weird in the Sphinx docs.
"""
cls.__doc__ = deprecate_doc(cls.__doc__, message)
cls.__deprecated__ = message
if cls.__new__ is object.__new__:
cls.__init__ = deprecate_function(
get_function(cls.__init__), message, warning_type
Expand Down
27 changes: 27 additions & 0 deletions astropy/utils/tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ def test_deprecated_class():
assert "function" not in TA.__init__.__doc__
assert "deprecated" in TA.__init__.__doc__

# Test that the ``__deprecated__`` attribute is set
# See https://peps.python.org/pep-0702/ for more information
assert (
TA.__deprecated__
== "The TA class is deprecated and may be removed in a future version."
)

# Make sure the object is picklable
pickle.dumps(TA)

Expand Down Expand Up @@ -257,12 +264,32 @@ def C(cls):
if A.__doc__ is not None:
assert "deprecated" in A.B.__doc__

# Test that the ``__deprecated__`` attribute is set
# See https://peps.python.org/pep-0702/ for more information
assert (
A.B.__deprecated__
== "The B method is deprecated and may be removed in a future version."
)
# And that it is not set on the original function which doesn't have
# the deprecation warning.
assert not hasattr(A.B.__wrapped__, "__deprecated__")

with pytest.warns(AstropyDeprecationWarning) as w:
A.C()
assert len(w) == 1
if A.__doc__ is not None:
assert "deprecated" in A.C.__doc__

# Test that the ``__deprecated__`` attribute is set
# See https://peps.python.org/pep-0702/ for more information
assert (
A.C.__deprecated__
== "The C method is deprecated and may be removed in a future version."
)
# And that it is not set on the original function which doesn't have
# the deprecation warning.
assert not hasattr(A.C.__wrapped__, "__deprecated__")


def test_deprecated_argument():
# Tests the decorator with function, method, staticmethod and classmethod.
Expand Down
2 changes: 2 additions & 0 deletions docs/changes/utils/15310.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``astropy.utils.decorators.deprecated`` now adds the ``__deprecated__`` attribute to
the objects it wraps, following the practice in https://peps.python.org/pep-0702/.