Skip to content

InjectQ Documentation

PyPI version Python versions License Coverage

InjectQ is a type-friendly dependency injection library for Python. It supports sync and async code, scopes, modules, and FastAPI, FastMCP and Taskiq integrations.

Using an AI coding agent?

Give it skill.md: one file with the recommended approach, API, pitfalls and limitations. Rendered page: AI Agent Skill.

Quick example

from injectq import InjectQ, inject, singleton

container = InjectQ.get_instance()
container[str] = "Hello from InjectQ"   # UserService needs a str

@singleton
class UserService:
    def __init__(self, message: str):
        self.message = message

    def greet(self) -> str:
        return f"Service says: {self.message}"

@inject
def main(service: UserService) -> None:
    print(service.greet())

if __name__ == "__main__":
    main()

Features

  • Dict-like API, bind() methods, @inject and Inject[T]
  • Lifetimes: @singleton, @transient, @scoped("request"), with exit stacks for cleanup
  • Factories: call_factory(), plus invoke() / ainvoke() for factories that mix injected and manual arguments
  • Async APIs: aget(), acall_factory(), ainvoke(), async_scope()
  • Modules and providers: Module, SimpleModule, ProviderModule, @provider
  • A resolver compiled per service (generated code, or plain closures with debug=True)
  • Safe under threads and asyncio tasks: the current container and scopes live in ContextVars, and reads take no lock
  • Integrations: InjectFastAPI, InjectFastMCP, InjectTaskiq (optional extras)
  • Diagnostics: dependency graph, validation, visualizer
  • Testing: container.override(...), override_dependency, isolated_container, InjectQ.test_mode()

Core patterns

Dict-like interface

container = InjectQ.get_instance()
container[str] = "config_value"
container[Database] = Database()

Function injection

@inject
def process(service: UserService):
    ...

Factory with injected and manual arguments

def create_service(db: Database, cache: Cache, user_id: str):
    return UserService(db, cache, user_id)

container.bind_factory("user_service", create_service)

# db and cache are injected, user_id is passed by hand
service = container.invoke("user_service", user_id="user123")
service = await container.ainvoke("async_service", batch_size=100)  # async factories

See Factory Methods.

Scopes and lifetimes

from injectq import InjectQ, singleton, transient, scoped

container = InjectQ.get_instance()

@singleton
class Database: ...        # one instance app-wide

@transient
class Validator: ...       # new instance every resolution

@scoped("request")
class RequestContext: ...  # one instance per request scope

with container.scope("request"):    # async: `async with container.async_scope("request"):`
    ctx1 = container.get(RequestContext)
    ctx2 = container.get(RequestContext)
    assert ctx1 is ctx2
# Outside the block, container.get(RequestContext) raises ScopeError

FastAPI

from typing import Annotated
from fastapi import FastAPI
from injectq import InjectQ, singleton
from injectq.integrations.fastapi import setup_fastapi, InjectFastAPI

app = FastAPI()
container = InjectQ.get_instance()
setup_fastapi(container, app)

@singleton
class UserService:
    def get_user(self, user_id: int) -> dict:
        return {"id": user_id}

@app.get("/users/{user_id}")
async def get_user(
    user_id: int,
    user_service: Annotated[UserService, InjectFastAPI(UserService)],
):
    return user_service.get_user(user_id)

Taskiq

from typing import Annotated
from injectq import InjectQ
from injectq.integrations.taskiq import setup_taskiq, InjectTaskiq

container = InjectQ.get_instance()
setup_taskiq(container, broker)

@broker.task()
async def save_data(
    data: dict,
    service: Annotated[RankingService, InjectTaskiq(RankingService)],
):
    await service.save(data)

Modules and providers

from injectq import InjectQ
from injectq.modules import Module, SimpleModule, ProviderModule, provider

class AppModule(Module):
    def configure(self, binder):
        binder.bind(Config, Config())
        binder.bind(Database, Database)

class Services(SimpleModule):
    def __init__(self):
        super().__init__()
        self.bind(UserService, UserService)

class Providers(ProviderModule):
    @provider
    def make_notifier(self, db: Database, cfg: Config) -> Notifier:
        return Notifier(db, cfg)

container = InjectQ(modules=[AppModule(), Services(), Providers()])

Testing

from injectq import InjectQ
from injectq.testing import override_dependency, isolated_container

container = InjectQ.get_instance()

with override_dependency(Database, MockDatabase()):
    service = container.get(UserService)

