Skip to main content

safetydance

safetydance is a Python framework for defining type-safe and flexibly composable application steps with a shared execution context for sharing variables between the steps. The design of safetydance is partially inspired by Cucumber.

pip install safetydance

Documentation: https://dcharbon.gitlab.io/safetydance/

Description

safetydance lets plain functions act as steps that share data through an execution context, and provides decorators that turn a function into a script, which runs its steps in a fresh context.

A step should be a function with a name that reads easily, like a short phrase or sentence. Steps may take arguments and they may access variables defined for the "context scope". Steps may call other steps, too.

A script is a function, decorated with @script, composing the execution of a series of steps. Each call to a script runs in its own execution context, so scripts never see each other's context data: not when one script calls another, and not when scripts run concurrently in asyncio tasks. Generator and async functions can be scripts, too.

To share context, call a step rather than a script; a step always uses its caller's context. A flow that is useful both on its own and inside larger scripts can be written as a step and wrapped once, e.g. log_in_script = script(log_in).

from safetydance import context_data, initializer, script


@context_data
class Shop:
    customer: str
    cart: list[tuple[str, float]] = initializer(lambda ctx: [])
    total: float = 0.0


def customer_named(name: str):
    Shop.customer = name


def adds_to_cart(item: str, price: float):
    Shop.cart.append((item, price))


def checks_out():
    Shop.total = sum(price for _, price in Shop.cart)


@script
def buy_breakfast() -> float:
    customer_named("Ada")
    adds_to_cart("coffee", 3.50)
    adds_to_cart("bagel", 2.50)
    checks_out()
    return Shop.total

A default wrapped in initializer(...) is called with the context on first read, giving each context its own value; any other default is a constant. Decorated scripts keep their signatures for type checkers.

In tests, @test_context works like @script and also puts the call's keyword arguments, such as pytest fixtures, into the context by name.

Context Scope Variables

A context scope variable is sort of like a global variable. The run of a script defines a context where context_data variables are stored for access by steps.

Think of a conversation between two friends. Much of the conversation will reference assumed shared knowledge, or context. For safetydance, the context scope provides a way for steps to share assumed context to make it possible to provide a more conversational style of programming

Migrating from 0.3

0.4 is a new API, not compatible with 0.3: @step functions are plain functions, step_data and ContextKey became ContextProperty and @context_data, and nested scripts no longer share a context. See the migration guide, or pin safetydance<0.4 until you can migrate. 0.5 brings back an optional @step, which only registers a plain function so Given, When, Then and And can find it; see Given, When, Then.

Future Work

  • Mypy extension to validate scripts. For example, prove that a script shouldn't fail due to missing context_data for any step in the script.
  • Dry run execution of steps
  • DAG derivation for scripts
  • Parallel evaluation for independent up to a join for DAG legs of a script.
  • Diagram output for script DAGs.

Setup for Development

uv sync                # create .venv with the dev dependency group
uv run pytest          # run the tests on the default interpreter
uv run nox             # run the tests on every supported Python version, and mypy
uv build               # build the sdist and wheel into dist/

The docs need Node, which mise installs with the pinned uv (see mise.toml). The guide builds into docs/_build/html, the API reference into docs/_build/html/api:

mise install                          # install the pinned Node and uv
mise exec -- uv run --locked nox -s docs

Metadata

Release files for safetydance 0.5.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 safetydance 0.5.0
File Size Uploaded Provenance Actions
safetydance-0.5.0.tar.gz 39.2 kB Without provenance Details for safetydance-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for safetydance 0.5.0
File Interpreter ABI Platform Actions
safetydance-0.5.0-py3-none-any.whl Python 3 none any Details for safetydance-0.5.0-py3-none-any.whl

Total release size: 57.3 kB

Release files / safetydance-0.5.0.tar.gz

Download URL safetydance-0.5.0.tar.gz
Size 39.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b5154f34928db0e9bf0079d924f7f8c4d92792ccc2c7074c2f2984d9e0634527
BLAKE2b-256 checksum
How to use checksums
50c19b83510a0336edea0be15588ce0645c91f1cef298eb02e0326face4908c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.0

Release files / safetydance-0.5.0-py3-none-any.whl

Download URL safetydance-0.5.0-py3-none-any.whl
Size 18.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b7e940333b22b07b53781f97cd775838b34dd6079d2187ee4c5b1f86993a06ad
BLAKE2b-256 checksum
How to use checksums
e9274614885865529eab2049f9ad3ca291a81271a243a22734e40d3683468b5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.0

Security for safetydance 0.5.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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