Closed wawa19933 closed 1 year ago
Here is a dirty workaround:
--- a/signature.py 2023-06-12 04:14:29.750363476 +0200
+++ b/signature.py 2023-06-12 04:14:43.009982670 +0200
@@ -15,6 +15,14 @@
"""Build a wrapper that adapts the received arguments to the inner ``method`` signature"""
sig = cls.from_callable(method)
+ if not isinstance(sig, SignatureAdapter):
+ n = SignatureAdapter()
+ n._parameters = sig._parameters
+ n.__slots__ = sig.__slots__
+ n._parameter_cls = sig._parameter_cls
+ n._bound_arguments_cls = sig._bound_arguments_cls
+ n.empty = sig.empty
+ sig = n
sig.method = method
sig.__name__ = (
method.func.__name__ if isinstance(method, partial) else method.__name__
This doesn't affect underlying unittest.mock patch
functions, though.
So, this example works:
class Observer:
def on_enter_state(self, event, model, source, target, state):
...
obs = Observer()
# on_enter_state = mocker.spy(obs, "on_enter_state")
on_enter_state = mocker.patch.object(obs, "on_enter_state")
class Machine(StateMachine):
a = State("Init", initial=True)
b = State("Fin")
cycle = a.to(b) | b.to(a)
def on_exit_state(self, source, target):
assert source != target
state = Machine().add_observer(obs)
assert state.a.is_active
state.cycle()
assert state.b.is_active
on_enter_state.assert_called_once()
Hi @wawa19933 , thanks for reporting this and the extra care on the context and details. I've added pytest-mock
as a dependency to the project to be able to reproduce.
Description
Throws exception during construction, when using pytest-mock.
from traceback:
Issue is caused by mock, that replaces class method with a wrapper, which already contains
Signature
instance. This instance is returned fromcls.from_callable
instead ofSignatureAdapter
.What I Did
here is a minimal code example