python-statemachine icon indicating copy to clipboard operation
python-statemachine copied to clipboard

InvalidDestinationState exception on multiple transitions

Open ye opened this issue 3 years ago • 0 comments

  • Python State Machine version: python-statemachine==0.8.0
  • Python version: Python 3.9.12
  • Operating System: macOS Monterey 12.3.1

Description

I am trying to setup a state machine with multiple destination states.

However, I can't seem to get it to work with one of the syntaxes.

Working transition:

pay = unpaid.to(paid, failed)

Nonworking transition:

pay = unpaid.to(paid) | unpaid.to(failed)

Can someone explain why the latter syntax won't work? If this is a bug, can we fix it?

Also, I would like to see more examples in the documentation, I spent so much time in digging through the unit test code before I can get the transition setup to work.

Thanks!

What I Did

from statemachine import StateMachine, State

PAY_FAILED = True

class InvoiceStateMachine(StateMachine):
    unpaid = State('unpaid', initial=True)
    paid = State('paid')
    failed = State('failed')

    pay = unpaid.to(paid) | unpaid.to(failed)

    def on_pay(self):
        if PAY_FAILED:
            return self.failed
        else:
            return self.paid


invoice_fsm = InvoiceStateMachine()
invoice_fsm.pay()

What I Got

$ python test-invalid-destination-state.py
Traceback (most recent call last):
  File "/Users/ye/test-invalid-destination-state.py", line 21, in <module>
    invoice_fsm.pay()
  File "/Users/ye/.venvs/test/lib/python3.9/site-packages/statemachine/statemachine.py", line 61, in __call__
    return self.func(*args, **kwargs)
  File "/Users/ye/.venvs/test/lib/python3.9/site-packages/statemachine/statemachine.py", line 85, in transition_callback
    return self._run(machine, *args, **kwargs)
  File "/Users/ye/.venvs/test/lib/python3.9/site-packages/statemachine/statemachine.py", line 193, in _run
    return transition._run(machine, *args, **kwargs)
  File "/Users/ye/.venvs/test/lib/python3.9/site-packages/statemachine/statemachine.py", line 114, in _run
    return machine._activate(self, *args, **kwargs)
  File "/Users/ye/.venvs/test/lib/python3.9/site-packages/statemachine/statemachine.py", line 416, in _activate
    result, destination = transition._get_destination(result)
  File "/Users/ye/.venvs/test/lib/python3.9/site-packages/statemachine/statemachine.py", line 161, in _get_destination
    raise InvalidDestinationState(self, destination)
statemachine.exceptions.InvalidDestinationState: failed is not a possible destination state for pay transition.

ye avatar Apr 25 '22 20:04 ye