diwire
A high-performance, type-driven dependency injection container for Python 3.10+ featuring compiled resolvers, deterministic cleanup, and native async support for modern applications.
diwire is a high-performance, type-driven dependency injection container designed for Python 3.10+. It simplifies object graph construction by leveraging Python's type system to resolve dependencies automatically. Created to provide a clean and intuitive experience, diwire focuses on a small, stable public API that emphasizes readability and developer productivity over framework-heavy abstractions. By treating constructor signatures as the primary source of truth for component wiring, it minimizes boilerplate and enables seamless integration into various architectural layers from web handlers to backend services.
Functionality includes resolving complex object graphs, managing component lifetimes, and handling scoping with deterministic cleanup. The library provides advanced features such as compiled resolvers for high-performance steady-state execution, native support for asynchronous programming, and the use of open generics to enhance type safety. It ensures resource management is robust through integrated support for generators and context managers that handle cleanup automatically when scopes exit, making it suitable for managing database sessions, HTTP clients, and other resource-intensive components.
Some of the key features are:
- Type-Driven Wiring: Constructs object graphs automatically by inspecting type hints in constructors.
- Compiled Resolvers: Offers a compile() method to precompute resolution paths for optimal performance on hot execution paths.
- Deterministic Cleanup: Manages the lifecycle of resources using generators and context managers with automatic cleanup upon scope termination.
- Zero Runtime Dependencies: Provides a lightweight footprint, making it easy to integrate into any project environment.
- Async Support: Offers first-class support for asynchronous resolution and providers, enabling seamless use with async-native frameworks.
- Scoped Management: Supports configurable lifetimes including transient and scoped, facilitating effective memory and resource management.
- Framework Integrations: Includes built-in support for FastAPI, Litestar, aiohttp, Flask, Django, and other major Python web frameworks.
- Open Generics: Allows for flexible registration and resolution of generic types, improving code reusability.
Operationally, developers begin by defining their dependencies as classes and then instantiating a diwire Container. The container acts as the registry for all components, handling the recursive resolution of dependencies. For web applications, developers typically utilize the provided middleware—such as the RequestContextMiddleware for FastAPI—to manage request-scoped dependencies. By decorating endpoints with ResolverContext.inject(), applications can inject dependencies directly into functions, ensuring that object lifetimes remain consistent with the scope of the request or task. The system allows for both explicit registration of providers and implicit auto-registration based on type signatures.
Some common use cases include:
- Web API Development: Managing request-scoped database sessions, repositories, and service layers in frameworks like FastAPI or Litestar.
- Background Tasks: Handling scoped dependencies for Celery workers to ensure clean resource release after job completion.
- Test Automation: Simplifying test setup by overriding dependencies via the container to inject mocks or stubs without modifying application code.
- Application Lifecycle Management: Controlling the lifecycle of complex objects like database connections or API clients, ensuring they are correctly instantiated and torn down at the right time.
- Clean Architecture: Implementing clean, decoupled architectures where business logic remains unaware of how its infrastructure dependencies are created or managed.
Comments
0Markdown is supported.