diff --git a/doc/changelog.d/2346.added.md b/doc/changelog.d/2346.added.md new file mode 100644 index 0000000000..7d44fc9a7a --- /dev/null +++ b/doc/changelog.d/2346.added.md @@ -0,0 +1 @@ +Preserve original signature of decorated methods diff --git a/src/ansys/geometry/core/misc/checks.py b/src/ansys/geometry/core/misc/checks.py index 7b26d93aa4..a5a384102b 100644 --- a/src/ansys/geometry/core/misc/checks.py +++ b/src/ansys/geometry/core/misc/checks.py @@ -22,6 +22,7 @@ """Provides functions for performing common checks.""" from collections.abc import Iterable +import functools from typing import TYPE_CHECKING import warnings @@ -41,6 +42,7 @@ def ensure_design_is_active(method): is not necessary to call this. """ + @functools.wraps(method) def wrapper(self, *args, **kwargs): import ansys.geometry.core as pyansys_geometry from ansys.geometry.core.errors import GeometryRuntimeError @@ -310,6 +312,7 @@ def min_backend_version(major: int, minor: int, service_pack: int): from ansys.geometry.core.logger import LOG def backend_version_decorator(method): + @functools.wraps(method) def wrapper(self, *args, **kwargs): method_version = semver.Version(major, minor, service_pack) if hasattr(self, "_grpc_client"): @@ -365,6 +368,7 @@ def deprecated_method( """ def deprecated_decorator(method): + @functools.wraps(method) def wrapper(*args, **kwargs): msg = f"The method '{method.__name__}' is deprecated." if alternative: @@ -408,6 +412,7 @@ def deprecated_argument( """ def deprecated_decorator(method): + @functools.wraps(method) def wrapper(*args, **kwargs): if arg in kwargs and kwargs[arg] is not None: msg = f"The argument '{arg}' in '{method.__name__}' is deprecated." @@ -473,6 +478,7 @@ def graphics_required(method): Decorated method. """ + @functools.wraps(method) def wrapper(*args, **kwargs): run_if_graphics_required() return method(*args, **kwargs) @@ -524,6 +530,7 @@ def kwargs_passed_not_accepted(method): """ import inspect + @functools.wraps(method) def wrapper(*args, **kwargs): # Get the method signature sig = inspect.signature(method) diff --git a/tests/test_misc_checks.py b/tests/test_misc_checks.py index c2e6d5f436..955ad691cf 100644 --- a/tests/test_misc_checks.py +++ b/tests/test_misc_checks.py @@ -468,9 +468,27 @@ def my_method(arg1, arg2, **kwargs): ): my_method(arg1=1, arg2=2, arg3=3) - # TODO: This test fails, fix decorator #2338 - # @kwargs_passed_not_accepted - # @deprecated_argument("arg3") - # def my_method(arg1, arg2, **kwargs): - # """A method that accepts no keyword arguments.""" - # return arg1 + arg2 + @kwargs_passed_not_accepted + @deprecated_argument("arg3") + def my_method_diff_order(arg1, arg2, **kwargs): + """A method that accepts no keyword arguments.""" + return arg1 + arg2 + + # Call the method without kwargs - should not raise an error + assert my_method_diff_order(1, 2) == 3 + assert my_method_diff_order(arg1=1, arg2=2) == 3 + + # Call the method with kwargs - should raise an error + with pytest.raises( + TypeError, + match="The following keyword arguments are not accepted in the " + "method 'my_method_diff_order': unexpected_arg, another_one.", + ): + my_method_diff_order(1, 2, unexpected_arg=3, another_one="test") + + with pytest.raises( + TypeError, + match="The following keyword arguments are not accepted in the " + "method 'my_method_diff_order': arg3.", + ): + my_method_diff_order(arg1=1, arg2=2, arg3=3)