pytransitions / transitions

A lightweight, object-oriented finite state machine implementation in Python with many extensions
MIT License
5.5k stars 525 forks source link

Can't reference IntEnum state with value 0 in transition destination #440

Closed Pathfinder216 closed 4 years ago

Pathfinder216 commented 4 years ago

I defined an IntEnum that I'm using as the states. It has a member with value 0. When defining the transitions, I can't use that member as a destination. I can, however, get it to work by using a string of the enumeration name.

Minimal example:

from enum import IntEnum
from transitions import Machine

class State(IntEnum):
    FOO = 0
    BAR = 1

transitions = [
    ['foo', State.FOO, State.BAR],
    ['bar', State.BAR, State.FOO],
    # ['bar', State.BAR, 'FOO'],
]

class StateMachine(Machine):
    def __init__(self):
        super().__init__(states=State, initial=State.FOO, transitions=transitions)

m = StateMachine()
m.foo()
assert m.is_BAR()
m.bar()
assert m.is_FOO()  # raises AssertionError

Making any one (or more) of the following changes causes it to work fine and not raise an AssertionError:

I setup logging to the console to see what was going on. I noticed the following message: DEBUG:transitions.core:Initiating transition from state BAR to state None...

aleneum commented 4 years ago

Hi @Pathfinder216,

I can confirm this bug with Machine in version 0.8.1. I added your case to the core tests and pushed a fix. Thank you for taking the time to file such an informative issue. Much appreciated!