fgmacedo / python-statemachine

Python Finite State Machines made easy.
MIT License
858 stars 84 forks source link

Action callback gets called twice when mixing definition methods #406

Closed waszil closed 11 months ago

waszil commented 11 months ago

Description

Action callbacks mixed with decorator syntax and naming convention get called twice: I used the decorator method, but accidentally named the callbacks as per the naming convention method. I would expect either silent handling of this, or raising an exception prohibiting this.

What I Did

from statemachine import State
from statemachine import StateMachine

class ExampleStateMachine(StateMachine):
    Created = State(initial=True)
    Inited = State()

    initialize = Created.to(Inited)

    @initialize.before
    def before_initialize(self):
        print("before init")

    @initialize.on
    def on_initialize(self):
        print("on init")

def test_sm():
    sm = ExampleStateMachine()
    sm.initialize()

Prints:

before init
before init
on init
on init
fgmacedo commented 11 months ago

Hi @waszil , thanks for reporting this.

I've noted this behavior and It's also fixed on the branch related to Improve setup performance.

I'll add a regression test to ensure the problem is fixed.