Skip to content

Commit c795c45

Browse files
committed
Refactor deprecation helpers out into decorators
1 parent 8f62b99 commit c795c45

File tree

3 files changed

+66
-52
lines changed

3 files changed

+66
-52
lines changed

parcels/utils/_decorators.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""Utilities to help with deprecations."""
2+
3+
from __future__ import annotations
4+
5+
import functools
6+
import inspect
7+
import warnings
8+
from collections.abc import Callable
9+
from datetime import timedelta
10+
11+
import numpy as np
12+
13+
PACKAGE = "Parcels"
14+
15+
16+
def deprecated(msg: str = "") -> Callable:
17+
"""Decorator marking a function as being deprecated
18+
19+
Parameters
20+
----------
21+
msg : str, optional
22+
Custom message to append to the deprecation warning.
23+
24+
Examples
25+
--------
26+
```
27+
@deprecated("Please use `another_function` instead")
28+
def some_old_function(x, y):
29+
return x + y
30+
31+
@deprecated()
32+
def some_other_old_function(x, y):
33+
return x + y
34+
```
35+
"""
36+
if msg:
37+
msg = " " + msg
38+
39+
def decorator(func):
40+
@functools.wraps(func)
41+
def wrapper(*args, **kwargs):
42+
msg_formatted = (
43+
f"`{func.__qualname__}` is deprecated and will be removed in a future release of {PACKAGE}.{msg}"
44+
)
45+
46+
warnings.warn(msg_formatted, category=DeprecationWarning, stacklevel=3)
47+
return func(*args, **kwargs)
48+
49+
_patch_docstring(wrapper, f"\n\n.. deprecated:: {msg}")
50+
return wrapper
51+
52+
return decorator
53+
54+
55+
def deprecated_made_private(func: Callable) -> Callable:
56+
return deprecated(
57+
"It has moved to the internal API as it is not expected to be directly used by "
58+
"the end-user. If you feel that you use this code directly in your scripts, please "
59+
"comment on our tracking issue at https://github.com/OceanParcels/Parcels/issues/1695.",
60+
)(func)
61+
62+
63+
def _patch_docstring(obj: Callable, extra: str) -> None:
64+
obj.__doc__ = f"{obj.__doc__ or ''}{extra}".strip()

parcels/utils/_helpers.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,6 @@
1313
PACKAGE = "Parcels"
1414

1515

16-
def deprecated(msg: str = "") -> Callable:
17-
"""Decorator marking a function as being deprecated
18-
19-
Parameters
20-
----------
21-
msg : str, optional
22-
Custom message to append to the deprecation warning.
23-
24-
Examples
25-
--------
26-
```
27-
@deprecated("Please use `another_function` instead")
28-
def some_old_function(x, y):
29-
return x + y
30-
31-
@deprecated()
32-
def some_other_old_function(x, y):
33-
return x + y
34-
```
35-
"""
36-
if msg:
37-
msg = " " + msg
38-
39-
def decorator(func):
40-
@functools.wraps(func)
41-
def wrapper(*args, **kwargs):
42-
msg_formatted = (
43-
f"`{func.__qualname__}` is deprecated and will be removed in a future release of {PACKAGE}.{msg}"
44-
)
45-
46-
warnings.warn(msg_formatted, category=DeprecationWarning, stacklevel=3)
47-
return func(*args, **kwargs)
48-
49-
patch_docstring(wrapper, f"\n\n.. deprecated:: {msg}")
50-
return wrapper
51-
52-
return decorator
53-
54-
55-
def deprecated_made_private(func: Callable) -> Callable:
56-
return deprecated(
57-
"It has moved to the internal API as it is not expected to be directly used by "
58-
"the end-user. If you feel that you use this code directly in your scripts, please "
59-
"comment on our tracking issue at https://github.com/OceanParcels/Parcels/issues/1695.",
60-
)(func)
61-
62-
63-
def patch_docstring(obj: Callable, extra: str) -> None:
64-
obj.__doc__ = f"{obj.__doc__ or ''}{extra}".strip()
65-
66-
6716
def timedelta_to_float(dt: float | timedelta | np.timedelta64) -> float:
6817
"""Convert a timedelta to a float in seconds."""
6918
if isinstance(dt, timedelta):

tests-v3/tools/test_helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import pytest
55

66
import parcels.utils._helpers as helpers
7-
from parcels.utils._helpers import deprecated, deprecated_made_private, timedelta_to_float
7+
from parcels.utils._helpers import timedelta_to_float
8+
from parcels.utils._decorators import deprecated, deprecated_made_private
89

910

1011
def test_format_list_items_multiline():

0 commit comments

Comments
 (0)