|
| 1 | +# python-statemachine |
| 2 | + |
| 3 | +Python Finite State Machines made easy. |
| 4 | + |
| 5 | +## Project overview |
| 6 | + |
| 7 | +A library for building finite state machines in Python, with support for sync and async engines, |
| 8 | +Django integration, diagram generation, and a flexible callback/listener system. |
| 9 | + |
| 10 | +- **Source code:** `statemachine/` |
| 11 | +- **Tests:** `tests/` |
| 12 | +- **Documentation:** `docs/` (Sphinx + MyST Markdown, hosted on ReadTheDocs) |
| 13 | + |
| 14 | +## Architecture |
| 15 | + |
| 16 | +- `statemachine.py` — Core `StateMachine` class |
| 17 | +- `factory.py` — `StateMachineMetaclass` handles class construction, state/transition validation |
| 18 | +- `state.py` / `event.py` — Descriptor-based `State` and `Event` definitions |
| 19 | +- `transition.py` / `transition_list.py` — Transition logic and composition (`|` operator) |
| 20 | +- `callbacks.py` — Priority-based callback registry (`CallbackPriority`, `CallbackGroup`) |
| 21 | +- `dispatcher.py` — Listener/observer pattern, `callable_method` wraps callables with signature adaptation |
| 22 | +- `signature.py` — `SignatureAdapter` for dependency injection into callbacks |
| 23 | +- `engines/sync.py`, `engines/async_.py` — Sync and async run-to-completion engines |
| 24 | +- `registry.py` — Global state machine registry (used by `MachineMixin`) |
| 25 | +- `mixins.py` — `MachineMixin` for domain model integration (e.g., Django models) |
| 26 | +- `spec_parser.py` — Boolean expression parser for condition guards |
| 27 | +- `contrib/diagram.py` — Diagram generation via pydot/Graphviz |
| 28 | + |
| 29 | +## Environment setup |
| 30 | + |
| 31 | +```bash |
| 32 | +uv sync --all-extras --dev |
| 33 | +pre-commit install |
| 34 | +``` |
| 35 | + |
| 36 | +## Running tests |
| 37 | + |
| 38 | +Always use `uv` to run commands: |
| 39 | + |
| 40 | +```bash |
| 41 | +# Run all tests (parallel) |
| 42 | +uv run pytest -n auto |
| 43 | + |
| 44 | +# Run a specific test file |
| 45 | +uv run pytest tests/test_signature.py |
| 46 | + |
| 47 | +# Run a specific test |
| 48 | +uv run pytest tests/test_signature.py::TestSignatureAdapter::test_wrap_fn_single_positional_parameter |
| 49 | + |
| 50 | +# Skip slow tests |
| 51 | +uv run pytest -m "not slow" |
| 52 | +``` |
| 53 | + |
| 54 | +Tests include doctests from both source modules (`--doctest-modules`) and markdown docs |
| 55 | +(`--doctest-glob=*.md`). Coverage is enabled by default. |
| 56 | + |
| 57 | +## Linting and formatting |
| 58 | + |
| 59 | +```bash |
| 60 | +# Lint |
| 61 | +uv run ruff check . |
| 62 | + |
| 63 | +# Auto-fix lint issues |
| 64 | +uv run ruff check --fix . |
| 65 | + |
| 66 | +# Format |
| 67 | +uv run ruff format . |
| 68 | + |
| 69 | +# Type check |
| 70 | +uv run mypy statemachine/ tests/ |
| 71 | +``` |
| 72 | + |
| 73 | +## Code style |
| 74 | + |
| 75 | +- **Formatter/Linter:** ruff (line length 99, target Python 3.9) |
| 76 | +- **Rules:** pycodestyle, pyflakes, isort, pyupgrade, flake8-comprehensions, flake8-bugbear, flake8-pytest-style |
| 77 | +- **Imports:** single-line, sorted by isort |
| 78 | +- **Docstrings:** Google convention |
| 79 | +- **Naming:** PascalCase for classes, snake_case for functions/methods, UPPER_SNAKE_CASE for constants |
| 80 | +- **Type hints:** used throughout; `TYPE_CHECKING` for circular imports |
| 81 | +- Pre-commit hooks enforce ruff + mypy + pytest |
| 82 | + |
| 83 | +## Design principles |
| 84 | + |
| 85 | +- **Decouple infrastructure from domain:** Modules like `signature.py` and `dispatcher.py` are |
| 86 | + general-purpose (signature adaptation, listener/observer pattern) and intentionally not coupled |
| 87 | + to the state machine domain. Prefer this separation even for modules that are only used |
| 88 | + internally — it keeps responsibilities clear and the code easier to reason about. |
| 89 | +- **Favor small, focused modules:** When adding new functionality, consider whether it can live in |
| 90 | + its own module with a well-defined boundary, rather than growing an existing one. |
| 91 | + |
| 92 | +## Building documentation |
| 93 | + |
| 94 | +```bash |
| 95 | +# Build HTML docs |
| 96 | +uv run sphinx-build docs docs/_build/html |
| 97 | + |
| 98 | +# Live reload for development |
| 99 | +uv run sphinx-autobuild docs docs/_build/html --re-ignore "auto_examples/.*" |
| 100 | +``` |
| 101 | + |
| 102 | +## Git workflow |
| 103 | + |
| 104 | +- Main branch: `develop` |
| 105 | +- PRs target `develop` |
| 106 | +- Releases are tagged as `v*.*.*` |
| 107 | +- Signed commits preferred (`git commit -s`) |
| 108 | +- Use [Conventional Commits](https://www.conventionalcommits.org/) messages |
| 109 | + (e.g., `feat:`, `fix:`, `refactor:`, `test:`, `docs:`, `chore:`, `perf:`) |
| 110 | + |
| 111 | +## Security |
| 112 | + |
| 113 | +- Do not commit secrets, credentials, or `.env` files |
| 114 | +- Validate at system boundaries; trust internal code |
0 commit comments