Type-safe scope declaration generator for fundi
dependency injection
fundi-codegen
generates TypedDict
classes that describe the expected scope
structure for fundi
-based functions.
This allows you to call inject(scope, scan(...), stack)
with full type checking, autocomplete, and IDE support — no plugins or runtime introspection needed.
fundi-scopegen
analyzes a function (and its dependency tree) via scan(...)
, extracts all from_(...)
and outputs:
- a full list of all keys required in the
scope
- expected types (supporting
FromType
,Literal
,Optional
,Union
,Annotated
,Parameter
, etc.) - import statements for required symbols
- a
TypedDict
class that serves as a contract for what thescope
must contain
How it works:
- Recursively walks the dependency tree using
scan(...)
- Detects all
from_(...)
-based dependencies - Builds a
TypedDict
based on flattened parameter annotations - Outputs a ready-to-use Python type declaration
fundi-scopegen play.application
Output:
from typing import Any, TypedDict, NotRequired
from fundi import FromType
from fundi.types import Parameter
class ApplicationScope(TypedDict):
parameter: NotRequired[FromType[None | Parameter]]
app_name: NotRequired[str | None]
some: NotRequired[str]
username: Any
fundi-scopegen play.application --colored
fundi-scopegen play.application --no-imports
Output:
class ApplicationScope(TypedDict):
parameter: NotRequired[FromType[None | Parameter]]
app_name: NotRequired[str | None]
some: NotRequired[str]
username: Any
from contextlib import ExitStack
from fundi import inject, scan
from play import application
from scopes import ApplicationScope
scope: ApplicationScope = {
"username": "kuyugama",
"some": "waffel",
}
with ExitStack() as stack:
result = inject(scope, scan(application), stack)
- Full IDE autocomplete
- Type-checking with
mypy
orpyright
- Lightweight: no runtime cost or decorators
- Easy to regenerate on dependency changes
🐾 Stop guessing your scope shape. Generate it once, and type it forever 😼