1212if t .TYPE_CHECKING :
1313 F = t .TypeVar ("F" , bound = t .Callable [..., t .Any ])
1414
15- def pass_context (f ):
15+
16+ def pass_context (f : "F" ) -> "F" :
1617 """Marks a callback as wanting to receive the current context
1718 object as first argument.
1819 """
1920
2021 def new_func (* args , ** kwargs ):
2122 return f (get_current_context (), * args , ** kwargs )
2223
23- return update_wrapper (new_func , f )
24+ return update_wrapper (t . cast ( "F" , new_func ) , f )
2425
2526
26- def pass_obj (f ) :
27+ def pass_obj (f : "F" ) -> "F" :
2728 """Similar to :func:`pass_context`, but only pass the object on the
2829 context onwards (:attr:`Context.obj`). This is useful if that object
2930 represents the state of a nested system.
@@ -32,10 +33,12 @@ def pass_obj(f):
3233 def new_func (* args , ** kwargs ):
3334 return f (get_current_context ().obj , * args , ** kwargs )
3435
35- return update_wrapper (new_func , f )
36+ return update_wrapper (t . cast ( "F" , new_func ) , f )
3637
3738
38- def make_pass_decorator (object_type , ensure = False ):
39+ def make_pass_decorator (
40+ object_type : t .Type , ensure : bool = False
41+ ) -> "t.Callable[[F], F]" :
3942 """Given an object type this creates a decorator that will work
4043 similar to :func:`pass_obj` but instead of passing the object of the
4144 current context, it will find the innermost context of type
@@ -58,22 +61,25 @@ def new_func(ctx, *args, **kwargs):
5861 remembered on the context if it's not there yet.
5962 """
6063
61- def decorator (f ) :
64+ def decorator (f : "F" ) -> "F" :
6265 def new_func (* args , ** kwargs ):
6366 ctx = get_current_context ()
67+
6468 if ensure :
6569 obj = ctx .ensure_object (object_type )
6670 else :
6771 obj = ctx .find_object (object_type )
72+
6873 if obj is None :
6974 raise RuntimeError (
7075 "Managed to invoke callback without a context"
7176 f" object of type { object_type .__name__ !r} "
7277 " existing."
7378 )
79+
7480 return ctx .invoke (f , obj , * args , ** kwargs )
7581
76- return update_wrapper (new_func , f )
82+ return update_wrapper (t . cast ( "F" , new_func ) , f )
7783
7884 return decorator
7985
0 commit comments