Skip to main content

Dependant

This class represents a structured model for managing dependencies and their associated parameters, such as path, query, header, and body fields. It tracks nested dependencies, security scopes, and the execution context of a callable to determine its lifecycle and caching behavior. Additionally, it provides utilities to identify the nature of the callable, including whether it is a coroutine, generator, or asynchronous generator.

Attributes​

AttributeTypeDescription
path_paramslist[ModelField] = []List of model fields representing parameters extracted from the URL path.
query_paramslist[ModelField] = []List of model fields representing parameters extracted from the URL query string.
header_paramslist[ModelField] = []List of model fields representing parameters extracted from the HTTP request headers.
cookie_paramslist[ModelField] = []List of model fields representing parameters extracted from the HTTP cookies.
body_paramslist[ModelField] = []List of model fields representing parameters extracted from the HTTP request body.
dependencieslist[[Dependant](dependant.md?sid=fastapi_dependencies_models_dependant)] = []List of other Dependant objects that this dependency relies on.
name`strNone` = null
call`Callable[..., Any]None` = null
request_param_name`strNone` = null
websocket_param_name`strNone` = null
http_connection_param_name`strNone` = null
response_param_name`strNone` = null
background_tasks_param_name`strNone` = null
security_scopes_param_name`strNone` = null
own_oauth_scopes`list[str]None` = null
parent_oauth_scopes`list[str]None` = null
use_cachebool = trueDetermines whether the result of the dependency should be cached for the duration of the request.
path`strNone` = null
scope`Literal['function', 'request']None` = null

Constructor​

Signature​

def Dependant(
path_params: list[ModelField] = [],
query_params: list[ModelField] = [],
header_params: list[ModelField] = [],
cookie_params: list[ModelField] = [],
body_params: list[ModelField] = [],
dependencies: list[[Dependant](dependant.md?sid=fastapi_dependencies_models_dependant)] = [],
name: str | None = null,
call: Callable[..., Any]| None = null,
request_param_name: str | None = null,
websocket_param_name: str | None = null,
http_connection_param_name: str | None = null,
response_param_name: str | None = null,
background_tasks_param_name: str | None = null,
security_scopes_param_name: str | None = null,
own_oauth_scopes: list[str]| None = null,
parent_oauth_scopes: list[str]| None = null,
use_cache: bool = true,
path: str | None = null,
scope: Literal['function', 'request']| None = null
) - > null

Parameters​

NameTypeDescription
path_paramslist[ModelField] = []List of path parameters for the dependency.
query_paramslist[ModelField] = []List of query parameters for the dependency.
header_paramslist[ModelField] = []List of header parameters for the dependency.
cookie_paramslist[ModelField] = []List of cookie parameters for the dependency.
body_paramslist[ModelField] = []List of body parameters for the dependency.
dependencieslist[[Dependant](dependant.md?sid=fastapi_dependencies_models_dependant)] = []List of sub-dependencies required by this dependency.
name`strNone` = null
call`Callable[..., Any]None` = null
request_param_name`strNone` = null
websocket_param_name`strNone` = null
http_connection_param_name`strNone` = null
response_param_name`strNone` = null
background_tasks_param_name`strNone` = null
security_scopes_param_name`strNone` = null
own_oauth_scopes`list[str]None` = null
parent_oauth_scopes`list[str]None` = null
use_cachebool = trueWhether to cache the result of this dependency.
path`strNone` = null
scope`Literal['function', 'request']None` = null

Methods​


oauth_scopes()​

@classmethod
def oauth_scopes() - > list[str]

Aggregates all OAuth2 scopes required by this dependency and its parents. It preserves the order of scopes while ensuring uniqueness.

Returns​

TypeDescription
list[str]A list of unique OAuth2 scope strings required for authorization.

cache_key()​

@classmethod
def cache_key() - > DependencyCacheKey

Generates a unique identifier used to cache the result of the dependency execution. The key is composed of the callable, relevant OAuth scopes, and the execution scope.

Returns​

TypeDescription
DependencyCacheKeyA tuple containing the callable, sorted scopes, and scope string used for cache lookups.

is_gen_callable()​

@classmethod
def is_gen_callable() - > bool

Detects if the dependency callable is a standard Python generator function. It inspects the callable and its call method to support both functions and classes.

Returns​

TypeDescription
boolTrue if the callable is a synchronous generator, otherwise False.

is_async_gen_callable()​

@classmethod
def is_async_gen_callable() - > bool

Detects if the dependency callable is an asynchronous generator function. This is used to determine if the dependency requires an async context manager for execution.

Returns​

TypeDescription
boolTrue if the callable is an async generator, otherwise False.

is_coroutine_callable()​

@classmethod
def is_coroutine_callable() - > bool

Detects if the dependency callable is a coroutine function (async def). This helps the framework decide whether to await the dependency call.

Returns​

TypeDescription
boolTrue if the callable is an awaitable coroutine function, otherwise False.

computed_scope()​

@classmethod
def computed_scope() - > str | None

Calculates the lifecycle scope of the dependency based on its type. It defaults to 'request' for generator-based dependencies to ensure proper cleanup.

Returns​

TypeDescription
`strNone`