|
| 1 | +# StateMachine 2.4.0 |
| 2 | + |
| 3 | +*November 5, 2024* |
| 4 | + |
| 5 | +## What's new in 2.4.0 |
| 6 | + |
| 7 | +This release introduces powerful new features for the `StateMachine` library: {ref}`Condition expressions` and explicit definition of {ref}`Events`. These updates make it easier to define complex transition conditions and enhance performance, especially in workflows with nested or recursive event structures. |
| 8 | + |
| 9 | +### Python compatibility in 2.4.0 |
| 10 | + |
| 11 | +StateMachine 2.4.0 supports Python 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, and 3.13. |
| 12 | + |
| 13 | +### Conditions expressions in 2.4.0 |
| 14 | + |
| 15 | +This release introduces support for conditionals with Boolean algebra. You can now use expressions like `or`, `and`, and `not` directly within transition conditions, simplifying the definition of complex state transitions. This allows for more flexible and readable condition setups in your state machine configurations. |
| 16 | + |
| 17 | +Example (with a spoiler of the next highlight): |
| 18 | + |
| 19 | +```py |
| 20 | +>>> from statemachine import StateMachine, State, Event |
| 21 | + |
| 22 | +>>> class AnyConditionSM(StateMachine): |
| 23 | +... start = State(initial=True) |
| 24 | +... end = State(final=True) |
| 25 | +... |
| 26 | +... submit = Event( |
| 27 | +... start.to(end, cond="used_money or used_credit"), |
| 28 | +... name="finish order", |
| 29 | +... ) |
| 30 | +... |
| 31 | +... used_money: bool = False |
| 32 | +... used_credit: bool = False |
| 33 | + |
| 34 | +>>> sm = AnyConditionSM() |
| 35 | +>>> sm.submit() |
| 36 | +Traceback (most recent call last): |
| 37 | +TransitionNotAllowed: Can't finish order when in Start. |
| 38 | + |
| 39 | +>>> sm.used_credit = True |
| 40 | +>>> sm.submit() |
| 41 | +>>> sm.current_state.id |
| 42 | +'end' |
| 43 | + |
| 44 | +``` |
| 45 | + |
| 46 | +```{seealso} |
| 47 | +See {ref}`Condition expressions` for more details or take a look at the {ref}`sphx_glr_auto_examples_lor_machine.py` example. |
| 48 | +``` |
| 49 | + |
| 50 | +### Explicit event creation in 2.4.0 |
| 51 | + |
| 52 | +Now you can explicit declare {ref}`Events` using the {ref}`event` class. This allows custom naming, translations, and also helps your IDE to know that events are callable. |
| 53 | + |
| 54 | +```py |
| 55 | +>>> from statemachine import StateMachine, State, Event |
| 56 | + |
| 57 | +>>> class StartMachine(StateMachine): |
| 58 | +... created = State(initial=True) |
| 59 | +... started = State(final=True) |
| 60 | +... |
| 61 | +... start = Event(created.to(started), name="Launch the machine") |
| 62 | +... |
| 63 | +>>> [e.id for e in StartMachine.events] |
| 64 | +['start'] |
| 65 | +>>> [e.name for e in StartMachine.events] |
| 66 | +['Launch the machine'] |
| 67 | +>>> StartMachine.start.name |
| 68 | +'Launch the machine' |
| 69 | + |
| 70 | +``` |
| 71 | + |
| 72 | +```{seealso} |
| 73 | +See {ref}`Events` for more details. |
| 74 | +``` |
| 75 | + |
| 76 | +### Recursive state machines (infinite loop) |
| 77 | + |
| 78 | +We removed a note from the docs saying to avoid recursion loops. Since the {ref}`StateMachine 2.0.0` release we've turned the RTC model enabled by default, allowing nested events to occour as all events are put on an internal queue before being executed. |
| 79 | + |
| 80 | +```{seealso} |
| 81 | +See {ref}`sphx_glr_auto_examples_recursive_event_machine.py` for an example of an infinite loop state machine declaration using `after` action callback to call the same event over and over again. |
| 82 | +
|
| 83 | +``` |
| 84 | + |
| 85 | + |
| 86 | +## Bugfixes in 2.4.0 |
| 87 | + |
| 88 | +- Fixes [#484](https://github.com/fgmacedo/python-statemachine/issues/484) issue where nested events inside loops could leak memory by incorrectly |
| 89 | + referencing previous `event_data` when queuing the next event. This fix improves performance and stability in event-heavy workflows. |
0 commit comments