bozhilov

Infer the type of a function in a decorator

Sep 1st, 2023
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. from typing import Any, Callable, TypeVar, cast
  2.  
  3. FuncT = TypeVar('FuncT', bound=Callable[..., Any])
  4.  
  5.  
  6. def handle_exception(func: FuncT) -> FuncT:
  7.     def wrapper(*arg, **kwargs):  # type: ignore
  8.         request = arg[0]
  9.         try:
  10.             response = func(*arg, **kwargs)
  11.             return response
  12.         except Exception as e:
  13.             e.handle_body = request.body  # type: ignore
  14.             raise e
  15.  
  16.     return cast(FuncT, wrapper)
  17.  
Advertisement
Add Comment
Please, Sign In to add comment