Skip to content

Commit

Permalink
Merge pull request #2212 from matejcik/type-hints
Browse files Browse the repository at this point in the history
Improve type hints
  • Loading branch information
davidism committed Mar 17, 2022
2 parents d14ee19 + a0bbbf2 commit 5ba2320
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Version 8.1.0
- It's possible to pass a list of ``params`` to ``@command``. Any
params defined with decorators are appended to the passed params.
:issue:`2131`.
- ``@command`` decorator is annotated as returning the correct type if
a ``cls`` argument is used. :issue:`2211`


Version 8.0.4
Expand Down
27 changes: 23 additions & 4 deletions src/click/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,38 @@ def new_func(*args, **kwargs): # type: ignore
return decorator


CmdType = t.TypeVar("CmdType", bound=Command)


@t.overload
def command(
name: t.Optional[str] = None,
cls: t.Type[CmdType] = ...,
**attrs: t.Any,
) -> t.Callable[..., CmdType]:
...


@t.overload
def command(
name: t.Optional[str] = None,
cls: t.Optional[t.Type[Command]] = None,
**attrs: t.Any,
) -> t.Callable[[F], Command]:
) -> t.Callable[..., Command]:
...


@t.overload
def command(
name: t.Callable,
cls: t.Type[CmdType] = ...,
**attrs: t.Any,
) -> CmdType:
...


@t.overload
def command(
name: t.Callable,
cls: t.Optional[t.Type[Command]] = None,
**attrs: t.Any,
) -> Command:
...
Expand All @@ -143,7 +162,7 @@ def command(
name: t.Union[str, t.Callable, None] = None,
cls: t.Optional[t.Type[Command]] = None,
**attrs: t.Any,
) -> t.Union[Command, t.Callable[[F], Command]]:
) -> t.Union[Command, t.Callable[..., Command]]:
r"""Creates a new :class:`Command` and uses the decorated function as
callback. This will also automatically attach all decorated
:func:`option`\s and :func:`argument`\s as parameters to the command.
Expand Down

0 comments on commit 5ba2320

Please sign in to comment.