44
55## What's new in 3.1.0
66
7- ### Sphinx directive for inline diagrams
8-
9- A new Sphinx extension renders state machine diagrams directly in your
10- documentation from an importable class path — no manual image generation
11- needed.
7+ ### Text representations with ` format() `
128
13- Add ` "statemachine.contrib.diagram.sphinx_ext" ` to your ` conf.py `
14- extensions, then use the directive in any MyST Markdown page :
9+ State machines now support Python's built-in ` format() ` protocol. Use f-strings
10+ or ` format() ` to get text representations — on both classes and instances :
1511
16- ```` markdown
17- ``` {statemachine-diagram} myproject.machines.OrderControl
18- :events: receive_payment
19- :caption: After payment
20- :target:
12+ ``` python
13+ f " { TrafficLightMachine:md } "
14+ f " { sm:mermaid} "
15+ format (sm, " rst" )
2116```
22- ````
2317
24- The directive supports the same options as the standard ` image ` /` figure `
25- directives (` :width: ` , ` :height: ` , ` :scale: ` , ` :align: ` , ` :target: ` ,
26- ` :class: ` , ` :name: ` ), plus ` :events: ` to instantiate the machine and send
27- events before rendering (highlighting the current state).
18+ Supported formats:
2819
29- Using ` :target: ` without a value makes the diagram clickable, opening the
30- full SVG in a new browser tab for zooming — useful for large statecharts.
20+ | Format | Output | Requires |
21+ | -----------| ---------------------------| -----------------------|
22+ | ` dot ` | Graphviz DOT source | ` pydot ` |
23+ | ` svg ` | SVG markup (via Graphviz) | ` pydot ` + ` graphviz ` |
24+ | ` mermaid ` | Mermaid stateDiagram-v2 | — |
25+ | ` md ` | Markdown transition table | — |
26+ | ` rst ` | RST transition table | — |
3127
32- See {ref}` diagram:Sphinx directive ` for full documentation.
33- [ #589 ] ( https://github.com/fgmacedo/python-statemachine/pull/589 ) .
28+ See {ref}` diagram:Text representations ` for details.
3429
3530
36- ### Performance: 5x–7x faster event processing
37-
38- The engine's hot paths have been systematically profiled and optimized, resulting in
39- ** 4.7x–7.7x faster event throughput** and ** 1.9x–2.6x faster setup** across all
40- machine types. All optimizations are internal — no public API changes.
41- See [ #592 ] ( https://github.com/fgmacedo/python-statemachine/pull/592 ) for details.
31+ ### Formatter facade
4232
33+ A new ` Formatter ` facade with decorator-based registration unifies all text
34+ format rendering behind a single API. Adding a new format requires only
35+ registering a render function — no changes to ` __format__ ` , the CLI, or the
36+ Sphinx directive:
4337
44- ### Thread safety documentation
45-
46- The sync engine is thread-safe: multiple threads can send events to the same state
47- machine instance concurrently. This is now documented in the
48- {ref}` processing model <thread-safety> ` and verified by stress tests.
49- [ #592 ] ( https://github.com/fgmacedo/python-statemachine/pull/592 ) .
38+ ``` python
39+ from statemachine.contrib.diagram import formatter
5040
41+ formatter.render(sm, " mermaid" )
42+ formatter.supported_formats()
5143
52- ### Diagram CLI ` --events ` option
44+ @formatter.register_format (" custom" )
45+ def _render_custom (machine_or_class ):
46+ ...
47+ ```
5348
54- The ` python -m statemachine.contrib.diagram ` command now accepts ` --events ` to
55- instantiate the machine and send events before rendering, highlighting the
56- current active state — matching the Sphinx directive's ` :events: ` option.
57- See {ref}` diagram:Command line ` for details.
49+ See {ref}` formatter-api ` for details.
5850
5951
6052### Mermaid diagram support
6153
6254State machines can now be rendered as
6355[ Mermaid ` stateDiagram-v2 ` ] ( https://mermaid.js.org/syntax/stateDiagram.html )
64- source text — no Graphviz installation required.
56+ source text — no Graphviz installation required. Supports compound states,
57+ parallel regions, history states, guards, and active-state highlighting.
6558
6659Three ways to use it:
6760
68- - ** ` format() ` / f-strings:** ` f"{sm:mermaid}" ` , ` f"{sm:md}" ` , ` f"{sm:rst}" ` —
69- works on both instances and classes.
61+ - ** f-strings:** ` f"{sm:mermaid}" `
7062- ** CLI:** ` python -m statemachine.contrib.diagram MyMachine - --format mermaid `
7163- ** Sphinx directive:** ` :format: mermaid ` renders via ` sphinxcontrib-mermaid ` .
7264
73- A new ` TransitionTableRenderer ` produces markdown or RST transition tables
74- from the same diagram IR. See {ref}` diagram:Text representations with format() `
75- and {ref}` diagram:Mermaid output ` for details.
65+ See {ref}` diagram:Mermaid format ` for details.
7666
7767
7868### Auto-expanding docstrings
@@ -94,13 +84,76 @@ class TrafficLight(StateChart):
9484```
9585
9686Any registered format works: ` md ` , ` rst ` , ` mermaid ` , ` dot ` , etc.
87+ Works with Sphinx autodoc — the expanded docstring is what gets rendered.
9788See {ref}` diagram:Auto-expanding docstrings ` for details.
9889
9990
91+ ### Sphinx directive for inline diagrams
92+
93+ A new Sphinx extension renders state machine diagrams directly in your
94+ documentation from an importable class path — no manual image generation
95+ needed.
96+
97+ Add ` "statemachine.contrib.diagram.sphinx_ext" ` to your ` conf.py `
98+ extensions, then use the directive in any MyST Markdown page:
99+
100+ ```` markdown
101+ ``` {statemachine-diagram} myproject.machines.OrderControl
102+ :events: receive_payment
103+ :caption: After payment
104+ :target:
105+ ```
106+ ````
107+
108+ The directive supports the same options as the standard ` image ` /` figure `
109+ directives (` :width: ` , ` :height: ` , ` :scale: ` , ` :align: ` , ` :target: ` ,
110+ ` :class: ` , ` :name: ` ), plus ` :events: ` to instantiate the machine and send
111+ events before rendering (highlighting the current state).
112+
113+ Using ` :target: ` without a value makes the diagram clickable, opening the
114+ full SVG in a new browser tab for zooming — useful for large statecharts.
115+
116+ The ` :format: mermaid ` option renders via ` sphinxcontrib-mermaid ` instead of
117+ Graphviz.
118+
119+ See {ref}` diagram:Sphinx directive ` for full documentation.
120+ [ #589 ] ( https://github.com/fgmacedo/python-statemachine/pull/589 ) .
121+
122+
123+ ### Diagram CLI ` --events ` and ` --format ` options
124+
125+ The ` python -m statemachine.contrib.diagram ` command now accepts:
126+
127+ - ` --events ` to instantiate the machine and send events before rendering,
128+ highlighting the current active state.
129+ - ` --format ` to choose the output format (` mermaid ` , ` md ` , ` rst ` , ` dot ` , ` svg ` ,
130+ or image formats via Graphviz). Use ` - ` as the output path to write text
131+ formats to stdout.
132+
133+ See {ref}` diagram:Command line ` for details.
134+ [ #593 ] ( https://github.com/fgmacedo/python-statemachine/pull/593 ) .
135+
136+
137+ ### Performance: 5x–7x faster event processing
138+
139+ The engine's hot paths have been systematically profiled and optimized, resulting in
140+ ** 4.7x–7.7x faster event throughput** and ** 1.9x–2.6x faster setup** across all
141+ machine types. All optimizations are internal — no public API changes.
142+ See [ #592 ] ( https://github.com/fgmacedo/python-statemachine/pull/592 ) for details.
143+
144+
145+ ### Thread safety documentation
146+
147+ The sync engine is thread-safe: multiple threads can send events to the same state
148+ machine instance concurrently. This is now documented in the
149+ {ref}` processing model <thread-safety> ` and verified by stress tests.
150+ [ #592 ] ( https://github.com/fgmacedo/python-statemachine/pull/592 ) .
151+
152+
100153### Bugfixes in 3.1.0
101154
102155- Fixes silent misuse of ` Event() ` with multiple positional arguments. Passing more than one
103- transition to ` Event() ` (e.g., ` Event(t1, t2) ` ) now raises {ref} ` InvalidDefinition ` with a
156+ transition to ` Event() ` (e.g., ` Event(t1, t2) ` ) now raises ` InvalidDefinition ` with a
104157 clear message suggesting the ` | ` operator. Previously, the second argument was silently
105158 interpreted as the event ` id ` , leaving the extra transitions eventless (auto-firing).
106159 [ #588 ] ( https://github.com/fgmacedo/python-statemachine/pull/588 ) .
0 commit comments