fastapi_di
Depends で依存を明示する汎用 DI。解決戦略(リゾルバ)を差し替えられる。
名前のとおり FastAPI の DI に似せている。Depends / Dependant /
solve_dependencies の設計を踏襲しつつ、HTTP 依存(Request、query/path/body、
pydantic 検証)を全て剥がした。FastAPI には依存せず(依存パッケージなし)、
Web に限らずパイプライン処理・バッチ・CLI・ライブラリ内部の配線に使える。
- 依存は明示
Dependsのみ。型アノテーションによる自動ワイヤリングはしない。 書いていないものは注入されない。 - 解析と実行が分かれている。シグネチャを辿るだけで依存グラフが得られる(実行しない)。
- 参照の意味を差し替えられる。
Depends("name")の"name"が何を指すかは リゾルバが決める。既定は完全一致だが、チェーンや凍結表に差し替えられる。
要件: Python >= 3.14 / 依存パッケージなし。
インストール
uv add fastapi_di
使う
import asyncio
from typing import Annotated
from fastapi_di import Depends, Injector
def get_settings() -> dict:
return {"dsn": "postgres://localhost"}
def get_dsn(settings: Annotated[dict, Depends(get_settings)]) -> str:
return settings["dsn"]
class Database:
def __init__(self, dsn: str = Depends(get_dsn)) -> None:
self.dsn = dsn
async def main() -> None:
injector = Injector()
async with injector.scope() as scope: # スコープ = 依存の生存期間
db = await scope.inject(Database) # Database -> get_dsn -> get_settings
print(db.dsn)
asyncio.run(main())
クラス内の派生値(depend_property)
depend_property は getter の引数を同じクラスのメンバから解決するプロパティ。
Injector とは無縁の同期ディスクリプタで、Depends の構文だけを共有する。
既定はキャッシュしない(毎回再計算)。cache="auto" にすると依存から戦略が
伝搬し(cached_property だけに依存すれば自分もキャッシュ、property が混ざれば
毎回再計算)、cache=True で強制キャッシュする。
from functools import cached_property
from fastapi_di import Depends, depend_property
class Service:
@cached_property
def user(self) -> dict:
return {"name": "alice", "deleted_at": None}
@depend_property(cache="auto")
def is_active_user(self, user=Depends(user)) -> bool:
return user["deleted_at"] is None
print(Service().is_active_user) # True(user が cached かつ cache="auto" なのでキャッシュされる)
詳細は 使う を参照。
ビジョン
狙いは依存性の解決を標準化すること。三段階で考えている。
- 小回りの利く小さな DI — 数十行のスクリプトから使える。フレームワークを
持ち込まず、
Dependsを書くだけ。今ここ。 - パイプライン処理などへの展開 — 依存グラフは DAG そのもの。同じ宣言から 実行順序・並列性・キャッシュ境界を導ける。DI とワークフローを別物にしない。
- 依存関係の定義フォーマット — OpenAPI が HTTP インターフェースに対して 果たした役割を、依存関係に対して果たすもの。何が何に依存しているかを 機械可読な形で宣言・可視化・検証でき、言語や実装をまたいで共有できる形式。
graph.to_json() と bindings.json は、その 3 番目に向けた最初の一歩。
前者が「どう繋がっているか」、後者が「なぜそう繋がったか」を外に出す。
ドキュメント
- 使う — スコープ、setup/teardown、差し替え、名前での参照
- 依存グラフを取り出す —
to_json、層タグ、visualize(Mermaid / LLM テキスト) - リゾルバとマニフェスト — 解決戦略の差し替え、配線の凍結
- 開発 — make タスク、examples
Metadata
Release files for fastapi-di 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fastapi_di-0.1.2.tar.gz | 83.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_di-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 131.5 kB
Release files / fastapi_di-0.1.2.tar.gz
| Download URL | fastapi_di-0.1.2.tar.gz |
|---|---|
| Size | 83.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c8bf7f5229be971a35c49eb5a8e4ea063c295dcffedcbc9d8e51c1e22170f72d
|
|
BLAKE2b-256 checksum How to use checksums |
eda95772292ec77977a4d0222c1e55fcdc030cd0ab070ea86079d0fcc868e4ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.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 Jul 24, 2026.
Transparency logRelease files / fastapi_di-0.1.2-py3-none-any.whl
| Download URL | fastapi_di-0.1.2-py3-none-any.whl |
|---|---|
| Size | 48.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
54b42aea37f3ca2dc35101dd612d9ddf74e3a8e89c0e0c31dd40c847260de99e
|
|
BLAKE2b-256 checksum How to use checksums |
7d81af6df643ee2200b79389994cc84f03b38714d34f666fc7817bc549279b47
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.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 Jul 24, 2026.
Transparency log