Browse Source

Added TypeVar to make Depends aware of dependency methods return type

pull/14335/head
Ewen Lorimer 8 months ago
parent
commit
ba29c2a530
No known key found for this signature in database GPG Key ID: 59A3C5A26F9BC24E
  1. 31
      fastapi/param_functions.py

31
fastapi/param_functions.py

@ -1,4 +1,16 @@
from typing import Any, Callable, Dict, List, Optional, Sequence, Union from typing import (
Any,
AsyncGenerator,
Awaitable,
Callable,
Dict,
Generator,
List,
Optional,
Sequence,
TypeVar,
Union,
)
from annotated_doc import Doc from annotated_doc import Doc
from fastapi import params from fastapi import params
@ -7,6 +19,7 @@ from fastapi.openapi.models import Example
from typing_extensions import Annotated, Literal, deprecated from typing_extensions import Annotated, Literal, deprecated
_Unset: Any = Undefined _Unset: Any = Undefined
_RT = TypeVar("_RT")
def Path( # noqa: N802 def Path( # noqa: N802
@ -2220,7 +2233,17 @@ def File( # noqa: N802
def Depends( # noqa: N802 def Depends( # noqa: N802
dependency: Annotated[ dependency: Annotated[
Optional[Callable[..., Any]], Optional[
Callable[
...,
Union[
AsyncGenerator[_RT, Any],
Generator[_RT, Any, None],
Awaitable[_RT],
_RT,
],
]
],
Doc( Doc(
""" """
A "dependable" callable (like a function). A "dependable" callable (like a function).
@ -2265,7 +2288,7 @@ def Depends( # noqa: N802
""" """
), ),
] = None, ] = None,
) -> Any: ) -> _RT:
""" """
Declare a FastAPI dependency. Declare a FastAPI dependency.
@ -2295,7 +2318,7 @@ def Depends( # noqa: N802
return commons return commons
``` ```
""" """
return params.Depends(dependency=dependency, use_cache=use_cache, scope=scope) return params.Depends(dependency=dependency, use_cache=use_cache, scope=scope) # type: ignore[return-value]
def Security( # noqa: N802 def Security( # noqa: N802

Loading…
Cancel
Save