Skip to main content

pico-fastapi

PyPI Ask DeepWiki License: MIT CI (tox matrix) codecov Quality Gate Status Duplicated Lines (%) Maintainability Rating Docs Interactive Lab

Pico-FastAPI

Pico-FastAPI seamlessly integrates Pico-IoC with FastAPI, bringing true inversion of control and constructor-based dependency injection to one of the fastest and most elegant Python web frameworks.

It provides scoped lifecycles, automatic controller registration, and clean architectural boundaries, without global state and without FastAPI’s function-based dependency system.

Requires Python 3.11+ Built on FastAPI Fully async-compatible Real IoC with constructor injection Supports singleton, request, session, and websocket scopes

With Pico-FastAPI you get the speed, clarity, and async performance of FastAPI, enhanced by a real IoC container for clean, testable, and maintainable applications.


Why pico-fastapi

FastAPI’s built-in dependency system is function-based, which often ties business logic to the framework. Pico-FastAPI moves dependency resolution into the IoC container, promoting separation of concerns and testability.

Concern FastAPI Default pico-fastapi
Dependency injection Function-based Constructor-based
Architecture Framework-driven Domain-driven
Testing Simulate DI calls Override components in container
Scopes Manual or ad-hoc Automatic (singleton, request, session, websocket)

Core Features

  • Controller classes with @controller
  • Route decorators: @get, @post, @put, @delete, @patch, @websocket
  • Constructor injection for controllers and services
  • Automatic registration into FastAPI
  • Scoped resolution via middleware for request, session, and websocket
  • Full Pico-IoC feature set: profiles, overrides, interceptors, cleanup hooks

Installation

pip install pico-fastapi

Quick Example

from pico_fastapi import controller, get

@controller(prefix="/api")
class ApiController:
    def __init__(self, service: "MyService"):
        self.service = service

    @get("/hello")
    async def hello(self):
        return {"msg": self.service.greet()}
from pico_ioc import component

@component
class MyService:
    def greet(self) -> str:
        return "hello from service"
from pico_ioc import init
from fastapi import FastAPI

container = init(
    modules=[
        "controllers",
        "services",
        "pico_fastapi",
    ]
)

app = container.get(FastAPI)

Quick Example (with pico-boot auto-discovery)

1. Controller

from pico_fastapi import controller, get

@controller(prefix="/api")
class ApiController:
    def __init__(self, service: "MyService"):
        self.service = service

    @get("/hello")
    async def hello(self):
        return {"msg": self.service.greet()}

2. Service

from pico_ioc import component

@component
class MyService:
    def greet(self) -> str:
        return "hello from service"

3. App Initialization (Using pico-boot)

from pico_boot import init
from fastapi import FastAPI

# No need to declare "pico_fastapi" anymore.
# pico-fastapi is auto-discovered via entry points.

container = init(
    modules=[
        "controllers",
        "services",
    ]
)

app = container.get(FastAPI)

WebSocket Example

from pico_fastapi import controller, websocket
from fastapi import WebSocket

@controller
class ChatController:
    @websocket("/ws")
    async def chat(self, websocket: WebSocket):
        await websocket.accept()
        while True:
            msg = await websocket.receive_text()
            await websocket.send_text(f"Echo: {msg}")

Testing with Overrides

from pico_ioc import init
from fastapi import FastAPI
from fastapi.testclient import TestClient

class FakeService:
    def greet(self) -> str:
        return "test"

container = init(
    modules=["controllers", "services", "pico_fastapi"],
    overrides={"MyService": FakeService()}
)

app = container.get(FastAPI)
client = TestClient(app)

assert client.get("/api/hello").json() == {"msg": "test"}

Static Files Configuration Example

from dataclasses import dataclass
from typing import Protocol, runtime_checkable
from fastapi import FastAPI
from starlette.staticfiles import StaticFiles
from pico_ioc import component, configured
from pico_fastapi import FastApiConfigurer

@configured(target="self", prefix="fastapi", mapping="tree")
@dataclass
class StaticSettings:
    static_dir: str = "public"
    static_url: str = "/static"

@component
class StaticFilesConfigurer(FastApiConfigurer):
    priority = -100
    def __init__(self, settings: StaticSettings):
        self.settings = settings
    def configure_app(self, app: FastAPI) -> None:
        app.mount(self.settings.static_url, StaticFiles(directory=self.settings.static_dir), name="static")
from pico_ioc import init, configuration, YamlTreeSource
from fastapi import FastAPI

container = init(
    modules=[
        "pico_fastapi",
        "static_config",
    ],
    config=configuration(
        YamlTreeSource("config.yml")
    ),
)

app = container.get(FastAPI)
fastapi:
  title: "My App"
  version: "1.0.0"
  debug: true
  static_dir: "public"
  static_url: "/assets"

JWT Authentication Configuration Example

