81
81
82
82
ResponseEventT = TypeVar ("ResponseEventT" , bound = BaseProxyEvent )
83
83
ResponseT = TypeVar ("ResponseT" )
84
+ P = TypeVar ("P" )
85
+ T = TypeVar ("T" )
86
+ SimpleFunction = Callable [P , T ]
87
+ DecoratorFunction = Callable [[SimpleFunction ], SimpleFunction ]
84
88
85
89
if TYPE_CHECKING :
86
90
from aws_lambda_powertools .event_handler .openapi .compat import (
@@ -951,7 +955,7 @@ def route(
951
955
security : Optional [List [Dict [str , List [str ]]]] = None ,
952
956
openapi_extensions : Optional [Dict [str , Any ]] = None ,
953
957
middlewares : Optional [List [Callable [..., Any ]]] = None ,
954
- ):
958
+ ) -> DecoratorFunction :
955
959
raise NotImplementedError ()
956
960
957
961
def use (self , middlewares : List [Callable [..., Response ]]) -> None :
@@ -1011,7 +1015,7 @@ def get(
1011
1015
security : Optional [List [Dict [str , List [str ]]]] = None ,
1012
1016
openapi_extensions : Optional [Dict [str , Any ]] = None ,
1013
1017
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1014
- ):
1018
+ ) -> DecoratorFunction :
1015
1019
"""Get route decorator with GET `method`
1016
1020
1017
1021
Examples
@@ -1068,7 +1072,7 @@ def post(
1068
1072
security : Optional [List [Dict [str , List [str ]]]] = None ,
1069
1073
openapi_extensions : Optional [Dict [str , Any ]] = None ,
1070
1074
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1071
- ):
1075
+ ) -> DecoratorFunction :
1072
1076
"""Post route decorator with POST `method`
1073
1077
1074
1078
Examples
@@ -1126,7 +1130,7 @@ def put(
1126
1130
security : Optional [List [Dict [str , List [str ]]]] = None ,
1127
1131
openapi_extensions : Optional [Dict [str , Any ]] = None ,
1128
1132
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1129
- ):
1133
+ ) -> DecoratorFunction :
1130
1134
"""Put route decorator with PUT `method`
1131
1135
1132
1136
Examples
@@ -1184,7 +1188,7 @@ def delete(
1184
1188
security : Optional [List [Dict [str , List [str ]]]] = None ,
1185
1189
openapi_extensions : Optional [Dict [str , Any ]] = None ,
1186
1190
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1187
- ):
1191
+ ) -> DecoratorFunction :
1188
1192
"""Delete route decorator with DELETE `method`
1189
1193
1190
1194
Examples
@@ -1241,7 +1245,7 @@ def patch(
1241
1245
security : Optional [List [Dict [str , List [str ]]]] = None ,
1242
1246
openapi_extensions : Optional [Dict [str , Any ]] = None ,
1243
1247
middlewares : Optional [List [Callable ]] = None ,
1244
- ):
1248
+ ) -> DecoratorFunction :
1245
1249
"""Patch route decorator with PATCH `method`
1246
1250
1247
1251
Examples
@@ -1301,7 +1305,7 @@ def head(
1301
1305
security : Optional [List [Dict [str , List [str ]]]] = None ,
1302
1306
openapi_extensions : Optional [Dict [str , Any ]] = None ,
1303
1307
middlewares : Optional [List [Callable ]] = None ,
1304
- ):
1308
+ ) -> DecoratorFunction :
1305
1309
"""Head route decorator with HEAD `method`
1306
1310
1307
1311
Examples
@@ -1528,7 +1532,7 @@ def __init__(
1528
1532
self ._dynamic_routes : List [Route ] = []
1529
1533
self ._static_routes : List [Route ] = []
1530
1534
self ._route_keys : List [str ] = []
1531
- self ._exception_handlers : Dict [Type , Callable ] = {}
1535
+ self ._exception_handlers : Dict [Type , DecoratorFunction ] = {}
1532
1536
self ._cors = cors
1533
1537
self ._cors_enabled : bool = cors is not None
1534
1538
self ._cors_methods : Set [str ] = {"OPTIONS" }
@@ -1988,7 +1992,7 @@ def route(
1988
1992
security : Optional [List [Dict [str , List [str ]]]] = None ,
1989
1993
openapi_extensions : Optional [Dict [str , Any ]] = None ,
1990
1994
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1991
- ):
1995
+ ) -> DecoratorFunction :
1992
1996
"""Route decorator includes parameter `method`"""
1993
1997
1994
1998
def register_resolver (func : Callable ):
@@ -2345,7 +2349,7 @@ def not_found(self, func: Optional[Callable] = None):
2345
2349
return self .exception_handler (NotFoundError )
2346
2350
return self .exception_handler (NotFoundError )(func )
2347
2351
2348
- def exception_handler (self , exc_class : Union [Type [Exception ], List [Type [Exception ]]]):
2352
+ def exception_handler (self , exc_class : Union [Type [Exception ], List [Type [Exception ]]]) -> DecoratorFunction :
2349
2353
def register_exception_handler (func : Callable ):
2350
2354
if isinstance (exc_class , list ): # pragma: no cover
2351
2355
for exp in exc_class :
@@ -2356,7 +2360,7 @@ def register_exception_handler(func: Callable):
2356
2360
2357
2361
return register_exception_handler
2358
2362
2359
- def _lookup_exception_handler (self , exp_type : Type ) -> Optional [Callable ]:
2363
+ def _lookup_exception_handler (self , exp_type : Type ) -> Optional [DecoratorFunction ]:
2360
2364
# Use "Method Resolution Order" to allow for matching against a base class
2361
2365
# of an exception
2362
2366
for cls in exp_type .__mro__ :
@@ -2506,12 +2510,12 @@ def _get_fields_from_routes(routes: Sequence[Route]) -> List["ModelField"]:
2506
2510
class Router (BaseRouter ):
2507
2511
"""Router helper class to allow splitting ApiGatewayResolver into multiple files"""
2508
2512
2509
- def __init__ (self ):
2513
+ def __init__ (self ) -> "Router" :
2510
2514
self ._routes : Dict [tuple , Callable ] = {}
2511
2515
self ._routes_with_middleware : Dict [tuple , List [Callable ]] = {}
2512
2516
self .api_resolver : Optional [BaseRouter ] = None
2513
2517
self .context = {} # early init as customers might add context before event resolution
2514
- self ._exception_handlers : Dict [Type , Callable ] = {}
2518
+ self ._exception_handlers : Dict [Type , DecoratorFunction ] = {}
2515
2519
2516
2520
def route (
2517
2521
self ,
@@ -2530,7 +2534,7 @@ def route(
2530
2534
security : Optional [List [Dict [str , List [str ]]]] = None ,
2531
2535
openapi_extensions : Optional [Dict [str , Any ]] = None ,
2532
2536
middlewares : Optional [List [Callable [..., Any ]]] = None ,
2533
- ):
2537
+ ) -> DecoratorFunction :
2534
2538
def register_route (func : Callable ):
2535
2539
# All dict keys needs to be hashable. So we'll need to do some conversions:
2536
2540
methods = (method ,) if isinstance (method , str ) else tuple (method )
@@ -2636,7 +2640,7 @@ def route(
2636
2640
security : Optional [List [Dict [str , List [str ]]]] = None ,
2637
2641
openapi_extensions : Optional [Dict [str , Any ]] = None ,
2638
2642
middlewares : Optional [List [Callable [..., Any ]]] = None ,
2639
- ):
2643
+ ) -> DecoratorFunction :
2640
2644
# NOTE: see #1552 for more context.
2641
2645
return super ().route (
2642
2646
rule .rstrip ("/" ),
0 commit comments