dependency injection to simplify event functions

Author: patrickwolfCreated Jul 3, 2025Updated Jul 3, 2025
Labelsenhancement

You have an idea for a feature that would make transitions more helpful or easier to use? Great! We are looking forward to your suggestion.

Is your feature request related to a problem? Please describe. the events passed to a function need to be parsed which adds complexity to the functions

Describe the solution you'd like using dependency injection would allow for the parsing to be done by transitions

Additional context I like how its approached here for python statemachine Since its all open source maybe there can even be collaboration around the dependency injection code which would make it an easy feature

You can pass arbitrary positional or keyword arguments to the event, and they will be propagated to all actions and callbacks using something similar to dependency injection. In other words, the library will only inject the parameters declared on the callback method.

Note how before_cycle was declared:

python
def before_cycle(self, event: str, source: State, target: State, message: str = ""):
    message = ". " + message if message else ""
    return f"Running {event} from {source.id} to {target.id}{message}"

The params event, source, target (and others) are available built-in to be used on any action. The param message is user-defined, in our example we made it default empty so we can call cycle with or without a message parameter.

If we pass a message parameter, it will be used on the before_cycle action:

python
sm.send("cycle", message="Please, now slowdown.")
'Running cycle from green to yellow. Please, now slowdown.'

Source: pytransitions/transitions