Skip to main content

Canary Framework

A minimal, decorator-driven framework for dependency injection, lifecycle, and ASGI web apps — in plain Python.

中文 · Documentation · Changelog

Install

pip install canary-framework            # core
pip install "canary-framework[web]"     # + web extension (ASGI / OpenAPI)

Requires Python 3.12+.

The model

  • cocoa is the smallest runnable unit — a plain Python class marked with @cocoa. Dependencies are declared with deps=[...]; @on_init / @on_start / @on_stop declare optional lifecycle behaviour.
  • Canary is the orchestrator. Canary(*roots) resolves the dependency graph, topologically sorts it, and drives the full lifecycle — and is itself an ASGI application.

Quick start

import asyncio

from canary_framework import Canary, cocoa, on_start


@cocoa
class Config:
    def __init__(self) -> None:
        self.database_url = "postgresql://localhost/dev"


@cocoa(deps=[Config])
class Database:
    @on_start
    async def connect(self) -> None:
        await self.pool.connect()  # self.config is injected


@cocoa(deps=[Database])
class UserService: ...


async def main() -> None:
    app = Canary(UserService)
    await app.init()  # build the graph, run @on_init
    await app.start()  # inject deps, run @on_start
    assert app[Database].config is app[Config]
    await app.stop()  # run @on_stop in reverse order


asyncio.run(main())

Dependency injection

Cocoas declare dependencies with deps=[...] — no __init__ plumbing, no DSL. Each dependency is injected lazily as self.<snake_case_name> at start():

@cocoa(deps=[Database, Cache])
class UserService:
    def __init__(self) -> None:
        self._ready = False  # no dependency wiring here

Canary resolves the graph from the roots, injects one shared instance per type, and drives initialization and startup in topological order.

Lifecycle

Three optional hooks — each sync or async, any number per stage:

Stage Decorator Runs
Init @on_init init(), topological order
Start @on_start start(), topological order, deps injected
Stop @on_stop stop(), reverse topological order
@cocoa(deps=[Config])
class Database:
    @on_init
    def build_pool(self) -> None: ...

    @on_start
    async def connect(self) -> None: ...

    @on_stop
    async def disconnect(self) -> None: ...

Web apps

The web extension turns a @cocoa service into a FastAPI-style ASGI app with automatic OpenAPI docs:

from pydantic import BaseModel
from canary_framework import Canary
from canary_framework.web import get, post, web_cocoa


class BorrowRequest(BaseModel):
    member_id: int


@web_cocoa(deps=[BookRepository, LibraryService])
class LibraryAPI:
    @get("/books/{book_id}")
    async def get_book(self, book_id: int) -> dict: ...

    @post("/books/{book_id}/borrow")
    async def borrow(self, book_id: int, body: BorrowRequest) -> dict: ...


app = Canary(LibraryAPI)  # `app` is the ASGI application
uvicorn examples.library.web:app --reload
# GET /docs  ·  /redoc  ·  /openapi.json

Examples

Runnable examples live in examples/, from a minimal unit through dependency injection, lifecycle hooks, multi-root composition, and a layered library web app.

Documentation

License

Apache-2.0.

Release files for canary-framework 0.9.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 canary-framework 0.9.0
File Size Uploaded
canary_framework-0.9.0.tar.gz 65.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for canary-framework 0.9.0
File Interpreter ABI Platform
canary_framework-0.9.0-py3-none-any.whl Python 3 none any Details

Total release size: 101.1 kB

Release files / canary_framework-0.9.0.tar.gz

Download URL canary_framework-0.9.0.tar.gz
Size 65.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a33d2e39e90007e582dbbae04e9b04e8e472a08f983e6c22078a2b25bd9a68d4
BLAKE2b-256 checksum
How to use checksums
5ff03331947eb59e4a2b2ca0f948773d3d269cd35cbdf15d5b848b3c3e0df15b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Aug 15, 2026.

Transparency log

Release files / canary_framework-0.9.0-py3-none-any.whl

Download URL canary_framework-0.9.0-py3-none-any.whl
Size 35.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7406ab36374567e3a4bb9cfb6dd84bfabee6c6815194ee519bdb9c567aac55b9
BLAKE2b-256 checksum
How to use checksums
9b8a802b699184eeb36f05242975398105cbf560ff3014f1dfc1c14dde7440f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Aug 15, 2026.

Transparency log

Release history Release notifications | RSS feed

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.10.0

2 release files

0.9.2

2 release files

0.9.1

2 release files

This release

0.9.0 This release

2 release files

0.6.1

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.13

2 release files

0.4.9

2 release files

0.4.8

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

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