Dependency Injection Reference

Module Summary

next.deps exposes the resolver, the parameter providers, the dependency cache, and the public markers used in annotations.

Public API

Resolver

Walk a callable’s signature and fill its parameters from the context.

DependencyResolver is the orchestrator consumed by page views, form actions, and component renderers. It instantiates every subclass registered under RegisteredParameterProvider._registry on first use. Providers register by simply importing their module, which runs the __init_subclass__ hook on RegisteredParameterProvider.

class next.deps.resolver.DependencyResolver(*providers: ParameterProvider | RegisteredParameterProvider)[source]

Build keyword-arguments for a callable by consulting registered providers.

EXPLICIT_RESOLVE_KEYS: ClassVar[frozenset[str]] = frozenset({'_cache', '_context_data', '_stack', 'form', 'request'})
__get__(obj: object, owner: type[object]) DependencyResolver[source]

Return the resolver itself when accessed as a descriptor.

__init__(*providers: ParameterProvider | RegisteredParameterProvider) None[source]

Initialise with explicit providers or defer to the auto-registry.

register_dependency(name: str, callable_dep: Callable[..., Any]) Callable[..., Any][source]

Register a callable as a dependency reachable through Depends(“name”).

dependency(name: str) Callable[[Callable[..., Any]], Callable[..., Any]][source]

Return a decorator that registers the callable under the given name.

add_provider(provider: ParameterProvider) None[source]

Append a provider after the existing list.

register(provider: ParameterProvider | type[ParameterProvider]) ParameterProvider | type[ParameterProvider][source]

Register a provider, accepting either a class or an instance.

resolve(func: Callable[..., T], context: ResolutionContext) dict[str, Any][source]

Return keyword arguments ready to call func with the given context.

resolve_dependencies(func: Callable[..., Any], **context: object) dict[str, Any][source]

Resolve func from a loose kwargs mapping and build a context object.

resolve_with_template_context(func: Callable[..., Any], *, request: HttpRequest | None = None, template_context: dict[str, Any] | None = None, _cache: dict[str, Any] | DependencyCache | None = None, _stack: list[str] | None = None) dict[str, Any][source]

Resolve func for component callables using template context.

Keys from EXPLICIT_RESOLVE_KEYS are stripped from the context data so that name-based providers cannot shadow dedicated providers such as HttpRequestProvider on a parameter literally named request.

next.deps.resolver.resolver

The shared DependencyResolver singleton used by pages, form actions, and component renderers throughout the framework. Import it as from next.deps import resolver when you need to call resolver.resolve_dependencies from a custom provider or a test helper.

Providers

ParameterProvider is the minimal protocol the resolver consumes. RegisteredParameterProvider is the auto-registered base used by the built-in providers. Subclasses join the resolver’s registry through __init_subclass__, so the resolver instantiates them on first use without an explicit import.

Parameter-provider contracts and the auto-registry ABC.

ParameterProvider is the minimal Protocol consumed by DependencyResolver. RegisteredParameterProvider is the ABC used by built-in providers that ship with the framework. Subclasses of the ABC join the module-level _registry through __init_subclass__, which lets the resolver instantiate them on first use without importing them explicitly. The resolver consults providers in ascending priority order, so a lower priority value is checked first. ProviderRegistry is a lightweight list-style helper kept around for tests and future external consumers.

class next.deps.providers.ParameterProvider(*args, **kwargs)[source]

Minimal protocol consumed by DependencyResolver.

can_handle(param: inspect.Parameter, context: ResolutionContext) bool[source]

Return True when this provider owns the parameter.

resolve(param: inspect.Parameter, context: ResolutionContext) object[source]

Return the resolved value for the parameter.

__init__(*args, **kwargs)
class next.deps.providers.RegisteredParameterProvider[source]

Auto-registered base used by built-in providers shipped with the framework.

resolver: ClassVar[DependencyResolver]

Build keyword-arguments for a callable by consulting registered providers.

priority: ClassVar[int] = 100
classmethod __init_subclass__(**kwargs: object) None[source]

Track concrete subclasses for lazy instantiation by the resolver.

abstractmethod can_handle(param: inspect.Parameter, context: ResolutionContext) bool[source]

Return True when this provider owns the parameter.

abstractmethod resolve(param: inspect.Parameter, context: ResolutionContext) object[source]

Return the resolved value for the parameter.

Note

ProviderRegistry is internal. It is absent from next.deps.__all__ and carries no compatibility promise.

Markers

Annotation markers and the default Depends provider.

