Skip to content
Myr

Merge pull request #2233 from henryiii/henryiii/fix/commandtype

Before adoption: this commit came in with the history the project adopted, so no landing records it.

fix(types): decorator typing fails

authored by David Lord · committed by GitHub · parents fb7c6db, e234cf9 · Browse files · 3 files+46 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+46 added−25 removed

  • other:+46 added−25 removed

3 files+46 added−25 removed

modified:src/click/core.py+30 added-4 removed
1807 unmodified lines
1808 raise TypeError("Command has no name.")1809 _check_multicommand(self, name, cmd, register=True)1810 self.commands[name] = cmd18111812 @t.overload1813 def command(self, __func: t.Callable[..., t.Any]) -> Command:1814 ...18151816 @t.overload1817 def command(1818 self, *args: t.Any, **kwargs: t.Any1819 ) -> t.Callable[[t.Callable[..., t.Any]], Command]:1820 ...18211822 def command(1823 self, *args: t.Any, **kwargs: t.Any1824 ) -> t.Union[t.Callable[[t.Callable[..., t.Any]], Command], Command]:1825 """A shortcut decorator for declaring and attaching a command to1826 the group. This takes the same arguments as :func:`command` and1827 immediately registers the created command with this group by
15 unmodified lines
18431844 func: t.Optional[t.Callable] = None18451846 if args and callable(args[0]):1837 func = args[0]1838 args = args[1:]1847 assert (1848 len(args) == 1 and not kwargs1849 ), "Use 'command(**kwargs)(callable)' to provide arguments."1850 (func,) = args1851 args = ()18521853 def decorator(f: t.Callable[..., t.Any]) -> Command:1854 cmd: Command = command(*args, **kwargs)(f)1855 self.add_command(cmd)
3 unmodified lines
1859 return decorator(func)18601861 return decorator18621863 @t.overload1864 def group(self, __func: t.Callable[..., t.Any]) -> "Group":1865 ...18661867 @t.overload1868 def group(1869 self, *args: t.Any, **kwargs: t.Any1870 ) -> t.Callable[[t.Callable[..., t.Any]], "Group"]:1871 ...18721873 def group(1874 self, *args: t.Any, **kwargs: t.Any1875 ) -> t.Union[t.Callable[[t.Callable[..., t.Any]], "Group"], "Group"]:1876 """A shortcut decorator for declaring and attaching a group to1877 the group. This takes the same arguments as :func:`group` and1878 immediately registers the created group with this group by
12 unmodified lines
18911892 func: t.Optional[t.Callable] = None18931894 if args and callable(args[0]):1872 func = args[0]1873 args = args[1:]1895 assert (1896 len(args) == 1 and not kwargs1897 ), "Use 'group(**kwargs)(callable)' to provide arguments."1898 (func,) = args1899 args = ()19001901 if self.group_class is not None and kwargs.get("cls") is None:1902 if self.group_class is type:1903 kwargs["cls"] = type(self)
1092 unmodified lines
modified:src/click/decorators.py+13 added-21 removed
124 unmodified lines
125126127@t.overload128def command(129 name: t.Optional[str] = None,130 cls: t.Type[CmdType] = ...,131 **attrs: t.Any,132) -> t.Callable[..., CmdType]:129 __func: t.Callable[..., t.Any],130) -> Command:131 ...132133134@t.overload
5 unmodified lines
140141142@t.overload143def command(146 name: t.Callable,144 name: t.Optional[str] = None,145 cls: t.Type[CmdType] = ...,146 **attrs: t.Any,149) -> CmdType:147) -> t.Callable[..., CmdType]:148 ...149150153@t.overload151def command(155 name: t.Callable,156 **attrs: t.Any,157) -> Command:158 ...159160161def command(152 name: t.Union[str, t.Callable, None] = None,153 cls: t.Optional[t.Type[Command]] = None,154 **attrs: t.Any,155) -> t.Union[Command, t.Callable[..., Command]]:
24 unmodified lines
180 .. versionchanged:: 8.1181 The ``params`` argument can be used. Decorated params are182 appended to the end of the list.183 """194 if cls is None:195 cls = Command184185 func: t.Optional[t.Callable] = None186187 if callable(name):188 func = name189 name = None190 assert cls is None, "Use 'command(cls=cls)(callable)' to specify a class."191 assert not attrs, "Use 'command(**kwargs)(callable)' to provide arguments."192193 if cls is None:194 cls = Command195196 def decorator(f: t.Callable[..., t.Any]) -> Command:197 if isinstance(f, Command):198 raise TypeError("Attempted to convert a callback into a command twice.")199
27 unmodified lines
227228229@t.overload230def group(238 name: t.Optional[str] = None,239 **attrs: t.Any,240) -> t.Callable[[F], Group]:231 __func: t.Callable,232) -> Group:233 ...234235236@t.overload237def group(246 name: t.Callable,238 name: t.Optional[str] = None,239 **attrs: t.Any,248) -> Group:240) -> t.Callable[[F], Group]:241 ...242243244def group(
253 unmodified lines
modified:CHANGES.rst+3 added
3 unmodified lines
4-------------56Unreleased78- Fix an issue with decorator typing that caused type checking to9 report that a command was not callable. :issue:`2227`101112Version 8.1.013-------------1415Released 2022-03-28
1005 unmodified lines