fix(types): decorator typing fails
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Henry Schreiner · committed by David Lord · parent fb7c6db · 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
Provenance
- Commit
e234cf9- Tree
2ef98bf- Author
Henry Schreiner <henryschreineriii@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
fb7c6db
3 files changed+46 added−25 removed
modified:src/click/core.py+30 added-4 removed
1807 unmodified lines1808 raise TypeError("Command has no name.")1809 _check_multicommand(self, name, cmd, register=True)1810 self.commands[name] = cmd1811 1812+ @t.overload1813+ def command(self, __func: t.Callable[..., t.Any]) -> Command:1814+ ...1815+1816+ @t.overload1817 def command(1818 self, *args: t.Any, **kwargs: t.Any1819+ ) -> t.Callable[[t.Callable[..., t.Any]], Command]:1820+ ...1821+1822+ 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 by15 unmodified lines1843 1844 func: t.Optional[t.Callable] = None1845 1846 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 = ()1852 1853 def decorator(f: t.Callable[..., t.Any]) -> Command:1854 cmd: Command = command(*args, **kwargs)(f)1855 self.add_command(cmd)3 unmodified lines1859 return decorator(func)1860 1861 return decorator1862 1863+ @t.overload1864+ def group(self, __func: t.Callable[..., t.Any]) -> "Group":1865+ ...1866+1867+ @t.overload1868 def group(1869 self, *args: t.Any, **kwargs: t.Any1870+ ) -> t.Callable[[t.Callable[..., t.Any]], "Group"]:1871+ ...1872+1873+ 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 by12 unmodified lines1891 1892 func: t.Optional[t.Callable] = None1893 1894 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 = ()1900 1901 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 linesmodified:src/click/decorators.py+13 added-21 removed
124 unmodified lines125 126 127 @t.overload128 def 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 ...132 133 134 @t.overload5 unmodified lines140 141 142 @t.overload143 def 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 ...149 150 153-@t.overload151 def command(155- name: t.Callable,156- **attrs: t.Any,157-) -> Command:158- ...159-160-161-def 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 lines180 .. 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 = Command184 185 func: t.Optional[t.Callable] = None186 187 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."192 193+ if cls is None:194+ cls = Command195+196 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 lines227 228 229 @t.overload230 def group(238- name: t.Optional[str] = None,239- **attrs: t.Any,240-) -> t.Callable[[F], Group]:231+ __func: t.Callable,232+) -> Group:233 ...234 235 236 @t.overload237 def group(246- name: t.Callable,238+ name: t.Optional[str] = None,239 **attrs: t.Any,248-) -> Group:240+) -> t.Callable[[F], Group]:241 ...242 243 244 def group(253 unmodified linesmodified:CHANGES.rst+3 added
3 unmodified lines4 -------------5 6 Unreleased7 8+- Fix an issue with decorator typing that caused type checking to9+ report that a command was not callable. :issue:`2227`10 11+12 Version 8.1.013 -------------14 15 Released 2022-03-281005 unmodified lines