Browse Source

Add scope to Depends parameters

pull/14262/head
Sebastián Ramírez 9 months ago
parent
commit
33a2edbf10
  1. 24
      fastapi/param_functions.py
  2. 3
      fastapi/params.py

24
fastapi/param_functions.py

@ -4,7 +4,7 @@ from annotated_doc import Doc
from fastapi import params
from fastapi._compat import Undefined
from fastapi.openapi.models import Example
from typing_extensions import Annotated, deprecated
from typing_extensions import Annotated, Literal, deprecated
_Unset: Any = Undefined
@ -2245,6 +2245,26 @@ def Depends( # noqa: N802
"""
),
] = True,
scope: Annotated[
Literal["function", "request"],
Doc(
"""
Mainly for dependencies with `yield`, define when the dependency function
should start (the code before `yield`) and when it should end (the code
after `yield`).
* `"function"`: start the dependency before the *path operation function*
that handles the request, end the dependency after the *path operation
function* ends, but **before** the response is sent back to the client.
So, the dependency function will be executed *around* the *path operation
**function***.
* `"request"`: start the dependency before the *path operation function*
that handles the request (similar to when using `"function"`), but end
**after** the response is sent back to the client. So, the dependency
function will be executed *around* the **request** and response cycle.
"""
),
] = "request",
) -> Any:
"""
Declare a FastAPI dependency.
@ -2275,7 +2295,7 @@ def Depends( # noqa: N802
return commons
```
"""
return params.Depends(dependency=dependency, use_cache=use_cache)
return params.Depends(dependency=dependency, use_cache=use_cache, scope=scope)
def Security( # noqa: N802

3
fastapi/params.py

@ -5,7 +5,7 @@ from typing import Any, Callable, Dict, List, Optional, Sequence, Union
from fastapi.openapi.models import Example
from pydantic.fields import FieldInfo
from typing_extensions import Annotated, deprecated
from typing_extensions import Annotated, Literal, deprecated
from ._compat import (
PYDANTIC_V2,
@ -766,6 +766,7 @@ class File(Form): # type: ignore[misc]
class Depends:
dependency: Optional[Callable[..., Any]] = None
use_cache: bool = True
scope: Literal["function", "request"] = "request"
@dataclass

Loading…
Cancel
Save