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

feature(ooo): add deprecated function decorator #778

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions ding/utils/deprecation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import functools
import textwrap
import warnings

Check warning on line 3 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L1-L3

Added lines #L1 - L3 were not covered by tests


def deprecated(since, removed_in, up_to=None):

Check warning on line 6 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L6

Added line #L6 was not covered by tests
ooooo-create marked this conversation as resolved.
Show resolved Hide resolved
"""Decorate a function to signify its deprecation.
ooooo-create marked this conversation as resolved.
Show resolved Hide resolved

Args:
since: The version when the function was first deprecated.
removed_in: The version when the function will be removed.
up_to: The new API users should use.
"""
ooooo-create marked this conversation as resolved.
Show resolved Hide resolved

def decorator(func):
existing_docstring = func.__doc__ or ""

Check warning on line 16 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L15-L16

Added lines #L15 - L16 were not covered by tests

deprecated_doc = f'.. deprecated:: {since}\n This will be removed in {removed_in}'

Check warning on line 18 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L18

Added line #L18 was not covered by tests

if up_to is not None:
deprecated_doc += f', please use `{up_to}` instead.'

Check warning on line 21 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L20-L21

Added lines #L20 - L21 were not covered by tests
else:
deprecated_doc += '.'

Check warning on line 23 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L23

Added line #L23 was not covered by tests

# split docstring at first occurrence of newline
summary_and_body = existing_docstring.split("\n", 1)

Check warning on line 26 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L26

Added line #L26 was not covered by tests
ooooo-create marked this conversation as resolved.
Show resolved Hide resolved

if len(summary_and_body) > 1:
summary, body = summary_and_body

Check warning on line 29 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L28-L29

Added lines #L28 - L29 were not covered by tests

body = textwrap.dedent(body)

Check warning on line 31 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L31

Added line #L31 was not covered by tests

new_docstring_parts = [deprecated_doc, "\n\n", summary, body]

Check warning on line 33 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L33

Added line #L33 was not covered by tests
else:
summary = summary_and_body[0]

Check warning on line 35 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L35

Added line #L35 was not covered by tests

new_docstring_parts = [deprecated_doc, "\n\n", summary]

Check warning on line 37 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L37

Added line #L37 was not covered by tests

func.__doc__ = "".join(new_docstring_parts)

Check warning on line 39 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L39

Added line #L39 was not covered by tests

@functools.wraps(func)
def wrapper(*args, **kwargs):
warning_msg = (

Check warning on line 43 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L41-L43

Added lines #L41 - L43 were not covered by tests
f'API `{func.__module__}.{func.__name__}` is deprecated since version {since} '
f'and will be removed in version {removed_in}'
)
if up_to is not None:
warning_msg += f", please use `{up_to}` instead."

Check warning on line 48 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L47-L48

Added lines #L47 - L48 were not covered by tests
else:
warning_msg += "."

Check warning on line 50 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L50

Added line #L50 was not covered by tests

warnings.warn(warning_msg, category=FutureWarning, stacklevel=2)
return func(*args, **kwargs)

Check warning on line 53 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L52-L53

Added lines #L52 - L53 were not covered by tests

return wrapper

Check warning on line 55 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L55

Added line #L55 was not covered by tests

return decorator

Check warning on line 57 in ding/utils/deprecation.py

View check run for this annotation

Codecov / codecov/patch

ding/utils/deprecation.py#L57

Added line #L57 was not covered by tests
ooooo-create marked this conversation as resolved.
Show resolved Hide resolved
Loading