summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatejcik <ja@matejcik.cz>2022-03-11 10:28:42 +0100
committermatejcik <ja@matejcik.cz>2022-03-11 11:23:16 +0100
commita0bbbf2033c6b4eab480c53e6cbb0fd2d737badd (patch)
tree9e903d19b4a2ee34f07836d5ca16721ee4d294bd
parentd14ee193d01096113d5de0428b8552bcd5f368e9 (diff)
downloadclick-a0bbbf2033c6b4eab480c53e6cbb0fd2d737badd.tar.gz
correctly annotate return type of @command(cls=XYZ)
-rw-r--r--CHANGES.rst2
-rw-r--r--src/click/decorators.py27
2 files changed, 25 insertions, 4 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 440589a..8662316 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -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
diff --git a/src/click/decorators.py b/src/click/decorators.py
index d7b1724..05d1334 100644
--- a/src/click/decorators.py
+++ b/src/click/decorators.py
@@ -121,19 +121,38 @@ def pass_meta_key(
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:
...
@@ -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.