diff --git a/fastapi/applications.py b/fastapi/applications.py index 6d427cdc2..2c73c5663 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -1469,7 +1469,7 @@ class FastAPI(Starlette): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -1788,6 +1788,7 @@ class FastAPI(Starlette): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP GET operation. @@ -1806,6 +1807,7 @@ class FastAPI(Starlette): """ return self.router.get( path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -1828,6 +1830,7 @@ class FastAPI(Starlette): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def put( @@ -1842,7 +1845,7 @@ class FastAPI(Starlette): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -2161,6 +2164,7 @@ class FastAPI(Starlette): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP PUT operation. @@ -2184,6 +2188,7 @@ class FastAPI(Starlette): """ return self.router.put( path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -2206,6 +2211,7 @@ class FastAPI(Starlette): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def post( @@ -2220,7 +2226,7 @@ class FastAPI(Starlette): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -2539,6 +2545,7 @@ class FastAPI(Starlette): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP POST operation. @@ -2562,6 +2569,7 @@ class FastAPI(Starlette): """ return self.router.post( path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -2584,6 +2592,7 @@ class FastAPI(Starlette): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def delete( @@ -2598,7 +2607,7 @@ class FastAPI(Starlette): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -2917,6 +2926,7 @@ class FastAPI(Starlette): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP DELETE operation. @@ -2935,6 +2945,7 @@ class FastAPI(Starlette): """ return self.router.delete( path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -2957,6 +2968,7 @@ class FastAPI(Starlette): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def options( @@ -2971,7 +2983,7 @@ class FastAPI(Starlette): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -3290,6 +3302,7 @@ class FastAPI(Starlette): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP OPTIONS operation. @@ -3308,6 +3321,7 @@ class FastAPI(Starlette): """ return self.router.options( path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -3330,6 +3344,7 @@ class FastAPI(Starlette): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def head( @@ -3344,7 +3359,7 @@ class FastAPI(Starlette): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -3663,6 +3678,7 @@ class FastAPI(Starlette): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP HEAD operation. @@ -3681,6 +3697,7 @@ class FastAPI(Starlette): """ return self.router.head( path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -3703,6 +3720,7 @@ class FastAPI(Starlette): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def patch( @@ -3717,7 +3735,7 @@ class FastAPI(Starlette): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -4036,6 +4054,7 @@ class FastAPI(Starlette): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP PATCH operation. @@ -4081,6 +4100,8 @@ class FastAPI(Starlette): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + # *args, + **kwargs, ) def trace( @@ -4095,7 +4116,7 @@ class FastAPI(Starlette): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -4414,6 +4435,7 @@ class FastAPI(Starlette): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP TRACE operation. @@ -4432,6 +4454,7 @@ class FastAPI(Starlette): """ return self.router.trace( path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -4454,6 +4477,7 @@ class FastAPI(Starlette): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def websocket_route( diff --git a/fastapi/routing.py b/fastapi/routing.py index 457481e32..08362bdcb 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -430,7 +430,7 @@ class APIRoute(routing.Route): self, path: str, endpoint: Callable[..., Any], - *, + *args: Any, response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, @@ -459,6 +459,7 @@ class APIRoute(routing.Route): generate_unique_id_function: Union[ Callable[["APIRoute"], str], DefaultPlaceholder ] = Default(generate_unique_id), + **kwargs: Any, ) -> None: self.path = path self.endpoint = endpoint @@ -621,7 +622,7 @@ class APIRouter(routing.Router): def __init__( self, - *, + *args: Any, prefix: Annotated[str, Doc("An optional path prefix for the router.")] = "", tags: Annotated[ Optional[List[Union[str, Enum]]], @@ -833,6 +834,7 @@ class APIRouter(routing.Router): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> None: super().__init__( routes=routes, @@ -882,7 +884,7 @@ class APIRouter(routing.Router): self, path: str, endpoint: Callable[..., Any], - *, + *args: Any, response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, @@ -911,6 +913,7 @@ class APIRouter(routing.Router): generate_unique_id_function: Union[ Callable[[APIRoute], str], DefaultPlaceholder ] = Default(generate_unique_id), + **kwargs: Any, ) -> None: route_class = route_class_override or self.route_class responses = responses or {} @@ -932,7 +935,8 @@ class APIRouter(routing.Router): ) route = route_class( self.prefix + path, - endpoint=endpoint, + endpoint, + *args, response_model=response_model, status_code=status_code, tags=current_tags, @@ -957,13 +961,14 @@ class APIRouter(routing.Router): callbacks=current_callbacks, openapi_extra=openapi_extra, generate_unique_id_function=current_generate_unique_id, + **kwargs, ) self.routes.append(route) def api_route( self, path: str, - *, + *args: Any, response_model: Any = Default(None), status_code: Optional[int] = None, tags: Optional[List[Union[str, Enum]]] = None, @@ -989,11 +994,13 @@ class APIRouter(routing.Router): generate_unique_id_function: Callable[[APIRoute], str] = Default( generate_unique_id ), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: def decorator(func: DecoratedCallable) -> DecoratedCallable: self.add_api_route( path, func, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -1017,6 +1024,7 @@ class APIRouter(routing.Router): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) return func @@ -1122,7 +1130,7 @@ class APIRouter(routing.Router): def include_router( self, router: Annotated["APIRouter", Doc("The `APIRouter` to include.")], - *, + *args: Any, prefix: Annotated[str, Doc("An optional path prefix for the router.")] = "", tags: Annotated[ Optional[List[Union[str, Enum]]], @@ -1230,6 +1238,7 @@ class APIRouter(routing.Router): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> None: """ Include another `APIRouter` in the same current `APIRouter`. @@ -1302,6 +1311,7 @@ class APIRouter(routing.Router): self.add_api_route( prefix + route.path, route.endpoint, + *args, response_model=route.response_model, status_code=route.status_code, tags=current_tags, @@ -1328,6 +1338,7 @@ class APIRouter(routing.Router): callbacks=current_callbacks, openapi_extra=route.openapi_extra, generate_unique_id_function=current_generate_unique_id, + **kwargs, ) elif isinstance(route, routing.Route): methods = list(route.methods or []) @@ -1375,7 +1386,7 @@ class APIRouter(routing.Router): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -1694,6 +1705,7 @@ class APIRouter(routing.Router): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP GET operation. @@ -1714,7 +1726,8 @@ class APIRouter(routing.Router): ``` """ return self.api_route( - path=path, + path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -1738,6 +1751,7 @@ class APIRouter(routing.Router): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def put( @@ -1752,7 +1766,7 @@ class APIRouter(routing.Router): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -2071,6 +2085,7 @@ class APIRouter(routing.Router): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP PUT operation. @@ -2096,7 +2111,8 @@ class APIRouter(routing.Router): ``` """ return self.api_route( - path=path, + path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -2120,6 +2136,7 @@ class APIRouter(routing.Router): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def post( @@ -2134,7 +2151,7 @@ class APIRouter(routing.Router): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -2453,6 +2470,7 @@ class APIRouter(routing.Router): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP POST operation. @@ -2478,7 +2496,8 @@ class APIRouter(routing.Router): ``` """ return self.api_route( - path=path, + path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -2502,6 +2521,7 @@ class APIRouter(routing.Router): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def delete( @@ -2516,7 +2536,7 @@ class APIRouter(routing.Router): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -2835,6 +2855,7 @@ class APIRouter(routing.Router): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP DELETE operation. @@ -2855,7 +2876,8 @@ class APIRouter(routing.Router): ``` """ return self.api_route( - path=path, + path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -2879,6 +2901,7 @@ class APIRouter(routing.Router): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def options( @@ -2893,7 +2916,7 @@ class APIRouter(routing.Router): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -3212,6 +3235,7 @@ class APIRouter(routing.Router): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP OPTIONS operation. @@ -3232,7 +3256,8 @@ class APIRouter(routing.Router): ``` """ return self.api_route( - path=path, + path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -3256,6 +3281,7 @@ class APIRouter(routing.Router): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def head( @@ -3270,7 +3296,7 @@ class APIRouter(routing.Router): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -3589,6 +3615,7 @@ class APIRouter(routing.Router): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP HEAD operation. @@ -3614,7 +3641,8 @@ class APIRouter(routing.Router): ``` """ return self.api_route( - path=path, + path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -3638,6 +3666,7 @@ class APIRouter(routing.Router): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def patch( @@ -3652,7 +3681,7 @@ class APIRouter(routing.Router): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -3971,6 +4000,7 @@ class APIRouter(routing.Router): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP PATCH operation. @@ -3996,7 +4026,8 @@ class APIRouter(routing.Router): ``` """ return self.api_route( - path=path, + path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -4020,6 +4051,7 @@ class APIRouter(routing.Router): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) def trace( @@ -4034,7 +4066,7 @@ class APIRouter(routing.Router): """ ), ], - *, + *args: Any, response_model: Annotated[ Any, Doc( @@ -4353,6 +4385,7 @@ class APIRouter(routing.Router): """ ), ] = Default(generate_unique_id), + **kwargs: Any, ) -> Callable[[DecoratedCallable], DecoratedCallable]: """ Add a *path operation* using an HTTP TRACE operation. @@ -4378,7 +4411,8 @@ class APIRouter(routing.Router): ``` """ return self.api_route( - path=path, + path, + *args, response_model=response_model, status_code=status_code, tags=tags, @@ -4402,6 +4436,7 @@ class APIRouter(routing.Router): callbacks=callbacks, openapi_extra=openapi_extra, generate_unique_id_function=generate_unique_id_function, + **kwargs, ) @deprecated( diff --git a/tests/test_operations_signatures.py b/tests/test_operations_signatures.py index 1a749651d..832f9a0c9 100644 --- a/tests/test_operations_signatures.py +++ b/tests/test_operations_signatures.py @@ -1,22 +1,25 @@ import inspect +import pytest from fastapi import APIRouter, FastAPI -method_names = ["get", "put", "post", "delete", "options", "head", "patch", "trace"] - -def test_signatures_consistency(): - base_sig = inspect.signature(APIRouter.get) - for method_name in method_names: - router_method = getattr(APIRouter, method_name) - app_method = getattr(FastAPI, method_name) - router_sig = inspect.signature(router_method) - app_sig = inspect.signature(app_method) - param: inspect.Parameter - for key, param in base_sig.parameters.items(): - router_param: inspect.Parameter = router_sig.parameters[key] - app_param: inspect.Parameter = app_sig.parameters[key] - assert param.annotation == router_param.annotation - assert param.annotation == app_param.annotation - assert param.default == router_param.default - assert param.default == app_param.default +@pytest.mark.parametrize( + "method_name", ["get", "put", "post", "delete", "options", "head", "patch", "trace"] +) +@pytest.mark.parametrize( + "sig_param", inspect.signature(APIRouter.get).parameters.items() +) +def test_signatures_consistency(method_name, sig_param): + router_method = getattr(APIRouter, method_name) + app_method = getattr(FastAPI, method_name) + router_sig = inspect.signature(router_method) + app_sig = inspect.signature(app_method) + param: inspect.Parameter + key, param = sig_param + router_param: inspect.Parameter = router_sig.parameters[key] + app_param: inspect.Parameter = app_sig.parameters[key] + assert param.annotation == router_param.annotation + assert param.annotation == app_param.annotation + assert param.default == router_param.default + assert param.default == app_param.default