Auto-Wiring¶
spryx-di resolves dependencies automatically by inspecting __init__ type hints. No decorators, no registration of the handler itself.
class ListHandler:
def __init__(self, repo: ConversationRepo, reader: TeamReader) -> None:
self._repo = repo
self._reader = reader
handler = ctx.resolve(ListHandler)
# repo and reader are resolved from the container
How It Works¶
- Read
__init__type hints (excludingselfandreturn) - For each parameter,
resolve()the hinted type recursively - If a type has a default value and isn't registered, use the default
- Skip
*argsand**kwargsparameters - Construct the class with all resolved dependencies
Errors¶
UnresolvableTypeError¶
A required dependency isn't registered and has no default:
UnresolvableTypeError: Cannot resolve 'ListHandler'.
Parameter 'reader' expects type 'TeamReader' which is not registered.
Hint: Register it with container.register(TeamReader, <implementation>)
CircularDependencyError¶
Two or more types depend on each other:
TypeHintRequiredError¶
A parameter has no type hint: