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.
Future Work
- Mypy extension to validate scripts. For example, prove that a script shouldn't fail
due to missing
context_datafor 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.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| safetydance-0.4.0.tar.gz | 31.6 kB | Without provenance | Details for safetydance-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| safetydance-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for safetydance-0.4.0-py3-none-any.whl |
Total release size: 45.6 kB
Release files / safetydance-0.4.0.tar.gz
| Download URL | safetydance-0.4.0.tar.gz |
|---|---|
| Size | 31.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d8425c1260e354059e110518d1a62501a29a814d70cbd8fd49fda601fcd7f784
|
|
BLAKE2b-256 checksum How to use checksums |
c76a51d6acfa82db39f124f3e4e3f04416d0590ce96dcd2929217bf57002dfc4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.0
|
Release files / safetydance-0.4.0-py3-none-any.whl
| Download URL | safetydance-0.4.0-py3-none-any.whl |
|---|---|
| Size | 14.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3b0401dfa30dab38a58ea74f67c02f026f7d18179a3bfbc19a0e225af4f40ab6
|
|
BLAKE2b-256 checksum How to use checksums |
b2443fbd7b2f9e8cb2e8d1b4982830807d5fce9d08860d0bc71ac01841f5a02b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.0
|
Security for safetydance 0.4.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.