Skip to content

Commit 1bcf7b8

Browse files
feat: preserve original signature of decorated methods (#2346)
Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent d213085 commit 1bcf7b8

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

doc/changelog.d/2346.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Preserve original signature of decorated methods

src/ansys/geometry/core/misc/checks.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"""Provides functions for performing common checks."""
2323

2424
from collections.abc import Iterable
25+
import functools
2526
from typing import TYPE_CHECKING
2627
import warnings
2728

@@ -41,6 +42,7 @@ def ensure_design_is_active(method):
4142
is not necessary to call this.
4243
"""
4344

45+
@functools.wraps(method)
4446
def wrapper(self, *args, **kwargs):
4547
import ansys.geometry.core as pyansys_geometry
4648
from ansys.geometry.core.errors import GeometryRuntimeError
@@ -310,6 +312,7 @@ def min_backend_version(major: int, minor: int, service_pack: int):
310312
from ansys.geometry.core.logger import LOG
311313

312314
def backend_version_decorator(method):
315+
@functools.wraps(method)
313316
def wrapper(self, *args, **kwargs):
314317
method_version = semver.Version(major, minor, service_pack)
315318
if hasattr(self, "_grpc_client"):
@@ -365,6 +368,7 @@ def deprecated_method(
365368
"""
366369

367370
def deprecated_decorator(method):
371+
@functools.wraps(method)
368372
def wrapper(*args, **kwargs):
369373
msg = f"The method '{method.__name__}' is deprecated."
370374
if alternative:
@@ -408,6 +412,7 @@ def deprecated_argument(
408412
"""
409413

410414
def deprecated_decorator(method):
415+
@functools.wraps(method)
411416
def wrapper(*args, **kwargs):
412417
if arg in kwargs and kwargs[arg] is not None:
413418
msg = f"The argument '{arg}' in '{method.__name__}' is deprecated."
@@ -473,6 +478,7 @@ def graphics_required(method):
473478
Decorated method.
474479
"""
475480

481+
@functools.wraps(method)
476482
def wrapper(*args, **kwargs):
477483
run_if_graphics_required()
478484
return method(*args, **kwargs)
@@ -524,6 +530,7 @@ def kwargs_passed_not_accepted(method):
524530
"""
525531
import inspect
526532

533+
@functools.wraps(method)
527534
def wrapper(*args, **kwargs):
528535
# Get the method signature
529536
sig = inspect.signature(method)

tests/test_misc_checks.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,27 @@ def my_method(arg1, arg2, **kwargs):
468468
):
469469
my_method(arg1=1, arg2=2, arg3=3)
470470

471-
# TODO: This test fails, fix decorator #2338
472-
# @kwargs_passed_not_accepted
473-
# @deprecated_argument("arg3")
474-
# def my_method(arg1, arg2, **kwargs):
475-
# """A method that accepts no keyword arguments."""
476-
# return arg1 + arg2
471+
@kwargs_passed_not_accepted
472+
@deprecated_argument("arg3")
473+
def my_method_diff_order(arg1, arg2, **kwargs):
474+
"""A method that accepts no keyword arguments."""
475+
return arg1 + arg2
476+
477+
# Call the method without kwargs - should not raise an error
478+
assert my_method_diff_order(1, 2) == 3
479+
assert my_method_diff_order(arg1=1, arg2=2) == 3
480+
481+
# Call the method with kwargs - should raise an error
482+
with pytest.raises(
483+
TypeError,
484+
match="The following keyword arguments are not accepted in the "
485+
"method 'my_method_diff_order': unexpected_arg, another_one.",
486+
):
487+
my_method_diff_order(1, 2, unexpected_arg=3, another_one="test")
488+
489+
with pytest.raises(
490+
TypeError,
491+
match="The following keyword arguments are not accepted in the "
492+
"method 'my_method_diff_order': arg3.",
493+
):
494+
my_method_diff_order(arg1=1, arg2=2, arg3=3)

0 commit comments

Comments
 (0)