Skip to main content

State Machine v0.3.0

A lightweight, type-safe Python state machine implementation using modern Python 3.13+ features like PEP 695 type parameters.

Features

  • Type Safety: Leverage Python's type system to ensure consistent states, events, and contexts.
  • Decorator Support: Easily define transitions using the @sm.transition decorator.
  • Auditing: Built-in support for recording transition history via AuditedStateMachine and AuditContext.
  • Flexible Context: Pass a custom context object to transition actions to maintain state outside the machine.

Installation

This project uses uv for dependency management. To get started, ensure you have uv installed and run:

uv sync

You can install the package via pip:

pip install semantic-state-machine

Usage

Basic State Machine

Define your states and events as Enums and create a context class.

from enum import Enum
from semantic_state_machine import StateMachine

class State(Enum):
    LOCKED = 1
    UNLOCKED = 2

class Event(Enum):
    PUSH = 1
    COIN = 2

class TurnstileContext:
    def __init__(self):
        self.total_coins = 0

sm = StateMachine[State, Event, TurnstileContext]()

@sm.transition(State.LOCKED, Event.COIN, State.UNLOCKED)
def insert_coin(ctx: TurnstileContext) -> None:
    ctx.total_coins += 1
    print("Coin inserted. Turnstile unlocked.")

@sm.transition(State.UNLOCKED, Event.PUSH, State.LOCKED)
def push_turnstile(ctx: TurnstileContext) -> None:
    print("Turnstile pushed. Turnstile locked.")

# Usage
ctx = TurnstileContext()
current_state = State.LOCKED
current_state = sm.handle_transition(ctx, current_state, Event.COIN)
# current_state is now State.UNLOCKED

Audited State Machine

Use AuditedStateMachine and AuditContext to automatically track the history of transitions.

from semantic_state_machine import AuditedStateMachine, AuditContext

class MyContext(AuditContext[State, Event]):
    pass

sm = AuditedStateMachine[State, Event, MyContext]()

# Transitions are defined the same way
@sm.transition(State.LOCKED, Event.COIN, State.UNLOCKED)
def insert_coin(ctx: MyContext):
    pass

ctx = MyContext()
sm.handle_transition(ctx, State.LOCKED, Event.COIN)

# The transition is automatically recorded
print(ctx.audit_trail())  # [(State.LOCKED, Event.COIN)]

# Example of a custom audit trail formatter (e.g., counting transitions)
transition_count = ctx.audit_trail(lambda trail: len(trail))
print(f"Total transitions: {transition_count}")  # Total transitions: 1

Testing

The project includes a comprehensive suite of unit and integration tests.

Run All Tests

uv run pytest tests/

Run with Coverage

uv run pytest --cov=src tests/

The current implementation maintains 100% code coverage across the core logic and integration workflows.

Project Structure

  • src/semantic_state_machine/: Core implementation.
  • tests/unit/: Focused unit tests for individual components.
  • tests/integration/: End-to-end workflow simulations.
  • tests/hypothesis/: Property-based tests for robust edge-case discovery.

Development Standards

This project adheres to professional, type-safe development practices:

  • Pre-commit: We enforce linting (ruff), formatting (ruff-format), and type checking (ty) using pre-commit hooks.
  • Tiered Testing:
    • Fast Lane: Unit and integration tests run on every commit via the check CI job.
    • Robustness Lane: Property-based fuzzing using hypothesis executes during Pull Requests to uncover edge cases.
  • CI/CD: The pipeline is configured to enforce quality, with static analysis and fast tests acting as a gateway for property-based robustness testing.

Metadata

Release files for semantic-state-machine 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for semantic-state-machine 0.3.0
File Size Uploaded Provenance
semantic_state_machine-0.3.0.tar.gz 4.7 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for semantic-state-machine 0.3.0
File Interpreter ABI Platform
semantic_state_machine-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.4 kB

Release files / semantic_state_machine-0.3.0.tar.gz

Download URL semantic_state_machine-0.3.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
76b22652e5829bc63b4daaa3e336bab43a1c095d74eb1adf7fd7cdcf74de5963
BLAKE2b-256 checksum
How to use checksums
3b7c196ce4d83782f09027101999e67bd35aea0d58d804ef4149dbb31eef024e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 13, 2026.

Transparency log

Release files / semantic_state_machine-0.3.0-py3-none-any.whl

Download URL semantic_state_machine-0.3.0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90317c2df72c48b0e94d7939c0db7a64ac6e5177a28ca2d378ac95115e51afdf
BLAKE2b-256 checksum
How to use checksums
021638f2f98cbbe577fe93339194ba2c43f8b4f8e618e419bdbe22a443999c15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 13, 2026.

Transparency log

Security for semantic-state-machine 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for semantic_state_machine-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 13, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page