Skip to main content

Python custom implementation of unit of work pattern

Project description

pyUoW

Unit of Work for Python — composable units, transactional work managers, and a domain model toolkit.

📖 Read the docs  ·  Quickstart  ·  PyPI

Downloads Build Codecov Ruff Python versions License


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-firstEntity, AuditedEntity, SoftDeletableEntity, VersionedEntity, and an event-emitting Model base, 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyuow-0.9.2.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyuow-0.9.2-py3-none-any.whl (53.3 kB view details)

Uploaded Python 3

File details

Details for the file pyuow-0.9.2.tar.gz.

File metadata

  • Download URL: pyuow-0.9.2.tar.gz
  • Upload date:
  • Size: 22.7 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

Hashes for pyuow-0.9.2.tar.gz
Algorithm Hash digest
SHA256 bd7f91018442fbf39ab37e579f19cf7a9ac3e602d911381b74733988dc2742eb
MD5 e159b96f0d46736bbd5feb6adcea99ee
BLAKE2b-256 05f3b719456d6d3836f3b19849be1180ebd4c4a28bc0d4e19261162bda05563f

See more details on using hashes here.

File details

Details for the file pyuow-0.9.2-py3-none-any.whl.

File metadata

  • Download URL: pyuow-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 53.3 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

Hashes for pyuow-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b341152f35925daa0faed3d531143d2ceaeb5bd72403bb36bdc18a8693e68cf4
MD5 3c00ea7ec8aa6a7df803f341a405f273
BLAKE2b-256 e0f7f0b006095fc558e266a683e54e856f20951dc623f9d2d1c37ccd294e418d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page