Skip to content

Commit

Permalink
Define strict return value typing for Depends
Browse files Browse the repository at this point in the history
Depends can be used with a direct form of

    def endpoint(value = Depends(dependency)):
        pass

Currently, `value` becomes Any. This patch changes it to receive
a type matching what the dependency returns.
  • Loading branch information
JaniM committed Mar 7, 2024
1 parent eef1b7d commit e365b6e
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions fastapi/param_functions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Dict, List, Optional, Sequence, Union
from typing import Any, Callable, Dict, List, Optional, Sequence, TypeVar, TypeVar, Union

from fastapi import params
from fastapi._compat import Undefined
Expand Down Expand Up @@ -2217,9 +2217,12 @@ def File( # noqa: N802
)


DependencyResult = TypeVar("DependencyResult")


def Depends( # noqa: N802
dependency: Annotated[
Optional[Callable[..., Any]],
Optional[Callable[..., DependencyResult]],
Doc(
"""
A "dependable" callable (like a function).
Expand All @@ -2244,7 +2247,7 @@ def Depends( # noqa: N802
"""
),
] = True,
) -> Any:
) -> DependencyResult:
"""
Declare a FastAPI dependency.
Expand Down Expand Up @@ -2274,7 +2277,9 @@ async def read_items(commons: Annotated[dict, Depends(common_parameters)]):
return commons
```
"""
return params.Depends(dependency=dependency, use_cache=use_cache)
depends_any: Any = params.Depends(dependency=dependency, use_cache=use_cache)
depends: DependencyResult = depends_any
return depends


def Security( # noqa: N802
Expand Down

0 comments on commit e365b6e

Please sign in to comment.