|
|
|
@ -6,8 +6,14 @@ from typing import Any, Callable, List, Optional, Sequence, Union, Tuple, cast |
|
|
|
|
|
|
|
from fastapi._compat import ModelField |
|
|
|
from fastapi.security.base import SecurityBase |
|
|
|
from fastapi.types import EndpointDependencyCacheKey, LifespanDependencyCacheKey |
|
|
|
from typing_extensions import Literal, TypeAlias |
|
|
|
from fastapi.types import ( |
|
|
|
EndpointDependencyCacheKey, |
|
|
|
LifespanDependencyCacheKey, |
|
|
|
DependencyScope, |
|
|
|
LifespanDependencyScope, |
|
|
|
EndpointDependencyScope |
|
|
|
) |
|
|
|
from typing_extensions import TypeAlias |
|
|
|
|
|
|
|
if sys.version_info >= (3, 13): # pragma: no cover |
|
|
|
from inspect import iscoroutinefunction |
|
|
|
@ -21,14 +27,13 @@ class SecurityRequirement: |
|
|
|
scopes: Optional[Sequence[str]] = None |
|
|
|
|
|
|
|
|
|
|
|
@dataclass |
|
|
|
class LifespanDependant: |
|
|
|
call: Callable[..., Any] |
|
|
|
caller: Callable[..., Any] |
|
|
|
dependencies: List["LifespanDependant"] = field(default_factory=list) |
|
|
|
@dataclass(kw_only=True, slots=True) |
|
|
|
class _BaseDependant: |
|
|
|
name: Optional[str] = None |
|
|
|
use_cache: bool = True |
|
|
|
index: Optional[int] = None |
|
|
|
call: Optional[Callable[..., Any]] = None |
|
|
|
scope: DependencyScope = None |
|
|
|
use_cache: bool = True |
|
|
|
|
|
|
|
@cached_property |
|
|
|
def is_gen_callable(self) -> bool: |
|
|
|
@ -53,6 +58,14 @@ class LifespanDependant: |
|
|
|
dunder_call = getattr(self.call, "__call__", None) # noqa: B004 |
|
|
|
return iscoroutinefunction(dunder_call) |
|
|
|
|
|
|
|
|
|
|
|
@dataclass |
|
|
|
class LifespanDependant(_BaseDependant): |
|
|
|
scope: LifespanDependencyScope |
|
|
|
caller: Callable[..., Any] |
|
|
|
call: Callable[..., Any] |
|
|
|
dependencies: List["LifespanDependant"] = field(default_factory=list) |
|
|
|
|
|
|
|
@cached_property |
|
|
|
def cache_key(self) -> LifespanDependencyCacheKey: |
|
|
|
if self.use_cache: |
|
|
|
@ -66,7 +79,8 @@ class LifespanDependant: |
|
|
|
return self.caller, self.index |
|
|
|
|
|
|
|
@dataclass |
|
|
|
class EndpointDependant: |
|
|
|
class EndpointDependant(_BaseDependant): |
|
|
|
scope: EndpointDependencyScope = None |
|
|
|
path_params: List[ModelField] = field(default_factory=list) |
|
|
|
query_params: List[ModelField] = field(default_factory=list) |
|
|
|
header_params: List[ModelField] = field(default_factory=list) |
|
|
|
@ -75,9 +89,6 @@ class EndpointDependant: |
|
|
|
endpoint_dependencies: List["EndpointDependant"] = field(default_factory=list) |
|
|
|
lifespan_dependencies: List[LifespanDependant] = field(default_factory=list) |
|
|
|
security_requirements: List[SecurityRequirement] = field(default_factory=list) |
|
|
|
name: Optional[str] = None |
|
|
|
call: Optional[Callable[..., Any]] = None |
|
|
|
index: Optional[int] = None |
|
|
|
request_param_name: Optional[str] = None |
|
|
|
websocket_param_name: Optional[str] = None |
|
|
|
http_connection_param_name: Optional[str] = None |
|
|
|
@ -85,9 +96,7 @@ class EndpointDependant: |
|
|
|
background_tasks_param_name: Optional[str] = None |
|
|
|
security_scopes_param_name: Optional[str] = None |
|
|
|
security_scopes: Optional[List[str]] = None |
|
|
|
use_cache: bool = True |
|
|
|
path: Optional[str] = None |
|
|
|
scope: Union[Literal["function", "request"], None] = None |
|
|
|
|
|
|
|
@cached_property |
|
|
|
def cache_key(self) -> EndpointDependencyCacheKey: |
|
|
|
@ -97,29 +106,6 @@ class EndpointDependant: |
|
|
|
self.computed_scope or "", |
|
|
|
) |
|
|
|
|
|
|
|
@cached_property |
|
|
|
def is_gen_callable(self) -> bool: |
|
|
|
if inspect.isgeneratorfunction(self.call): |
|
|
|
return True |
|
|
|
dunder_call = getattr(self.call, "__call__", None) # noqa: B004 |
|
|
|
return inspect.isgeneratorfunction(dunder_call) |
|
|
|
|
|
|
|
@cached_property |
|
|
|
def is_async_gen_callable(self) -> bool: |
|
|
|
if inspect.isasyncgenfunction(self.call): |
|
|
|
return True |
|
|
|
dunder_call = getattr(self.call, "__call__", None) # noqa: B004 |
|
|
|
return inspect.isasyncgenfunction(dunder_call) |
|
|
|
|
|
|
|
@cached_property |
|
|
|
def is_coroutine_callable(self) -> bool: |
|
|
|
if inspect.isroutine(self.call): |
|
|
|
return iscoroutinefunction(self.call) |
|
|
|
if inspect.isclass(self.call): |
|
|
|
return False |
|
|
|
dunder_call = getattr(self.call, "__call__", None) # noqa: B004 |
|
|
|
return iscoroutinefunction(dunder_call) |
|
|
|
|
|
|
|
@cached_property |
|
|
|
def computed_scope(self) -> Union[str, None]: |
|
|
|
if self.scope: |
|
|
|
|