Skip to content

Commit 1033908

Browse files
gmalinvepyansys-ci-botSMoraisAnsyspre-commit-ci[bot]
authored andcommitted
FEAT: args deprecation decorator (#6086)
Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: Sébastien Morais <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 90de0ad commit 1033908

File tree

5 files changed

+83
-9
lines changed

5 files changed

+83
-9
lines changed

doc/changelog.d/6086.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
args deprecation decorator

src/ansys/aedt/core/application/analysis.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from ansys.aedt.core.generic.constants import VIEW
5252
from ansys.aedt.core.generic.file_utils import generate_unique_name
5353
from ansys.aedt.core.generic.file_utils import open_file
54+
from ansys.aedt.core.generic.general_methods import deprecate_argument
5455
from ansys.aedt.core.generic.general_methods import filter_tuple
5556
from ansys.aedt.core.generic.general_methods import is_linux
5657
from ansys.aedt.core.generic.general_methods import is_windows
@@ -782,8 +783,13 @@ def list_of_variations(self, setup=None, sweep=None):
782783
return [""]
783784

784785
@pyaedt_function_handler()
786+
@deprecate_argument(
787+
arg_name="analyze",
788+
message="The ``analyze`` argument will be removed in future versions. Analyze before exporting results.",
789+
)
785790
def export_results(
786791
self,
792+
analyze=False,
787793
export_folder=None,
788794
matrix_name="Original",
789795
matrix_type="S",
@@ -794,12 +800,13 @@ def export_results(
794800
include_gamma_comment=True,
795801
support_non_standard_touchstone_extension=False,
796802
variations=None,
797-
**kwargs,
798803
):
799804
"""Export all available reports to a file, including profile, and convergence and sNp when applicable.
800805
801806
Parameters
802807
----------
808+
analyze : bool
809+
Whether to analyze before export. Solutions must be present for the design.
803810
export_folder : str, optional
804811
Full path to the project folder. The default is ``None``, in which case the
805812
working directory is used.
@@ -848,14 +855,6 @@ def export_results(
848855
>>> aedtapp.analyze()
849856
>>> exported_files = aedtapp.export_results()
850857
"""
851-
analyze = False
852-
if "analyze" in kwargs:
853-
warnings.warn(
854-
"The ``analyze`` argument will be deprecated in future versions." "Analyze before exporting results.",
855-
DeprecationWarning,
856-
)
857-
analyze = kwargs["analyze"]
858-
859858
exported_files = []
860859
if not export_folder:
861860
export_folder = self.working_directory

src/ansys/aedt/core/circuit.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from ansys.aedt.core.generic.file_utils import generate_unique_name
4040
from ansys.aedt.core.generic.file_utils import open_file
4141
from ansys.aedt.core.generic.file_utils import read_configuration_file
42+
from ansys.aedt.core.generic.general_methods import deprecate_argument
4243
from ansys.aedt.core.generic.general_methods import is_linux
4344
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
4445
from ansys.aedt.core.generic.settings import settings
@@ -1648,6 +1649,10 @@ def import_edb_in_circuit(self, input_dir):
16481649
@pyaedt_function_handler(
16491650
touchstone="input_file", probe_pins="tx_schematic_pins", probe_ref_pins="tx_schematic_differential_pins"
16501651
)
1652+
@deprecate_argument(
1653+
arg_name="analyze",
1654+
message="The ``analyze`` argument will be removed in future versions. Analyze before exporting results.",
1655+
)
16511656
def create_tdr_schematic_from_snp(
16521657
self,
16531658
input_file,
@@ -1792,6 +1797,10 @@ def create_tdr_schematic_from_snp(
17921797
return True, tdr_probe_names
17931798

17941799
@pyaedt_function_handler(touchstone="input_file")
1800+
@deprecate_argument(
1801+
arg_name="analyze",
1802+
message="The ``analyze`` argument will be removed in future versions. Analyze before exporting results.",
1803+
)
17951804
def create_lna_schematic_from_snp(
17961805
self,
17971806
input_file,
@@ -1906,6 +1915,10 @@ def create_lna_schematic_from_snp(
19061915
tx_refs="tx_schematic_differential_pins",
19071916
rx_refs="rx_schematic_differentialial_pins",
19081917
)
1918+
@deprecate_argument(
1919+
arg_name="analyze",
1920+
message="The ``analyze`` argument will be removed in future versions. Analyze before exporting results.",
1921+
)
19091922
def create_ami_schematic_from_snp(
19101923
self,
19111924
input_file,
@@ -2008,6 +2021,10 @@ def create_ami_schematic_from_snp(
20082021
)
20092022

20102023
@pyaedt_function_handler()
2024+
@deprecate_argument(
2025+
arg_name="analyze",
2026+
message="The ``analyze`` argument will be removed in future versions. Analyze before exporting results.",
2027+
)
20112028
def create_ibis_schematic_from_snp(
20122029
self,
20132030
input_file,
@@ -2122,6 +2139,10 @@ def create_ibis_schematic_from_snp(
21222139
)
21232140

21242141
@pyaedt_function_handler()
2142+
@deprecate_argument(
2143+
arg_name="analyze",
2144+
message="The ``analyze`` argument will be removed in future versions. Analyze before exporting results.",
2145+
)
21252146
def create_ibis_schematic_from_pins(
21262147
self,
21272148
ibis_tx_file,

src/ansys/aedt/core/generic/general_methods.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import datetime
2727
import difflib
28+
import functools
2829
from functools import update_wrapper
2930
import inspect
3031
import itertools
@@ -34,6 +35,7 @@
3435
import sys
3536
import time
3637
import traceback
38+
import warnings
3739

3840
from ansys.aedt.core.aedt_logger import pyaedt_logger
3941
from ansys.aedt.core.generic.numbers import _units_assignment
@@ -235,6 +237,52 @@ def deprecate_kwargs(func_name, kwargs, aliases):
235237
kwargs[new] = kwargs.pop(alias)
236238

237239

240+
def deprecate_argument(arg_name: str, version: str = None, message: str = None, removed: bool = False):
241+
"""
242+
Decorator to deprecate a specific argument (positional or keyword) in a function.
243+
244+
Parameters:
245+
arg_name : str
246+
The name of the deprecated argument.
247+
version : str
248+
The version in which the argument was removed.
249+
message : str, optional
250+
Custom deprecation message.
251+
removed : bool
252+
If ``True``, using the argument raises a TypeError.
253+
If ``False``, a DeprecationWarning is issued.
254+
"""
255+
256+
def decorator(func):
257+
@functools.wraps(func)
258+
def wrapper(*args, **kwargs):
259+
sig = inspect.signature(func)
260+
try:
261+
bound_args = sig.bind_partial(*args, **kwargs)
262+
bound_args.apply_defaults()
263+
except TypeError:
264+
# In case of incomplete binding (e.g. missing required args), skip
265+
return func(*args, **kwargs)
266+
267+
if arg_name in bound_args.arguments:
268+
msg_version = ""
269+
if version:
270+
msg_version = f" in version {version}"
271+
if removed:
272+
raise TypeError(
273+
message or f"Argument '{arg_name}' was removed{msg_version} and is no longer supported."
274+
)
275+
else:
276+
warn_msg = message or f"Argument '{arg_name}' is deprecated and will be removed{msg_version}."
277+
warnings.warn(warn_msg, DeprecationWarning, stacklevel=2)
278+
279+
return func(*args, **kwargs)
280+
281+
return wrapper
282+
283+
return decorator
284+
285+
238286
def pyaedt_function_handler(direct_func=None, **deprecated_kwargs):
239287
"""Provide an exception handler, logging mechanism, and argument converter for client-server communications.
240288

src/ansys/aedt/core/q3d.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from ansys.aedt.core.generic.constants import MATRIXOPERATIONSQ2D
3333
from ansys.aedt.core.generic.constants import MATRIXOPERATIONSQ3D
3434
from ansys.aedt.core.generic.file_utils import generate_unique_name
35+
from ansys.aedt.core.generic.general_methods import deprecate_argument
3536
from ansys.aedt.core.generic.general_methods import pyaedt_function_handler
3637
from ansys.aedt.core.generic.numbers import decompose_variable_value
3738
from ansys.aedt.core.generic.settings import settings
@@ -2499,6 +2500,10 @@ def auto_assign_conductors(self):
24992500
return True
25002501

25012502
@pyaedt_function_handler()
2503+
@deprecate_argument(
2504+
arg_name="analyze",
2505+
message="The ``analyze`` argument will be removed in future versions. Analyze before exporting results.",
2506+
)
25022507
def export_w_elements(self, analyze=False, export_folder=None):
25032508
"""Export all W-elements to files.
25042509

0 commit comments

Comments
 (0)