From afa7461f287fb5709b1bad701587e4af9c3eaacd Mon Sep 17 00:00:00 2001 From: ProfiAnton Date: Mon, 13 Oct 2025 19:16:01 +0200 Subject: [PATCH 1/5] =?UTF-8?q?=E2=9C=A8=20Support=20Annotated=20dependenc?= =?UTF-8?q?ies=20in=20APIRouter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/routing.py | 29 ++++++++++++-- .../test_apirouter_annotated_dependencies.py | 38 +++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 tests/test_apirouter_annotated_dependencies.py diff --git a/fastapi/routing.py b/fastapi/routing.py index fe25d7dec..907511081 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -37,6 +37,7 @@ from fastapi.datastructures import Default, DefaultPlaceholder from fastapi.dependencies.models import Dependant from fastapi.dependencies.utils import ( _should_embed_body_fields, + analyze_param, get_body_field, get_dependant, get_flat_dependant, @@ -670,6 +671,22 @@ class APIRoute(routing.Route): return match, child_scope +def get_api_router_dep(dep: params.Depends | Any) -> params.Depends: + if isinstance(dep, params.Depends): + return dep + d = analyze_param( + param_name="APIRouter Dependency", + annotation=dep, + value=inspect.Signature.empty, + is_path_param=False, + ).depends + + assert d is not None, ( + "APIRouter dependency must be a Depends or be Annotated with Depends" + ) + return d + + class APIRouter(routing.Router): """ `APIRouter` class, used to group *path operations*, for example to structure @@ -716,11 +733,15 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends]], + Optional[Sequence[params.Depends | Any]], Doc( """ - A list of dependencies (using `Depends()`) to be applied to all the - *path operations* in this router. + A list of dependencies to be applied to all the *path operations* in + this router. + + Dependencies can be provided as `Depends(...)` instances, or using + `Annotated[..., Depends(...)]`. FastAPI will analyze and convert + these automatically when the router is included. Read more about it in the [FastAPI docs for Bigger Applications - Multiple Files](https://fastapi.tiangolo.com/tutorial/bigger-applications/#include-an-apirouter-with-a-custom-prefix-tags-responses-and-dependencies). @@ -927,7 +948,7 @@ class APIRouter(routing.Router): ) self.prefix = prefix self.tags: List[Union[str, Enum]] = tags or [] - self.dependencies = list(dependencies or []) + self.dependencies = [get_api_router_dep(dep) for dep in dependencies or []] self.deprecated = deprecated self.include_in_schema = include_in_schema self.responses = responses or {} diff --git a/tests/test_apirouter_annotated_dependencies.py b/tests/test_apirouter_annotated_dependencies.py new file mode 100644 index 000000000..bcf63e231 --- /dev/null +++ b/tests/test_apirouter_annotated_dependencies.py @@ -0,0 +1,38 @@ +from fastapi import APIRouter, Depends, FastAPI +from fastapi.testclient import TestClient +from typing_extensions import Annotated + + +def get_value() -> int: + return 1 + + +ValueDep = Annotated[int, Depends(get_value)] + + +router = APIRouter(dependencies=[ValueDep, Depends(lambda: "sdfgh")]) + + +@router.get("/") +def read_root(dep: ValueDep): + return {"dep": dep} + + +@router.get("/no_dep") +def no_dep(): + return {"status": 200} + + +app = FastAPI() +app.include_router(router) + + +def test_apirouter_annotated_dependencies(): + client = TestClient(app) + response = client.get("/") + assert response.status_code == 200 + assert response.json() == {"dep": 1} + + response = client.get("/no_dep") + assert response.status_code == 200 + assert response.json() == {"status": 200} From c9b8f6ff2d76e9d3d54510683abdd0d7a35ad7fa Mon Sep 17 00:00:00 2001 From: ProfiAnton Date: Thu, 16 Oct 2025 01:40:58 +0200 Subject: [PATCH 2/5] =?UTF-8?q?=E2=9C=A8=20Support=20Annotated=20dependenc?= =?UTF-8?q?ies=20in=20FastAPI=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/applications.py | 7 ++++--- fastapi/routing.py | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index 6db4b4e83..7379d92eb 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -333,7 +333,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends]], + Optional[Sequence[Depends | Any]], Doc( """ A list of global dependencies, they will be applied to each @@ -345,11 +345,12 @@ class FastAPI(Starlette): **Example** ```python + from typing import Annotated from fastapi import Depends, FastAPI from .dependencies import func_dep_1, func_dep_2 - - app = FastAPI(dependencies=[Depends(func_dep_1), Depends(func_dep_2)]) + annotated_dep=Annotated[str,lambda:"annotated"] + app = FastAPI(dependencies=[Depends(func_dep_1), Depends(func_dep_2),annotated_dep]) ``` """ ), diff --git a/fastapi/routing.py b/fastapi/routing.py index 907511081..47b9240f6 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -671,7 +671,7 @@ class APIRoute(routing.Route): return match, child_scope -def get_api_router_dep(dep: params.Depends | Any) -> params.Depends: +def get_depends_from_annotated(dep: params.Depends | Any) -> params.Depends: if isinstance(dep, params.Depends): return dep d = analyze_param( @@ -948,7 +948,9 @@ class APIRouter(routing.Router): ) self.prefix = prefix self.tags: List[Union[str, Enum]] = tags or [] - self.dependencies = [get_api_router_dep(dep) for dep in dependencies or []] + self.dependencies = [ + get_depends_from_annotated(dep) for dep in dependencies or [] + ] self.deprecated = deprecated self.include_in_schema = include_in_schema self.responses = responses or {} From 0253ac7273c2fc9c28b32bd8791942f24531b1ab Mon Sep 17 00:00:00 2001 From: ProfiAnton Date: Thu, 16 Oct 2025 02:57:36 +0200 Subject: [PATCH 3/5] =?UTF-8?q?=E2=9C=A8=20Support=20Annotated=20dependenc?= =?UTF-8?q?ies=20in=20APIRoute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/applications.py | 28 ++++----- fastapi/routing.py | 63 ++++++++++++------- .../test_apirouter_annotated_dependencies.py | 4 +- 3 files changed, 57 insertions(+), 38 deletions(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index 7379d92eb..38150eb28 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -333,7 +333,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends | Any]], + Optional[Sequence[Depends | routing.AnnotatedType]], Doc( """ A list of global dependencies, they will be applied to each @@ -1141,7 +1141,7 @@ class FastAPI(Starlette): response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, - dependencies: Optional[Sequence[Depends]] = None, + dependencies: Optional[Sequence[Depends | routing.AnnotatedType]] = None, summary: Optional[str] = None, description: Optional[str] = None, response_description: str = "Successful Response", @@ -1199,7 +1199,7 @@ class FastAPI(Starlette): response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, - dependencies: Optional[Sequence[Depends]] = None, + dependencies: Optional[Sequence[Depends | routing.AnnotatedType]] = None, summary: Optional[str] = None, description: Optional[str] = None, response_description: str = "Successful Response", @@ -1258,7 +1258,7 @@ class FastAPI(Starlette): endpoint: Callable[..., Any], name: Optional[str] = None, *, - dependencies: Optional[Sequence[Depends]] = None, + dependencies: Optional[Sequence[Depends | routing.AnnotatedType]] = None, ) -> None: self.router.add_api_websocket_route( path, @@ -1287,7 +1287,7 @@ class FastAPI(Starlette): ] = None, *, dependencies: Annotated[ - Optional[Sequence[Depends]], + Optional[Sequence[Depends | routing.AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be used for this @@ -1352,7 +1352,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends]], + Optional[Sequence[Depends | routing.AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to all the @@ -1611,7 +1611,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends]], + Optional[Sequence[Depends | routing.AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -1984,7 +1984,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends]], + Optional[Sequence[Depends | routing.AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -2362,7 +2362,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends]], + Optional[Sequence[Depends | routing.AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -2740,7 +2740,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends]], + Optional[Sequence[Depends | routing.AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -3113,7 +3113,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends]], + Optional[Sequence[Depends | routing.AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -3486,7 +3486,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends]], + Optional[Sequence[Depends | routing.AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -3859,7 +3859,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends]], + Optional[Sequence[Depends | routing.AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -4237,7 +4237,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends]], + Optional[Sequence[Depends | routing.AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the diff --git a/fastapi/routing.py b/fastapi/routing.py index 47b9240f6..5f1e2dcc0 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -17,6 +17,7 @@ from typing import ( List, Mapping, Optional, + Protocol, Sequence, Set, Tuple, @@ -464,6 +465,10 @@ def get_websocket_app( return app +class AnnotatedType(Protocol): + """this forces mypy to check if Any is handled correctly""" + + class APIWebSocketRoute(routing.WebSocketRoute): def __init__( self, @@ -471,13 +476,15 @@ class APIWebSocketRoute(routing.WebSocketRoute): endpoint: Callable[..., Any], *, name: Optional[str] = None, - dependencies: Optional[Sequence[params.Depends]] = None, + dependencies: Optional[Sequence[params.Depends | AnnotatedType]] = None, dependency_overrides_provider: Optional[Any] = None, ) -> None: self.path = path self.endpoint = endpoint self.name = get_name(endpoint) if name is None else name - self.dependencies = list(dependencies or []) + self.dependencies = [ + get_depends_from_annotated(dep) for dep in dependencies or [] + ] self.path_regex, self.path_format, self.param_convertors = compile_path(path) self.dependant = get_dependant(path=self.path_format, call=self.endpoint) for depends in self.dependencies[::-1]: @@ -513,7 +520,7 @@ class APIRoute(routing.Route): response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, - dependencies: Optional[Sequence[params.Depends]] = None, + dependencies: Optional[Sequence[params.Depends | AnnotatedType]] = None, summary: Optional[str] = None, description: Optional[str] = None, response_description: str = "Successful Response", @@ -606,7 +613,9 @@ class APIRoute(routing.Route): else: self.response_field = None # type: ignore self.secure_cloned_response_field = None - self.dependencies = list(dependencies or []) + self.dependencies = [ + get_depends_from_annotated(dep) for dep in dependencies or [] + ] self.description = description or inspect.cleandoc(self.endpoint.__doc__ or "") # if a "form feed" character (page break) is found in the description text, # truncate description text to the content preceding the first "form feed" @@ -671,7 +680,7 @@ class APIRoute(routing.Route): return match, child_scope -def get_depends_from_annotated(dep: params.Depends | Any) -> params.Depends: +def get_depends_from_annotated(dep: params.Depends | AnnotatedType) -> params.Depends: if isinstance(dep, params.Depends): return dep d = analyze_param( @@ -733,7 +742,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends | Any]], + Optional[Sequence[params.Depends | AnnotatedType]], Doc( """ A list of dependencies to be applied to all the *path operations* in @@ -987,7 +996,7 @@ class APIRouter(routing.Router): response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, - dependencies: Optional[Sequence[params.Depends]] = None, + dependencies: Optional[Sequence[params.Depends | AnnotatedType]] = None, summary: Optional[str] = None, description: Optional[str] = None, response_description: str = "Successful Response", @@ -1024,7 +1033,9 @@ class APIRouter(routing.Router): current_tags.extend(tags) current_dependencies = self.dependencies.copy() if dependencies: - current_dependencies.extend(dependencies) + current_dependencies.extend( + [get_depends_from_annotated(dep) for dep in dependencies] + ) current_callbacks = self.callbacks.copy() if callbacks: current_callbacks.extend(callbacks) @@ -1068,7 +1079,7 @@ class APIRouter(routing.Router): response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, - dependencies: Optional[Sequence[params.Depends]] = None, + dependencies: Optional[Sequence[params.Depends | AnnotatedType]] = None, summary: Optional[str] = None, description: Optional[str] = None, response_description: str = "Successful Response", @@ -1129,11 +1140,13 @@ class APIRouter(routing.Router): endpoint: Callable[..., Any], name: Optional[str] = None, *, - dependencies: Optional[Sequence[params.Depends]] = None, + dependencies: Optional[Sequence[params.Depends | AnnotatedType]] = None, ) -> None: current_dependencies = self.dependencies.copy() if dependencies: - current_dependencies.extend(dependencies) + current_dependencies.extend( + [get_depends_from_annotated(dep) for dep in dependencies] + ) route = APIWebSocketRoute( self.prefix + path, @@ -1164,7 +1177,7 @@ class APIRouter(routing.Router): ] = None, *, dependencies: Annotated[ - Optional[Sequence[params.Depends]], + Optional[Sequence[params.Depends | AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be used for this @@ -1240,7 +1253,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends]], + Optional[Sequence[params.Depends | AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to all the @@ -1386,7 +1399,9 @@ class APIRouter(routing.Router): current_tags.extend(route.tags) current_dependencies: List[params.Depends] = [] if dependencies: - current_dependencies.extend(dependencies) + current_dependencies.extend( + [get_depends_from_annotated(dep) for dep in dependencies] + ) if route.dependencies: current_dependencies.extend(route.dependencies) current_callbacks = [] @@ -1442,7 +1457,9 @@ class APIRouter(routing.Router): elif isinstance(route, APIWebSocketRoute): current_dependencies = [] if dependencies: - current_dependencies.extend(dependencies) + current_dependencies.extend( + [get_depends_from_annotated(dep) for dep in dependencies] + ) if route.dependencies: current_dependencies.extend(route.dependencies) self.add_api_websocket_route( @@ -1538,7 +1555,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends]], + Optional[Sequence[params.Depends | AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -1915,7 +1932,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends]], + Optional[Sequence[params.Depends | AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -2297,7 +2314,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends]], + Optional[Sequence[params.Depends | AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -2679,7 +2696,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends]], + Optional[Sequence[params.Depends | AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -3056,7 +3073,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends]], + Optional[Sequence[params.Depends | AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -3433,7 +3450,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends]], + Optional[Sequence[params.Depends | AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -3815,7 +3832,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends]], + Optional[Sequence[params.Depends | AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -4197,7 +4214,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends]], + Optional[Sequence[params.Depends | AnnotatedType]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the diff --git a/tests/test_apirouter_annotated_dependencies.py b/tests/test_apirouter_annotated_dependencies.py index bcf63e231..5fbe0a722 100644 --- a/tests/test_apirouter_annotated_dependencies.py +++ b/tests/test_apirouter_annotated_dependencies.py @@ -10,7 +10,7 @@ def get_value() -> int: ValueDep = Annotated[int, Depends(get_value)] -router = APIRouter(dependencies=[ValueDep, Depends(lambda: "sdfgh")]) +router = APIRouter(dependencies=[Depends(lambda: "sdfgh"), ValueDep]) @router.get("/") @@ -26,6 +26,8 @@ def no_dep(): app = FastAPI() app.include_router(router) +router.post("/", dependencies=[Depends(lambda: None)]) + def test_apirouter_annotated_dependencies(): client = TestClient(app) From bef402a41ef283b9eba2e8d92246f863107b3af6 Mon Sep 17 00:00:00 2001 From: ProfiAnton Date: Thu, 16 Oct 2025 05:06:21 +0200 Subject: [PATCH 4/5] use unions instead --- fastapi/applications.py | 28 ++++++++++++++-------------- fastapi/routing.py | 36 +++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index 38150eb28..10d7b47e7 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -333,7 +333,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends | routing.AnnotatedType]], + Optional[Sequence[Union[Depends, routing.AnnotatedType]]], Doc( """ A list of global dependencies, they will be applied to each @@ -1141,7 +1141,7 @@ class FastAPI(Starlette): response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, - dependencies: Optional[Sequence[Depends | routing.AnnotatedType]] = None, + dependencies: Optional[Sequence[Union[Depends, routing.AnnotatedType]]] = None, summary: Optional[str] = None, description: Optional[str] = None, response_description: str = "Successful Response", @@ -1199,7 +1199,7 @@ class FastAPI(Starlette): response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, - dependencies: Optional[Sequence[Depends | routing.AnnotatedType]] = None, + dependencies: Optional[Sequence[Union[Depends, routing.AnnotatedType]]] = None, summary: Optional[str] = None, description: Optional[str] = None, response_description: str = "Successful Response", @@ -1258,7 +1258,7 @@ class FastAPI(Starlette): endpoint: Callable[..., Any], name: Optional[str] = None, *, - dependencies: Optional[Sequence[Depends | routing.AnnotatedType]] = None, + dependencies: Optional[Sequence[Union[Depends, routing.AnnotatedType]]] = None, ) -> None: self.router.add_api_websocket_route( path, @@ -1287,7 +1287,7 @@ class FastAPI(Starlette): ] = None, *, dependencies: Annotated[ - Optional[Sequence[Depends | routing.AnnotatedType]], + Optional[Sequence[Union[Depends, routing.AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be used for this @@ -1352,7 +1352,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends | routing.AnnotatedType]], + Optional[Sequence[Union[Depends, routing.AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to all the @@ -1611,7 +1611,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends | routing.AnnotatedType]], + Optional[Sequence[Union[Depends, routing.AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -1984,7 +1984,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends | routing.AnnotatedType]], + Optional[Sequence[Union[Depends, routing.AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -2362,7 +2362,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends | routing.AnnotatedType]], + Optional[Sequence[Union[Depends, routing.AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -2740,7 +2740,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends | routing.AnnotatedType]], + Optional[Sequence[Union[Depends, routing.AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -3113,7 +3113,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends | routing.AnnotatedType]], + Optional[Sequence[Union[Depends, routing.AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -3486,7 +3486,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends | routing.AnnotatedType]], + Optional[Sequence[Union[Depends, routing.AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -3859,7 +3859,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends | routing.AnnotatedType]], + Optional[Sequence[Union[Depends, routing.AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -4237,7 +4237,7 @@ class FastAPI(Starlette): ), ] = None, dependencies: Annotated[ - Optional[Sequence[Depends | routing.AnnotatedType]], + Optional[Sequence[Union[Depends, routing.AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the diff --git a/fastapi/routing.py b/fastapi/routing.py index 5f1e2dcc0..bf04e0b4d 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -476,7 +476,7 @@ class APIWebSocketRoute(routing.WebSocketRoute): endpoint: Callable[..., Any], *, name: Optional[str] = None, - dependencies: Optional[Sequence[params.Depends | AnnotatedType]] = None, + dependencies: Optional[Sequence[Union[params.Depends, AnnotatedType]]] = None, dependency_overrides_provider: Optional[Any] = None, ) -> None: self.path = path @@ -520,7 +520,7 @@ class APIRoute(routing.Route): response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, - dependencies: Optional[Sequence[params.Depends | AnnotatedType]] = None, + dependencies: Optional[Sequence[Union[params.Depends, AnnotatedType]]] = None, summary: Optional[str] = None, description: Optional[str] = None, response_description: str = "Successful Response", @@ -680,7 +680,9 @@ class APIRoute(routing.Route): return match, child_scope -def get_depends_from_annotated(dep: params.Depends | AnnotatedType) -> params.Depends: +def get_depends_from_annotated( + dep: Union[params.Depends, AnnotatedType], +) -> params.Depends: if isinstance(dep, params.Depends): return dep d = analyze_param( @@ -742,7 +744,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends | AnnotatedType]], + Optional[Sequence[Union[params.Depends, AnnotatedType]]], Doc( """ A list of dependencies to be applied to all the *path operations* in @@ -996,7 +998,7 @@ class APIRouter(routing.Router): response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, - dependencies: Optional[Sequence[params.Depends | AnnotatedType]] = None, + dependencies: Optional[Sequence[Union[params.Depends, AnnotatedType]]] = None, summary: Optional[str] = None, description: Optional[str] = None, response_description: str = "Successful Response", @@ -1079,7 +1081,7 @@ class APIRouter(routing.Router): response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, - dependencies: Optional[Sequence[params.Depends | AnnotatedType]] = None, + dependencies: Optional[Sequence[Union[params.Depends, AnnotatedType]]] = None, summary: Optional[str] = None, description: Optional[str] = None, response_description: str = "Successful Response", @@ -1140,7 +1142,7 @@ class APIRouter(routing.Router): endpoint: Callable[..., Any], name: Optional[str] = None, *, - dependencies: Optional[Sequence[params.Depends | AnnotatedType]] = None, + dependencies: Optional[Sequence[Union[params.Depends, AnnotatedType]]] = None, ) -> None: current_dependencies = self.dependencies.copy() if dependencies: @@ -1177,7 +1179,7 @@ class APIRouter(routing.Router): ] = None, *, dependencies: Annotated[ - Optional[Sequence[params.Depends | AnnotatedType]], + Optional[Sequence[Union[params.Depends, AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be used for this @@ -1253,7 +1255,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends | AnnotatedType]], + Optional[Sequence[Union[params.Depends, AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to all the @@ -1555,7 +1557,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends | AnnotatedType]], + Optional[Sequence[Union[params.Depends, AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -1932,7 +1934,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends | AnnotatedType]], + Optional[Sequence[Union[params.Depends, AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -2314,7 +2316,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends | AnnotatedType]], + Optional[Sequence[Union[params.Depends, AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -2696,7 +2698,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends | AnnotatedType]], + Optional[Sequence[Union[params.Depends, AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -3073,7 +3075,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends | AnnotatedType]], + Optional[Sequence[Union[params.Depends, AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -3450,7 +3452,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends | AnnotatedType]], + Optional[Sequence[Union[params.Depends, AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -3832,7 +3834,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends | AnnotatedType]], + Optional[Sequence[Union[params.Depends, AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the @@ -4214,7 +4216,7 @@ class APIRouter(routing.Router): ), ] = None, dependencies: Annotated[ - Optional[Sequence[params.Depends | AnnotatedType]], + Optional[Sequence[Union[params.Depends, AnnotatedType]]], Doc( """ A list of dependencies (using `Depends()`) to be applied to the From 3dcd96e9202b6f35aa718b9b6c809044218266bc Mon Sep 17 00:00:00 2001 From: ProfiAnton Date: Tue, 21 Oct 2025 19:52:50 +0200 Subject: [PATCH 5/5] fix example --- fastapi/applications.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index 10d7b47e7..1b6f3ec57 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -349,7 +349,7 @@ class FastAPI(Starlette): from fastapi import Depends, FastAPI from .dependencies import func_dep_1, func_dep_2 - annotated_dep=Annotated[str,lambda:"annotated"] + annotated_dep=Annotated[str,Depends(lambda:"annotated")] app = FastAPI(dependencies=[Depends(func_dep_1), Depends(func_dep_2),annotated_dep]) ``` """