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 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
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 fastapi_di-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_di-0.1.0.tar.gz
- Upload date:
- Size: 67.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a263dafa08fb6dba08c71c1549889c50bde14ce6d0a9e496101740a25bb25093
|
|
| MD5 |
077053fcfdeb7d0e15c86fa368d1054c
|
|
| BLAKE2b-256 |
ff8daf42fe65682d9939f5ac2fc8455f75b02dc81ee1aa4b9de35a69bbc02428
|
Provenance
The following attestation bundles were made for fastapi_di-0.1.0.tar.gz:
Publisher:
release.yml on sasano8/fastapi_di
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_di-0.1.0.tar.gz -
Subject digest:
a263dafa08fb6dba08c71c1549889c50bde14ce6d0a9e496101740a25bb25093 - Sigstore transparency entry: 2227405888
- Sigstore integration time:
-
Permalink:
sasano8/fastapi_di@713ddcb193e7b0a3a32d6aeb61db8271795678aa -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/sasano8
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@713ddcb193e7b0a3a32d6aeb61db8271795678aa -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastapi_di-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_di-0.1.0-py3-none-any.whl
- Upload date:
- Size: 34.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc1b3e4fade4d3f79d07c1c991e886a07f7df8117d3825c7f8cca3377eba8419
|
|
| MD5 |
f53091ee4374a42f533d2455a9a09e30
|
|
| BLAKE2b-256 |
bfe85c5f8ef0d4e807319a1a8163dd7148d90a707d3efff4261d8fb14f85780a
|
Provenance
The following attestation bundles were made for fastapi_di-0.1.0-py3-none-any.whl:
Publisher:
release.yml on sasano8/fastapi_di
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_di-0.1.0-py3-none-any.whl -
Subject digest:
fc1b3e4fade4d3f79d07c1c991e886a07f7df8117d3825c7f8cca3377eba8419 - Sigstore transparency entry: 2227406487
- Sigstore integration time:
-
Permalink:
sasano8/fastapi_di@713ddcb193e7b0a3a32d6aeb61db8271795678aa -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/sasano8
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@713ddcb193e7b0a3a32d6aeb61db8271795678aa -
Trigger Event:
push
-
Statement type: