Skip to content

Commit 71e2b3a

Browse files
MNT: deprecated decorator (#830)
* ENH: refactor motor prints classes to inherit from _MotorPrints * STY: make format * MNT: update code and remove deprecated functions * ENH: add deprecation decorator and update deprecated methods * make format * fix warnings
1 parent e428604 commit 71e2b3a

File tree

3 files changed

+78
-20
lines changed

3 files changed

+78
-20
lines changed

rocketpy/mathutils/function.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
RBFInterpolator,
2525
)
2626

27-
from rocketpy.tools import from_hex_decode, to_hex_encode
27+
from rocketpy.tools import deprecated, from_hex_decode, to_hex_encode
2828

2929
from ..plots.plot_helpers import show_or_save_plot
3030

@@ -1459,14 +1459,13 @@ def plot(self, *args, **kwargs):
14591459
else:
14601460
print("Error: Only functions with 1D or 2D domains can be plotted.")
14611461

1462+
@deprecated(
1463+
reason="The `Function.plot1D` method is set to be deprecated and fully "
1464+
"removed in rocketpy v2.0.0",
1465+
alternative="Function.plot_1d",
1466+
)
14621467
def plot1D(self, *args, **kwargs): # pragma: no cover
14631468
"""Deprecated method, use Function.plot_1d instead."""
1464-
warnings.warn(
1465-
"The `Function.plot1D` method is set to be deprecated and fully "
1466-
+ "removed in rocketpy v2.0.0, use `Function.plot_1d` instead. "
1467-
+ "This method is calling `Function.plot_1d`.",
1468-
DeprecationWarning,
1469-
)
14701469
return self.plot_1d(*args, **kwargs)
14711470

14721471
def plot_1d( # pylint: disable=too-many-statements
@@ -1559,14 +1558,13 @@ def plot_1d( # pylint: disable=too-many-statements
15591558
if return_object:
15601559
return fig, ax
15611560

1561+
@deprecated(
1562+
reason="The `Function.plot2D` method is set to be deprecated and fully "
1563+
"removed in rocketpy v2.0.0",
1564+
alternative="Function.plot_2d",
1565+
)
15621566
def plot2D(self, *args, **kwargs): # pragma: no cover
15631567
"""Deprecated method, use Function.plot_2d instead."""
1564-
warnings.warn(
1565-
"The `Function.plot2D` method is set to be deprecated and fully "
1566-
+ "removed in rocketpy v2.0.0, use `Function.plot_2d` instead. "
1567-
+ "This method is calling `Function.plot_2d`.",
1568-
DeprecationWarning,
1569-
)
15701568
return self.plot_2d(*args, **kwargs)
15711569

15721570
def plot_2d( # pylint: disable=too-many-statements

rocketpy/rocket/rocket.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import math
2-
import warnings
32

43
import numpy as np
54

@@ -22,7 +21,7 @@
2221
from rocketpy.rocket.aero_surface.generic_surface import GenericSurface
2322
from rocketpy.rocket.components import Components
2423
from rocketpy.rocket.parachute import Parachute
25-
from rocketpy.tools import parallel_axis_theorem_from_com
24+
from rocketpy.tools import deprecated, parallel_axis_theorem_from_com
2625

2726

2827
# pylint: disable=too-many-instance-attributes, too-many-public-methods, too-many-instance-attributes
@@ -1173,16 +1172,16 @@ def add_nose(
11731172
self.add_surfaces(nose, position)
11741173
return nose
11751174

1175+
@deprecated(
1176+
reason="This method is set to be deprecated in version 1.0.0 and fully "
1177+
"removed by version 2.0.0",
1178+
alternative="Rocket.add_trapezoidal_fins",
1179+
)
11761180
def add_fins(self, *args, **kwargs): # pragma: no cover
11771181
"""See Rocket.add_trapezoidal_fins for documentation.
11781182
This method is set to be deprecated in version 1.0.0 and fully removed
11791183
by version 2.0.0. Use Rocket.add_trapezoidal_fins instead. It keeps the
11801184
same arguments and signature."""
1181-
warnings.warn(
1182-
"This method is set to be deprecated in version 1.0.0 and fully "
1183-
"removed by version 2.0.0. Use Rocket.add_trapezoidal_fins instead",
1184-
DeprecationWarning,
1185-
)
11861185
return self.add_trapezoidal_fins(*args, **kwargs)
11871186

11881187
def add_trapezoidal_fins(

rocketpy/tools.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import math
1515
import re
1616
import time
17+
import warnings
1718
from bisect import bisect_left
1819

1920
import dill
@@ -28,6 +29,66 @@
2829
INSTALL_MAPPING = {"IPython": "ipython"}
2930

3031

32+
def deprecated(reason=None, version=None, alternative=None):
33+
"""
34+
Decorator to mark functions or methods as deprecated.
35+
36+
This decorator issues a DeprecationWarning when the decorated function
37+
is called, indicating that it will be removed in future versions.
38+
39+
Parameters
40+
----------
41+
reason : str, optional
42+
Custom deprecation message. If not provided, a default message will be used.
43+
version : str, optional
44+
Version when the function will be removed. If provided, it will be
45+
included in the warning message.
46+
alternative : str, optional
47+
Name of the alternative function/method that should be used instead.
48+
If provided, it will be included in the warning message.
49+
50+
Returns
51+
-------
52+
callable
53+
The decorated function with deprecation warning functionality.
54+
55+
Examples
56+
--------
57+
>>> @deprecated(reason="This function is obsolete", version="v2.0.0",
58+
... alternative="new_function")
59+
... def old_function():
60+
... return "old result"
61+
62+
>>> @deprecated()
63+
... def another_old_function():
64+
... return "result"
65+
"""
66+
67+
def decorator(func):
68+
@functools.wraps(func)
69+
def wrapper(*args, **kwargs):
70+
# Build the deprecation message
71+
if reason:
72+
message = reason
73+
else:
74+
message = f"The function `{func.__name__}` is deprecated"
75+
76+
if version:
77+
message += f" and will be removed in {version}"
78+
79+
if alternative:
80+
message += f". Use `{alternative}` instead"
81+
82+
message += "."
83+
84+
warnings.warn(message, DeprecationWarning, stacklevel=2)
85+
return func(*args, **kwargs)
86+
87+
return wrapper
88+
89+
return decorator
90+
91+
3192
def tuple_handler(value):
3293
"""Transforms the input value into a tuple that represents a range. If the
3394
input is an int or float, the output is a tuple from zero to the input

0 commit comments

Comments
 (0)