DDependencyBase is the shared parent for type-annotation markers such as DForm or DUrl. Depends is a dataclass default value used to request dependency resolution by name, by callable, or by constant injection. DependsProvider is the built-in parameter provider that handles the Depends marker and registers itself through RegisteredParameterProvider.

class next.deps.markers.DDependencyBase[source]

Shared base for annotation markers such as DForm and DUrl.

class next.deps.markers.Depends(dependency: object | None = None)[source]

Mark a parameter as a dependency resolved by the resolver.

Use as a default parameter value. Depends(“name”) resolves a registered callable by name. Depends(callable) calls a factory with DI-resolved arguments. Depends(value) injects a constant value directly. Depends() resolves by the parameter name.

dependency: object | None
__init__(dependency: object | None = None) None
class next.deps.markers.DependsProvider(resolver: DependencyResolver)[source]

Provider that resolves parameters whose default is a Depends marker.

priority = 10
__init__(resolver: DependencyResolver) None[source]

Store the resolver used for nested dependency calls.

can_handle(param: inspect.Parameter, context: ResolutionContext) bool[source]

Return True when the parameter default is a Depends marker.

resolve(param: inspect.Parameter, context: ResolutionContext) object[source]

Resolve a Depends marker by name, callable, or constant.

Cache

Sentinels, cycle error, and per-resolution cache used during DI resolution.

The DependencyCache object accumulates resolved dependency values during a single resolution pass. The _IN_PROGRESS and _CACHE_MISS sentinels separate the three cache-lookup outcomes (hit, miss, and in-progress) without collapsing None-valued hits into misses.

next.deps.cache.get_request_dep_cache(request: object | None) dict[str, Any] | None[source]

Return the dispatch-scoped dep cache attached to request, or None.

FormActionDispatch.dispatch attaches its dep_cache dict to the request so downstream renderers (page context, component context) can rejoin the same DI cache during a validation-failure re-render. Consumers wrap the returned dict in DependencyCache to share state.

exception next.deps.cache.DependencyCycleError(cycle: list[str])[source]

Raised when dependency resolution re-enters a key already in progress.

__init__(cycle: list[str]) None[source]

Record the offending dependency chain for the error message.

class next.deps.cache.DependencyCache(backing_dict: dict[str, Any] | None = None)[source]

Store resolved dependency values and detect cycles via in-progress keys.

__init__(backing_dict: dict[str, Any] | None = None) None[source]

Initialise storage, optionally sharing an externally owned dict.

get(key: str) object[source]

Return the cached value, _IN_PROGRESS, or _CACHE_MISS.

set(key: str, value: object) None[source]

Store a finished resolution under the given key.

mark_in_progress(key: str) None[source]

Mark the key as currently being resolved for cycle detection.

unmark_in_progress(key: str) None[source]

Clear the in-progress marker for the key.

is_in_progress(key: str) bool[source]

Return True while the key is mid-resolution.

__len__() int[source]

Return the number of stored values.

__contains__(key: str) bool[source]

Return membership in the backing dict.

Context

Resolution-context snapshot passed to providers during DI resolution.

ResolutionContext collects request, form, URL kwargs, and template context data into a single immutable view. Providers read from this object without mutating it. RESERVED_KEYS lists the kwarg names that DependencyResolver.resolve_dependencies treats as fixed inputs rather than URL kwargs.

class next.deps.context.ResolutionContext(request: HttpRequest | None, form: object | None, url_kwargs: Mapping[str, Any], context_data: Mapping[str, Any], cache: DependencyCache, stack: list[str] = <factory>)[source]

Immutable snapshot of the inputs available during dependency resolution.

request: HttpRequest | None
form: object | None
url_kwargs: Mapping[str, Any]
context_data: Mapping[str, Any]
cache: DependencyCache
stack: list[str]
__init__(request: HttpRequest | None, form: object | None, url_kwargs: Mapping[str, Any], context_data: Mapping[str, Any], cache: DependencyCache, stack: list[str] = <factory>) None

RESERVED_KEYS lists the names (request, form, _cache, _stack, _context_data) stripped from name-based resolution. A context key cannot shadow a reserved resolver input. DependencyResolver.EXPLICIT_RESOLVE_KEYS is the class-level alias of the same frozenset. Subclasses override it to broaden or narrow the stripped names, and the resolver reads self.EXPLICIT_RESOLVE_KEYS rather than the module constant during dispatch. See Dependency Resolver for the resolution detail.

Signals

See Signals Reference for the provider_registered signal.

Checks

The dependency injection layer registers no Django system checks.

See Also

See also

Dependency Injection for the topic guide. Dependency Resolver for the resolver internals.