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
| Attribute | Type | Description |
|---|---|---|
| path_params | list[ModelField] = [] | List of model fields representing parameters extracted from the URL path. |
| query_params | list[ModelField] = [] | List of model fields representing parameters extracted from the URL query string. |
| header_params | list[ModelField] = [] | List of model fields representing parameters extracted from the HTTP request headers. |
| cookie_params | list[ModelField] = [] | List of model fields representing parameters extracted from the HTTP cookies. |
| body_params | list[ModelField] = [] | List of model fields representing parameters extracted from the HTTP request body. |
| dependencies | list[[Dependant](dependant.md?sid=fastapi_dependencies_models_dependant)] = [] | List of other Dependant objects that this dependency relies on. |
| 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 | Determines whether the result of the dependency should be cached for the duration of the request. |
| path | `str | None` = 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
| Name | Type | Description |
|---|---|---|
| path_params | list[ModelField] = [] | List of path parameters for the dependency. |
| query_params | list[ModelField] = [] | List of query parameters for the dependency. |
| header_params | list[ModelField] = [] | List of header parameters for the dependency. |
| cookie_params | list[ModelField] = [] | List of cookie parameters for the dependency. |
| body_params | list[ModelField] = [] | List of body parameters for the dependency. |
| dependencies | list[[Dependant](dependant.md?sid=fastapi_dependencies_models_dependant)] = [] | List of sub-dependencies required by this dependency. |
| 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 | Whether to cache the result of this dependency. |
| path | `str | None` = 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
| Type | Description |
|---|---|
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
| Type | Description |
|---|---|
DependencyCacheKey | A 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
| Type | Description |
|---|---|
bool | True 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
| Type | Description |
|---|---|
bool | True 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
| Type | Description |
|---|---|
bool | True 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
| Type | Description |
|---|---|
| `str | None` |