with isolated_container() as test_container:
    test_container.bind(Database, MockDatabase)
    ...

Where to go next

Performance

injectq compared with 13 other Python dependency injection libraries on the same object graphs. Every library first passes the same correctness checks, then is timed in its own process. Lower is better.

µs/op (median, lower is better) S1 singleton S2 transient chain S3 wide graph S4 request scope S5 injected call S6 async chain
injectq 0.083 0.302 0.550 0.968 0.143 0.427
aioinject [1] 0.295 0.787 1.768 2.278 n/a 0.992
dependency-injector [2] 0.057 1.203 3.038 n/a 1.127 37.8
dishka [3] 0.307 0.516 1.069 1.526 1.165 1.068
diwire [4] 0.424 0.609 0.884 4.609 6.820 0.874
inject [5] 0.158 4.785 12.2 n/a 2.462 n/a
injector [6] 1.331 42.7 90.6 14.4 16.8 n/a
kink [7] 0.168 3.688 12.6 n/a 2.366 n/a
lagom [8] 0.155 5.624 12.0 n/a 1.474 n/a
punq [9] 1.097 36.5 90.6 n/a n/a n/a
rodi [10] 0.290 0.758 1.852 0.836 1.056 n/a
svcs [11] 0.189 2.590 8.087 1.940 n/a 3.068
that-depends [12] 0.077 0.938 3.479 24.7 3.163 1.569
wireup [13] 0.092 0.512 1.434 1.130 0.269 0.676
hand-written (floor) 0.019 0.166 0.355 0.345 0.041 0.229
injectq rank 3 of 14 1 of 14 1 of 14 2 of 9 1 of 11 1 of 8
fastest DI library dependency-injector injectq injectq rodi injectq injectq

Bold: fastest DI library in the column (hand-written excluded). n/a: the library has no such feature.

Setup notes per library ([1]-[13]) [1] **aioinject**: sync `SyncContainer` for S1-S5/S7, async `Container` for S6; transients registered in the lifetime scope to resolve from the root context. n/a: S5 no generic injection decorator (per-framework `inject` only). [2] **dependency-injector**: S6 uses providers in async mode. n/a: S4 no enter/exit request scope (only context-local singletons, a different mechanism). [3] **dishka**: no transient lifetime; `cache=False` is its equivalent. [4] **diwire**: default `SCOPED` lifetime at the root scope = singleton; transient via `Lifetime.TRANSIENT`; S5 passes `diwire_resolver=` explicitly. [5] **inject**: one global injector; singletons via `bind_to_constructor`, transients via `bind_to_provider`, constructors wired with `autoparams`. n/a: S4 no request scope; S6 no async resolution. [6] **injector**: request scope is a custom `Scope` subclass (cache swapped on enter/exit); constructors need `@inject` (applied to the benchmark classes). n/a: S6 no async API. [7] **kink**: `@inject` on a class registers it (singleton; `use_factory=True` = transient) and mutates `__init__`; S7 restores `__init__` before re-decorating. n/a: S4 no request scope; S6 no async resolution. [8] **lagom**: unregistered classes are autowired as transient. n/a: S4 no request scope; S6 no async resolution. [9] **punq**: transient is the default scope. n/a: S4 no request scope (only transient and singleton); S5 no injection decorator; S6 no async API. [10] **rodi**: resolved with `provider.get` / `provider.exec` (each call creates an activation scope); S7 includes `build_provider()`, which validates and wires eagerly. n/a: S6 no async resolution (sync only). [11] **svcs**: caches per `Container`, so transient = a new `Container` per operation (its construction is timed). n/a: S5 no generic injection decorator (framework integrations only). [12] **that-depends**: S5 with `@inject(scope=None)`; S4 via a `ContextResource` in `ContextScopes.REQUEST`. [13] **wireup**: transients and scoped services resolve from a scope: S2, S3, S6 use one long-lived scope, S4/S5 enter one per operation.

14 libraries, AMD Ryzen 5 5600G with Radeon Graphics, CPython 3.13.13, median of 25 repeats, 2026-11-23. Full table, chart and method: docs/benchmarks.md.

Scenarios: S1 resolve a cached singleton, S2 a 3-level transient chain, S3 a wide graph (5 transients sharing a singleton), S4 enter a request scope, resolve, exit, S5 call an @inject function with 3 dependencies, S6 S2 through the async API. Full tables, chart and method: Benchmarks. Raw JSON: benchmarks/results/.

Contributing & License

See CONTRIBUTING.md and LICENSE for contribution rules and licensing.