Skip to content

Commit d2b315a

Browse files
committed
typing for pass decorators
1 parent 21a68bd commit d2b315a

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"sphinx_issues",
2525
"sphinx_tabs.tabs",
2626
]
27+
autodoc_typehints = "description"
2728
intersphinx_mapping = {"python": ("https://docs.python.org/3/", None)}
2829
issues_github_path = "pallets/click"
2930

src/click/decorators.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@
1212
if 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

Comments
 (0)