Skip to content

Commit d0b8a06

Browse files
authored
Implement deprecation warning decorator
Added a deprecation decorator to warn users about obsolete functions and their alternatives.
1 parent 00d3770 commit d0b8a06

1 file changed

Lines changed: 68 additions & 11 deletions

File tree

rocketpy/tools.py

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@
1515
import re
1616
import time
1717
from bisect import bisect_left
18-
18+
import warnings
1919
import dill
2020
import matplotlib.pyplot as plt
2121
import numpy as np
2222
import pytz
2323
from cftime import num2pydate
2424
from matplotlib.patches import Ellipse
2525
from packaging import version as packaging_version
26-
from rocketpy.mathutils.function import Function
27-
from rocketpy.mathutils.vector_matrix import Vector
28-
29-
# Mapping of module name and the name of the package that should be installed
3026
INSTALL_MAPPING = {"IPython": "ipython"}
3127

3228

@@ -55,6 +51,66 @@ def tuple_handler(value):
5551
return tuple(value)
5652
else:
5753
raise ValueError("value must be a list or tuple of length 1 or 2.")
54+
55+
def deprecated(reason=None, version=None, alternative=None):
56+
"""
57+
Decorator to mark functions or methods as deprecated.
58+
59+
This decorator issues a DeprecationWarning when the decorated function
60+
is called, indicating that it will be removed in future versions.
61+
62+
Parameters
63+
----------
64+
reason : str, optional
65+
Custom deprecation message. If not provided, a default message will be used.
66+
version : str, optional
67+
Version when the function will be removed. If provided, it will be
68+
included in the warning message.
69+
alternative : str, optional
70+
Name of the alternative function/method that should be used instead.
71+
If provided, it will be included in the warning message.
72+
73+
Returns
74+
-------
75+
callable
76+
The decorated function with deprecation warning functionality.
77+
78+
Examples
79+
--------
80+
>>> @deprecated(reason="This function is obsolete", version="v2.0.0",
81+
... alternative="new_function")
82+
... def old_function():
83+
... return "old result"
84+
85+
>>> @deprecated()
86+
... def another_old_function():
87+
... return "result"
88+
"""
89+
90+
def decorator(func):
91+
@functools.wraps(func)
92+
def wrapper(*args, **kwargs):
93+
# Build the deprecation message
94+
if reason:
95+
message = reason
96+
else:
97+
message = f"The function `{func.__name__}` is deprecated"
98+
99+
if version:
100+
message += f" and will be removed in {version}"
101+
102+
if alternative:
103+
message += f". Use `{alternative}` instead"
104+
105+
message += "."
106+
107+
warnings.warn(message, DeprecationWarning, stacklevel=2)
108+
return func(*args, **kwargs)
109+
110+
return wrapper
111+
112+
return decorator
113+
58114

59115

60116
def calculate_cubic_hermite_coefficients(x0, x1, y0, yp0, y1, yp1):
@@ -1023,10 +1079,10 @@ def wrapper(*args, **kwargs):
10231079

10241080

10251081

1026-
def _pat_dynamic_helper(com_inertia_moment, mass, distance_vec_3d, axes_term_lambda):
1027-
1028-
1029-
1082+
def _pat_dynamic_helper(com_inertia_moment, mass, distance_vec_3d, axes_term_lambda):
1083+
"Local import to break circular dependency with mathutils.function"
1084+
from rocketpy.mathutils.function import Function
1085+
from rocketpy.mathutils.vector_matrix import Vector
10301086
is_dynamic = (
10311087
isinstance(com_inertia_moment, Function)
10321088
or isinstance(mass, Function)
@@ -1058,8 +1114,9 @@ def new_source(t):
10581114
def _pat_dynamic_product_helper(
10591115
com_inertia_product, mass, distance_vec_3d, product_term_lambda
10601116
):
1061-
1062-
1117+
"Local import to break circular dependency with mathutils.function"
1118+
from rocketpy.mathutils.function import Function
1119+
from rocketpy.mathutils.vector_matrix import Vector
10631120
is_dynamic = (
10641121
isinstance(com_inertia_product, Function)
10651122
or isinstance(mass, Function)

0 commit comments

Comments
 (0)