Skip to main content

Canary Framework

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

CI PyPI Python License

中文 · 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:
        print(f"connecting to {self.config.database_url}")  # 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.2

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.2
File Size Uploaded
canary_framework-0.9.2.tar.gz 75.4 kB Details

Built distribution (wheel)

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

Total release size: 115.0 kB

Release files / canary_framework-0.9.2.tar.gz

Download URL canary_framework-0.9.2.tar.gz
Size 75.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ce7fa65086008d26b21cb7666c817b1f62d50c4d346b0b4d4affb2d13cb8ea40
BLAKE2b-256 checksum
How to use checksums
1604240641f2f070106a77075388c7df190ed6018e4f1a0dbc3f1204ff88b955
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 19, 2026.

Transparency log

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

Download URL canary_framework-0.9.2-py3-none-any.whl
Size 39.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0316210efb6846774a1d6a5b4b945cfe6f53b86062591fb310bc40f42cc848b2
BLAKE2b-256 checksum
How to use checksums
3696ba9978d9863df9e9680357aa56e197d1ba9f1cdbd8a5efe1019f53dc78ca
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 19, 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

This release

0.9.2 This release

2 release files

0.9.1

2 release files

0.9.0

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