inversipy¶
A powerful and type-safe dependency injection/IoC (Inversion of Control) library for Python 3.12+.
Why inversipy?¶
- Zero runtime dependencies - Pure Python, nothing to install beyond the library itself
- Type annotation-based - Dependencies resolved using Python type hints
- Pure classes - No container coupling; classes remain framework-agnostic
- Async-first - First-class support for async dependencies and factories
- Validated - Catch configuration errors at startup, not at runtime
Key Features¶
- Container with registration, resolution, and composition
- Three scopes: Singleton, Transient, and Request
- Module system with public/private access control
- Property injection via
Injectablebase class - Named dependencies for multiple implementations
- Collection injection with
InjectAll - Factory & Lazy injection for deferred resolution
- Optional dependencies with
T | None - Container freezing for runtime safety
- FastAPI integration
- Full MyPy support with strict typing
Quick Example¶
from inversipy import Container, Scopes
class Database:
def query(self, sql: str) -> list:
return ["result"]
class UserRepository:
def __init__(self, db: Database) -> None:
self.db = db
def get_users(self) -> list:
return self.db.query("SELECT * FROM users")
class UserService:
def __init__(self, repo: UserRepository) -> None:
self.repo = repo
def list_users(self) -> list:
return self.repo.get_users()
# Create container and register dependencies
container = Container()
container.register(Database, scope=Scopes.SINGLETON)
container.register(UserRepository)
container.register(UserService)
# Validate and freeze
container.validate()
container.freeze()
# Resolve dependencies
service = container.get(UserService)
users = service.list_users()