Skip to content

Commit c9a7215

Browse files
authored
fix: Add support for wrapping functools.partial.
1 parent 5f6bf4c commit c9a7215

4 files changed

Lines changed: 26 additions & 1 deletion

File tree

docs/releases/2.0.1.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# StateMachine 2.0.1
2+
3+
*Not released yet*
4+
5+
6+
StateMachine 2.0.1 is a bugfix release.
7+
8+
9+
## Bugfixes
10+
11+
- Fixes [#369](https://github.com/fgmacedo/python-statemachine/issues/336) adding support to wrap
12+
methods used as {ref}`Actions` decorated with `functools.partial`.

docs/releases/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Below are release notes through StateMachine and its patch releases.
1515
```{toctree}
1616
:maxdepth: 1
1717
18+
2.0.1
1819
2.0.0
1920
2021
```

statemachine/signature.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import itertools
2+
from functools import partial
23
from inspect import BoundArguments
34
from inspect import Parameter
45
from inspect import Signature
@@ -15,7 +16,9 @@ def wrap(cls, method):
1516

1617
sig = cls.from_callable(method)
1718
sig.method = method
18-
sig.__name__ = method.__name__
19+
sig.__name__ = (
20+
method.func.__name__ if isinstance(method, partial) else method.__name__
21+
)
1922
return sig
2023

2124
def __call__(self, *args: Any, **kwargs: Any) -> Any:

tests/test_signature.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
from functools import partial
23

34
import pytest
45

@@ -148,9 +149,17 @@ class TestSignatureAdapter:
148149
def test_wrap_fn_single_positional_parameter(self, func, args, kwargs, expected):
149150

150151
wrapped_func = SignatureAdapter.wrap(func)
152+
assert wrapped_func.__name__ == func.__name__
151153

152154
if inspect.isclass(expected) and issubclass(expected, Exception):
153155
with pytest.raises(expected):
154156
wrapped_func(*args, **kwargs)
155157
else:
156158
assert wrapped_func(*args, **kwargs) == expected
159+
160+
def test_support_for_partial(self):
161+
part = partial(positional_and_kw_arguments, event="activated")
162+
wrapped_func = SignatureAdapter.wrap(part)
163+
164+
assert wrapped_func("A", "B") == ("A", "B", "activated")
165+
assert wrapped_func.__name__ == positional_and_kw_arguments.__name__

0 commit comments

Comments
 (0)