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

Conditional transition evaluation order is confusing

Open emptierset opened this issue 3 months ago • 0 comments

From https://python-statemachine.readthedocs.io/en/latest/guards.html#conditions:

This feature allows for multiple transitions on the same Event, with each Transition checked in the order they are declared.

I was actually about to file a bug report, but rereading this passage made me try something unintuitive that appears to have answered my question. Basically, it seems a bit confusing (and worth documenting more specifically) that what matters for a particular Event is the order in which the various transitions are defined elsewhere, rather than their order in the Event's TransitionList. For example:


last_thing_to_check = s.FOO.to(s.QUX)

second_thing_to_check = s.FOO.to(s.BAZ)

@second_thing_to_check.cond
def second_thing_to_check_cond(self):
    # ...

first_thing_to_check = s.FOO.to(s.BAR)

@first_thing_to_check.cond
def first_thing_to_check_cond(self):
    # ...

go_somewhere = first_thing_to_check | second_thing_to_check | last_thing_to_check

I was struggling to figure out why go_somewhere was just doing last_thing_to_check without testing any of the conditional transitions. I'm not sure whether this is a bug, but I found the behavior very surprising. It might be worth documenting somewhere.

emptierset avatar Oct 26 '25 06:10 emptierset