Pico-ioc integration for FastAPI. Adds Spring Boot-style controllers, autoconfiguration, and scopes (request, websocket, session).
Project description
📦 pico-fastapi
pico-fastapi integrates Pico-IoC with FastAPI, enabling constructor-based dependency injection, scoped lifecycles, and clean architectural boundaries — without global state or FastAPI dependency functions.
🐍 Requires Python 3.10+
✅ Fully async-compatible
✅ Real IoC (constructor injection, not function injection)
✅ Works with request, session, and websocket scopes
🎯 Why pico-fastapi?
FastAPI’s built-in dependency system is function-based, which makes business logic tightly coupled to the framework.
pico-fastapi moves dependency resolution to the IoC container.
| Concern | FastAPI Default | pico-fastapi |
|---|---|---|
| Dependency injection | Function-based | Constructor-based |
| Architecture | Framework-driven | Domain-driven |
| Testing | Must simulate DI functions | Component overrides at container init |
| Scopes | Manual or ad-hoc | singleton, request, session, websocket |
🧱 Core Features
@controllerclass-based routing@get,@post,@websocket, etc.- Constructor injection for controllers & services
- Automatic registration into FastAPI
- Scoped resolution via middleware (
request,session,websocket) - Full compatibility with Pico-IoC features: overrides, profiles, interceptors, cleanup
📦 Installation
pip install pico-fastapi
Also requires:
pip install pico-ioc fastapi
🚀 Quick Example
# controllers.py
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()}
# services.py
class MyService:
def greet(self) -> str:
return "hello from service"
# main.py
from pico_ioc import init
from fastapi import FastAPI
container = init(
modules=[
"controllers",
"services",
"pico_fastapi.factory",
]
)
app = container.get(FastAPI) # ✅ retrieve the fully configured app
💬 WebSocket Example
from pico_fastapi import controller, websocket
from fastapi import WebSocket
@controller
class ChatController:
async def __init__(self):
pass
@websocket("/ws")
async def chat(self, websocket: WebSocket):
await websocket.accept()
while True:
message = await websocket.receive_text()
await websocket.send_text(f"Echo: {message}")
🧪 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.factory"],
overrides={ "MyService": FakeService() }
)
app = container.get(FastAPI)
client = TestClient(app)
assert client.get("/api/hello").json() == {"msg": "test"}
⚙️ How It Works
@controllerclasses are registered automatically- HTTP/WebSocket handlers are wrapped in a request or websocket scope
- All dependencies (services, config, state) are resolved through Pico-IoC
- Cleanup happens at application shutdown via lifespan integration
No global state. No implicit singletons. No magic.
📝 License
MIT — See LICENSE.
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 pico_fastapi-0.1.0.tar.gz.
File metadata
- Download URL: pico_fastapi-0.1.0.tar.gz
- Upload date:
- Size: 16.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6bb3648c475c2e5f7a74ffa6c4ef43e93454d4cd8a64368c7663740fce3d14fb
|
|
| MD5 |
0608a0934bc85d67c87289858779efe3
|
|
| BLAKE2b-256 |
9b3abfaa75f5970a1ac378fa53b69761045a90a453eb20d25bec47c4cd9755fd
|
File details
Details for the file pico_fastapi-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pico_fastapi-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b740c4579533d96e650f6205634340457b31e9baf64f34827ce516efaac4d05
|
|
| MD5 |
9cff4acf07b3cb1021187170f3e755bb
|
|
| BLAKE2b-256 |
5ba587538fa713f05ddbdd10e445b7ca318967638b155e171d3cf949f2878f48
|