import base64
import json
import hmac
import hashlib
from dataclasses import dataclass
from typing import Optional
from fastapi import FastAPI, Request
from starlette.middleware.base import BaseHTTPMiddleware
from pico_ioc import component, configured, PicoContainer
from pico_fastapi import FastApiConfigurer

def _b64url_decode(data: str) -> bytes:
    padding = "=" * (-len(data) % 4)
    return base64.urlsafe_b64decode(data + padding)

def _verify_hs256(token: str, secret: str) -> Optional[dict]:
    parts = token.split(".")
    if len(parts) != 3:
        return None
    header_b64, payload_b64, sig_b64 = parts
    signing_input = f"{header_b64}.{payload_b64}".encode()
    expected = hmac.new(secret.encode(), signing_input, hashlib.sha256).digest()
    try:
        signature = _b64url_decode(sig_b64)
    except Exception:
        return None
    if not hmac.compare_digest(signature, expected):
        return None
    try:
        payload_json = _b64url_decode(payload_b64)
        return json.loads(payload_json.decode())
    except Exception:
        return None

class JwtMiddleware(BaseHTTPMiddleware):
    def __init__(self, app, container: PicoContainer, secret: str):
        super().__init__(app)
        self.container = container
        self.secret = secret
    async def dispatch(self, request: Request, call_next):
        auth = request.headers.get("Authorization", "")
        if auth.startswith("Bearer "):
            token = auth.split(" ", 1)[1]
            claims = _verify_hs256(token, self.secret)
            if claims is not None:
                request.state.jwt_claims = claims
        response = await call_next(request)
        return response

@dataclass
class JwtSettings:
    secret: str = "changeme"
    header: str = "Authorization"

@component
class JwtConfigurer(FastApiConfigurer):
    priority = 10
    def __init__(self, container: PicoContainer, settings: JwtSettings):
        self.container = container
        self.settings = settings
    def configure_app(self, app: FastAPI) -> None:
        app.add_middleware(JwtMiddleware, container=self.container, secret=self.settings.secret)
from pico_ioc import init
from fastapi import FastAPI, Request
from pico_fastapi import controller, get

@controller(prefix="/api")
class ProfileController:
    def __init__(self):
        pass
    @get("/me")
    async def me(self, request: Request):
        claims = getattr(request.state, "jwt_claims", None)
        if claims is None:
            return {"error": "not authenticated"}, 401
        return {"sub": claims.get("sub")}

container = init(
    modules=[
        "pico_fastapi",
        "jwt_config",
        "controllers",
    ]
)

app = container.get(FastAPI)

How It Works

  • Controller classes are discovered and registered automatically
  • Each route executes within its own request or websocket scope
  • All dependencies are resolved via Pico-IoC
  • Cleanup and teardown occur at FastAPI lifespan

No global state and no implicit singletons.


AI Coding Skills

Install Claude Code or OpenAI Codex skills for AI-assisted development with pico-fastapi:

curl -sL https://raw.githubusercontent.com/dperezcabrera/pico-skills/main/install.sh | bash -s -- fastapi
Command Description
/add-controller Add FastAPI controllers with route decorators
/add-component Add components, factories, interceptors, settings
/add-tests Generate tests for pico components

All skills: curl -sL https://raw.githubusercontent.com/dperezcabrera/pico-skills/main/install.sh | bash

See pico-skills for details.


License

MIT

Download files

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

Source Distribution

pico_fastapi-0.3.1.tar.gz (82.9 kB view details)

Uploaded Source

Built Distribution

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

pico_fastapi-0.3.1-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file pico_fastapi-0.3.1.tar.gz.

File metadata

  • Download URL: pico_fastapi-0.3.1.tar.gz
  • Upload date:
  • Size: 82.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pico_fastapi-0.3.1.tar.gz
Algorithm Hash digest
SHA256 ffe248df7a8f3f3ce72497244c8e16acc5856964b13725369d8c3510c50ee52d
MD5 6babf7710b91eeccfdd3c60786674628
BLAKE2b-256 a93c0785cdf37c3396abcf9aad18347ffaaefe0cfb11a20295b20cfa64d94de1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pico_fastapi-0.3.1.tar.gz:

Publisher: publish-to-pypi.yml on dperezcabrera/pico-fastapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pico_fastapi-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pico_fastapi-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 19.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pico_fastapi-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 07d9425e9008a9552e18d4f47c89720a5808f2b166eb71b07bd8a984ad1bb152
MD5 2e34624ed948f47288c00a0fe2fc747c
BLAKE2b-256 4ce1a9f5278a634189595085c152db0a6bfb4d485062c2280794a09235d33a1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pico_fastapi-0.3.1-py3-none-any.whl:

Publisher: publish-to-pypi.yml on dperezcabrera/pico-fastapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.4.0

2 files

This release

0.3.1 This release

2 files

0.3.0

2 files

0.2.2

2 files

0.2.0

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 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