Ignacio Peluffo 1 month ago
committed by GitHub
parent
commit
2992982808
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 240
      fastapi/dependencies/models.py

240
fastapi/dependencies/models.py

@ -2,8 +2,8 @@ import inspect
import sys import sys
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass, field from dataclasses import dataclass, field
from functools import cached_property, partial from functools import partial
from typing import Any, Literal from typing import Any, Literal, cast
from fastapi._compat import ModelField from fastapi._compat import ModelField
from fastapi.security.base import SecurityBase from fastapi.security.base import SecurityBase
@ -18,17 +18,17 @@ else: # pragma: no cover
def _unwrapped_call(call: Callable[..., Any] | None) -> Any: def _unwrapped_call(call: Callable[..., Any] | None) -> Any:
if call is None: if call is None:
return call # pragma: no cover return call # pragma: no cover
unwrapped = inspect.unwrap(_impartial(call)) unwrapped = inspect.unwrap(cast(Callable[..., Any], _impartial(call)))
return unwrapped return unwrapped
def _impartial(func: Callable[..., Any]) -> Callable[..., Any]: def _impartial(func: Callable[..., Any] | None) -> Callable[..., Any] | None:
while isinstance(func, partial): while isinstance(func, partial):
func = func.func func = func.func
return func return func
@dataclass @dataclass(slots=True)
class Dependant: class Dependant:
path_params: list[ModelField] = field(default_factory=list) path_params: list[ModelField] = field(default_factory=list)
query_params: list[ModelField] = field(default_factory=list) query_params: list[ModelField] = field(default_factory=list)
@ -49,145 +49,239 @@ class Dependant:
use_cache: bool = True use_cache: bool = True
path: str | None = None path: str | None = None
scope: Literal["function", "request"] | None = None scope: Literal["function", "request"] | None = None
# Lazy cached fields
_oauth_scopes_cache: list[str] | None = field(default=None, init=False, repr=False)
_cache_key_cache: DependencyCacheKey | None = field(
default=None, init=False, repr=False
)
_uses_scopes_cache: bool | None = field(default=None, init=False, repr=False)
_is_security_scheme_cache: bool | None = field(default=None, init=False, repr=False)
_security_scheme_cache: SecurityBase | None = field(
default=None, init=False, repr=False
)
_security_dependencies_cache: list["Dependant"] | None = field(
default=None, init=False, repr=False
)
_is_gen_callable_cache: bool | None = field(default=None, init=False, repr=False)
_is_async_gen_callable_cache: bool | None = field(
default=None, init=False, repr=False
)
_is_coroutine_callable_cache: bool | None = field(
default=None, init=False, repr=False
)
_computed_scope_cache: str | None = field(default=None, init=False, repr=False)
@cached_property @property
def oauth_scopes(self) -> list[str]: def oauth_scopes(self) -> list[str]:
if self._oauth_scopes_cache is None:
scopes = self.parent_oauth_scopes.copy() if self.parent_oauth_scopes else [] scopes = self.parent_oauth_scopes.copy() if self.parent_oauth_scopes else []
# This doesn't use a set to preserve order, just in case # This doesn't use a set to preserve order, just in case
for scope in self.own_oauth_scopes or []: for scope in self.own_oauth_scopes or []:
if scope not in scopes: if scope not in scopes:
scopes.append(scope) scopes.append(scope)
return scopes self._oauth_scopes_cache = scopes
@cached_property return self._oauth_scopes_cache
@property
def cache_key(self) -> DependencyCacheKey: def cache_key(self) -> DependencyCacheKey:
if self._cache_key_cache is None:
scopes_for_cache = ( scopes_for_cache = (
tuple(sorted(set(self.oauth_scopes or []))) if self._uses_scopes else () tuple(sorted(set(self.oauth_scopes or []))) if self._uses_scopes else ()
) )
return ( self._cache_key_cache = (
self.call, self.call,
scopes_for_cache, scopes_for_cache,
self.computed_scope or "", self.computed_scope or "",
) )
@cached_property return self._cache_key_cache
@property
def _uses_scopes(self) -> bool: def _uses_scopes(self) -> bool:
if self._uses_scopes_cache is None:
if self.own_oauth_scopes: if self.own_oauth_scopes:
return True self._uses_scopes_cache = True
if self.security_scopes_param_name is not None: elif self.security_scopes_param_name is not None:
return True self._uses_scopes_cache = True
if self._is_security_scheme: elif self._is_security_scheme:
return True self._uses_scopes_cache = True
for sub_dep in self.dependencies: for sub_dep in self.dependencies:
if sub_dep._uses_scopes: if sub_dep._uses_scopes:
return True self._uses_scopes_cache = True
return False break
if self._uses_scopes_cache is None:
self._uses_scopes_cache = False
@cached_property return self._uses_scopes_cache
@property
def _is_security_scheme(self) -> bool: def _is_security_scheme(self) -> bool:
if self._is_security_scheme_cache is None:
if self.call is None: if self.call is None:
return False # pragma: no cover self._is_security_scheme_cache = False # pragma: no cover
else:
unwrapped = _unwrapped_call(self.call) unwrapped = _unwrapped_call(self.call)
return isinstance(unwrapped, SecurityBase) self._is_security_scheme_cache = isinstance(unwrapped, SecurityBase)
return self._is_security_scheme_cache
# Mainly to get the type of SecurityBase, but it's the same self.call # Mainly to get the type of SecurityBase, but it's the same self.call
@cached_property @property
def _security_scheme(self) -> SecurityBase: def _security_scheme(self) -> SecurityBase:
if self._security_scheme_cache is None:
unwrapped = _unwrapped_call(self.call) unwrapped = _unwrapped_call(self.call)
assert isinstance(unwrapped, SecurityBase) assert isinstance(unwrapped, SecurityBase)
return unwrapped self._security_scheme_cache = unwrapped
@cached_property return self._security_scheme_cache
@property
def _security_dependencies(self) -> list["Dependant"]: def _security_dependencies(self) -> list["Dependant"]:
security_deps = [dep for dep in self.dependencies if dep._is_security_scheme] if self._security_dependencies_cache is None:
return security_deps security_deps = [
dep for dep in self.dependencies if dep._is_security_scheme
]
self._security_dependencies_cache = security_deps
return self._security_dependencies_cache
@cached_property @property
def is_gen_callable(self) -> bool: def is_gen_callable(self) -> bool:
if self._is_gen_callable_cache is None:
if self.call is None: if self.call is None:
return False # pragma: no cover self._is_gen_callable_cache = False # pragma: no cover
if inspect.isgeneratorfunction( elif inspect.isgeneratorfunction(
_impartial(self.call) _impartial(self.call)
) or inspect.isgeneratorfunction(_unwrapped_call(self.call)): ) or inspect.isgeneratorfunction(_unwrapped_call(self.call)):
return True self._is_gen_callable_cache = True
if inspect.isclass(_unwrapped_call(self.call)): elif inspect.isclass(_unwrapped_call(self.call)):
return False self._is_gen_callable_cache = False
if self._is_gen_callable_cache is not None:
return self._is_gen_callable_cache
dunder_call = getattr(_impartial(self.call), "__call__", None) # noqa: B004 dunder_call = getattr(_impartial(self.call), "__call__", None) # noqa: B004
if dunder_call is None: if dunder_call is None:
return False # pragma: no cover self._is_gen_callable_cache = False # pragma: no cover
if inspect.isgeneratorfunction( elif inspect.isgeneratorfunction(
_impartial(dunder_call) _impartial(dunder_call)
) or inspect.isgeneratorfunction(_unwrapped_call(dunder_call)): ) or inspect.isgeneratorfunction(_unwrapped_call(dunder_call)):
return True self._is_gen_callable_cache = True
dunder_unwrapped_call = getattr(_unwrapped_call(self.call), "__call__", None) # noqa: B004
if self._is_gen_callable_cache is not None:
return self._is_gen_callable_cache
dunder_unwrapped_call = getattr( # noqa: B004
_unwrapped_call(self.call),
"__call__",
None,
)
if dunder_unwrapped_call is None: if dunder_unwrapped_call is None:
return False # pragma: no cover self._is_gen_callable_cache = False # pragma: no cover
if inspect.isgeneratorfunction( if inspect.isgeneratorfunction(
_impartial(dunder_unwrapped_call) _impartial(dunder_unwrapped_call)
) or inspect.isgeneratorfunction(_unwrapped_call(dunder_unwrapped_call)): ) or inspect.isgeneratorfunction(_unwrapped_call(dunder_unwrapped_call)):
return True self._is_gen_callable_cache = True
return False else:
self._is_gen_callable_cache = False
@cached_property return self._is_gen_callable_cache
@property
def is_async_gen_callable(self) -> bool: def is_async_gen_callable(self) -> bool:
if self._is_async_gen_callable_cache is None:
if self.call is None: if self.call is None:
return False # pragma: no cover self._is_async_gen_callable_cache = False # pragma: no cover
if inspect.isasyncgenfunction( elif inspect.isasyncgenfunction(
_impartial(self.call) _impartial(self.call)
) or inspect.isasyncgenfunction(_unwrapped_call(self.call)): ) or inspect.isasyncgenfunction(_unwrapped_call(self.call)):
return True self._is_async_gen_callable_cache = True
if inspect.isclass(_unwrapped_call(self.call)): elif inspect.isclass(_unwrapped_call(self.call)):
return False self._is_async_gen_callable_cache = False
if self._is_async_gen_callable_cache is not None:
return self._is_async_gen_callable_cache
dunder_call = getattr(_impartial(self.call), "__call__", None) # noqa: B004 dunder_call = getattr(_impartial(self.call), "__call__", None) # noqa: B004
if dunder_call is None: if dunder_call is None:
return False # pragma: no cover self._is_async_gen_callable_cache = False # pragma: no cover
if inspect.isasyncgenfunction( elif inspect.isasyncgenfunction(
_impartial(dunder_call) _impartial(dunder_call)
) or inspect.isasyncgenfunction(_unwrapped_call(dunder_call)): ) or inspect.isasyncgenfunction(_unwrapped_call(dunder_call)):
return True self._is_async_gen_callable_cache = True
dunder_unwrapped_call = getattr(_unwrapped_call(self.call), "__call__", None) # noqa: B004
if self._is_async_gen_callable_cache is not None:
return self._is_async_gen_callable_cache
dunder_unwrapped_call = getattr( # noqa: B004
_unwrapped_call(self.call), "__call__", None
)
if dunder_unwrapped_call is None: if dunder_unwrapped_call is None:
return False # pragma: no cover self._is_async_gen_callable_cache = False # pragma: no cover
if inspect.isasyncgenfunction( elif inspect.isasyncgenfunction(
_impartial(dunder_unwrapped_call) _impartial(dunder_unwrapped_call)
) or inspect.isasyncgenfunction(_unwrapped_call(dunder_unwrapped_call)): ) or inspect.isasyncgenfunction(_unwrapped_call(dunder_unwrapped_call)):
return True self._is_async_gen_callable_cache = True
return False else:
self._is_async_gen_callable_cache = False
@cached_property return self._is_async_gen_callable_cache
@property
def is_coroutine_callable(self) -> bool: def is_coroutine_callable(self) -> bool:
if self._is_coroutine_callable_cache is None:
if self.call is None: if self.call is None:
return False # pragma: no cover self._is_coroutine_callable_cache = False # pragma: no cover
if inspect.isroutine(_impartial(self.call)) and iscoroutinefunction( elif inspect.isroutine(_impartial(self.call)) and iscoroutinefunction(
_impartial(self.call) _impartial(self.call)
): ):
return True self._is_coroutine_callable_cache = True
if inspect.isroutine(_unwrapped_call(self.call)) and iscoroutinefunction( elif inspect.isroutine(_unwrapped_call(self.call)) and iscoroutinefunction(
_unwrapped_call(self.call) _unwrapped_call(self.call)
): ):
return True self._is_coroutine_callable_cache = True
if inspect.isclass(_unwrapped_call(self.call)): elif inspect.isclass(_unwrapped_call(self.call)):
return False self._is_coroutine_callable_cache = False
if self._is_coroutine_callable_cache is not None:
return self._is_coroutine_callable_cache
dunder_call = getattr(_impartial(self.call), "__call__", None) # noqa: B004 dunder_call = getattr(_impartial(self.call), "__call__", None) # noqa: B004
if dunder_call is None: if dunder_call is None:
return False # pragma: no cover self._is_coroutine_callable_cache = False # pragma: no cover
if iscoroutinefunction(_impartial(dunder_call)) or iscoroutinefunction( elif iscoroutinefunction(_impartial(dunder_call)) or iscoroutinefunction(
_unwrapped_call(dunder_call) _unwrapped_call(dunder_call)
): ):
return True self._is_coroutine_callable_cache = True
dunder_unwrapped_call = getattr(_unwrapped_call(self.call), "__call__", None) # noqa: B004
if self._is_coroutine_callable_cache is not None:
return self._is_coroutine_callable_cache
dunder_unwrapped_call = getattr( # noqa: B004
_unwrapped_call(self.call), "__call__", None
)
if dunder_unwrapped_call is None: if dunder_unwrapped_call is None:
return False # pragma: no cover self._is_coroutine_callable_cache = False # pragma: no cover
if iscoroutinefunction( elif iscoroutinefunction(
_impartial(dunder_unwrapped_call) _impartial(dunder_unwrapped_call)
) or iscoroutinefunction(_unwrapped_call(dunder_unwrapped_call)): ) or iscoroutinefunction(_unwrapped_call(dunder_unwrapped_call)):
return True self._is_coroutine_callable_cache = True
return False else:
self._is_coroutine_callable_cache = False
@cached_property return self._is_coroutine_callable_cache
@property
def computed_scope(self) -> str | None: def computed_scope(self) -> str | None:
if self._computed_scope_cache is None:
if self.scope: if self.scope:
return self.scope self._computed_scope_cache = self.scope
if self.is_gen_callable or self.is_async_gen_callable: elif self.is_gen_callable or self.is_async_gen_callable:
return "request" self._computed_scope_cache = "request"
return None else:
self._computed_scope_cache = None
return self._computed_scope_cache

Loading…
Cancel
Save