Dependant
This class represents a dependency in an application, encapsulating information about how a particular callable (function, method, or class) should be invoked, including its parameters, sub-dependencies, and security requirements. It manages various types of parameters (path, query, header, cookie, body) and recursively handles nested dependencies. The class also provides properties to determine the callable's type (generator, async generator, coroutine) and its associated security scopes.
Attributes
| Attribute | Type | Description |
|---|---|---|
| path_params | list[ModelField] = [] | Stores a list of ModelField objects representing path parameters extracted from the request, used to validate and inject values into the dependent callable. |
| query_params | list[ModelField] = [] | Stores a list of ModelField objects representing query parameters extracted from the request, used to validate and inject values into the dependent callable. |
| header_params | list[ModelField] = [] | Stores a list of ModelField objects representing header parameters extracted from the request, used to validate and inject values into the dependent callable. |
| cookie_params | list[ModelField] = [] | Stores a list of ModelField objects representing cookie parameters extracted from the request, used to validate and inject values into the dependent callable. |
| body_params | list[ModelField] = [] | Stores a list of ModelField objects representing body parameters extracted from the request, used to validate and inject values into the dependent callable. |
| dependencies | list["Dependant"] = [] | Stores a list of other Dependant objects that this Dependant relies on, forming a dependency chain for resolution. |
| 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 | A boolean indicating whether the result of this Dependant's callable should be cached, improving performance for repeated calls. |
| path | `str | None` = null |
| scope | `Literal["function", "request"] | None` = null |
Methods
oauth_scopes()
@classmethod
def oauth_scopes() - > list[str]
Retrieves the combined OAuth scopes required by this dependant and its parent dependants. This method ensures that scopes are unique while preserving their order.
Returns
| Type | Description |
|---|---|
list[str] | A list of unique OAuth scope strings, maintaining the order of appearance. |
cache_key()
@classmethod
def cache_key() - > DependencyCacheKey
Generates a unique key for caching the dependant's resolution. This key is based on the callable, sorted unique OAuth scopes (if applicable), and the computed scope.
Returns
| Type | Description |
|---|---|
DependencyCacheKey | A tuple representing the cache key, consisting of the callable, sorted unique OAuth scopes, and the computed scope string. |
is_gen_callable()
@classmethod
def is_gen_callable() - > bool
Determines if the dependant's callable is a generator function. This is used to identify callables that yield values, typically for dependency injection with cleanup.
Returns
| Type | Description |
|---|---|
bool | True if the callable is a generator function, False otherwise. |
is_async_gen_callable()
@classmethod
def is_async_gen_callable() - > bool
Determines if the dependant's callable is an asynchronous generator function. This is used to identify async callables that yield values, typically for dependency injection with asynchronous cleanup.
Returns
| Type | Description |
|---|---|
bool | True if the callable is an asynchronous generator function, False otherwise. |
is_coroutine_callable()
@classmethod
def is_coroutine_callable() - > bool
Determines if the dependant's callable is a coroutine function. This is used to identify asynchronous functions that need to be awaited.
Returns
| Type | Description |
|---|---|
bool | True if the callable is a coroutine function, False otherwise. |
computed_scope()
@classmethod
def computed_scope() - > str | None
Calculates the scope of the dependant, which can be 'function' or 'request'. This determines the lifecycle of the dependency.
Returns
| Type | Description |
|---|---|
| `str | None` |