Python custom implementation of unit of work pattern
Project description
Unit of Work for Python — composable units, transactional work managers, and a domain model toolkit.
📖 Read the docs · Quickstart · PyPI
What is PyUoW?
PyUoW is a Python implementation of the Unit of Work behavioural pattern. It gives you small, typed building blocks for business logic that compose into a flow and run inside a Work Manager that handles transactions, batching, and domain events.
The result: business logic that reads top-to-bottom, separates orchestration from execution, and stays testable as it grows.
Highlights
- Composable units — write each step as a
ConditionalUnit/RunUnit/FinalUnit, chain them with>>, validate the chain at build time. - Transactional out of the box — a SQLAlchemy adapter ships in
pyuow.contrib.sqlalchemy(sync + async), with nested-transaction support. - Domain-first —
Entity,AuditedEntity,SoftDeletableEntity,VersionedEntity, and an event-emittingModelbase, all immutable dataclasses. - Sync and async parity — every primitive has an
aio/twin; pick one per flow. - Strict types — passes
mypy --strict; runs on Python 3.10 through 3.14.
Install
pip install pyuow # core
pip install "pyuow[sqlalchemy]" # with SQLAlchemy integration
Python ≥ 3.10.
AI-agent skill
After installing, run pyuow install-skill to drop an AI-agent skill file into your project so Claude Code / OpenCode learn PyUoW's idioms. Add --global to install for your user account, or --check to dry-run.
See the step-by-step guide (or docs/install-skill.md).
At a glance
from dataclasses import dataclass
from pyuow import (
BaseContext, BaseParams, ConditionalUnit, ErrorUnit,
FinalUnit, Result, RunUnit,
)
from pyuow.work.noop import NoOpWorkManager
@dataclass(frozen=True)
class Greeting(BaseParams):
name: str
@dataclass
class Ctx(BaseContext[Greeting]):
params: Greeting
class IsNamePresent(ConditionalUnit[Ctx, str]):
def condition(self, ctx: Ctx) -> bool:
return bool(ctx.params.name)
class Greet(RunUnit[Ctx, str]):
def run(self, ctx: Ctx) -> None:
print(f"Hello, {ctx.params.name}!")
class Done(FinalUnit[Ctx, str]):
def finish(self, ctx: Ctx) -> Result[str]:
return Result.ok("greeted")
flow = (
IsNamePresent(on_failure=ErrorUnit(exc=ValueError("name required")))
>> Greet()
>> Done()
).build()
result = NoOpWorkManager().by(flow).do_with(Ctx(params=Greeting(name="Alice")))
assert result.get() == "greeted"
What's in the box
| Concept | What it gives you |
|---|---|
| Units & Flow | ConditionalUnit / RunUnit / FinalUnit / ErrorUnit, chained with >> |
| Result | ok / error / empty plus .map, .and_then, .unwrap_or |
| Context | Mutable, immutable, and domain-aware context bases |
| Work Manager | NoOp, transactional, and domain-transactional managers |
| Domain Model | Entity, AuditedEntity, Model, Batch, events, typed exceptions |
| DataPoints | Typed producer / consumer contracts between units |
| SQLAlchemy | Ready-made repositories, table mixins, and transaction manager |
| Async | A sync and an aio/ twin for every primitive |
Contributing
PRs welcome — see CONTRIBUTING.md for the dev setup, or run:
poetry install --with docs
make tests # pytest + coverage
make fmt # ruff + mypy
make docs-serve # live preview at http://127.0.0.1:8000
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyuow-0.9.1.tar.gz.
File metadata
- Download URL: pyuow-0.9.1.tar.gz
- Upload date:
- Size: 22.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.4.1 CPython/3.14.5 Linux/6.17.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d5a3710f2f77099ba2b9230840674d6fc2e784613e3c242ccf92f99dcb20ada
|
|
| MD5 |
d3bd6c5b59ce82708d05954ab3a4bde0
|
|
| BLAKE2b-256 |
82ccc1d701a151fba17a16322bfef643bcf3244efb2c5fb0d980af7a5abd4191
|
File details
Details for the file pyuow-0.9.1-py3-none-any.whl.
File metadata
- Download URL: pyuow-0.9.1-py3-none-any.whl
- Upload date:
- Size: 53.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.4.1 CPython/3.14.5 Linux/6.17.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db3494d775ab06227fcf78c169821e2a2fd0200d87997304a672ff76cf75da8d
|
|
| MD5 |
0fc529dff953ad767315daff910eaf11
|
|
| BLAKE2b-256 |
6b720962df6fac46655bf4c1d0ae998384c44a1a84bad98f9e6cd0c818b0a213
|