command and group decorator parentheses are optional
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Angus L'Herrou · committed by David Lord · parent 7796147 · Browse files · 4 files·+124 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+124 added−5 removed
- tests:+37 added−0 removed
- other:+87 added−5 removed
Provenance
- Commit
74e7f70- Tree
7a22e7d- Author
Angus L'Herrou <piraka@brandeis.edu>- Committer
David Lord <davidism@gmail.com>- Parent
7796147
4 files changed+124 added−5 removed
modified:src/click/core.py+28 added-4 removed
1795 unmodified lines1796 self.commands[name] = cmd1797 1798 def command(1799 self, *args: t.Any, **kwargs: t.Any1800- ) -> t.Callable[[t.Callable[..., t.Any]], Command]:1800+ ) -> t.Union[t.Callable[[t.Callable[..., t.Any]], Command], Command]:1801 """A shortcut decorator for declaring and attaching a command to1802 the group. This takes the same arguments as :func:`command` and1803 immediately registers the created command with this group by1804 calling :meth:`add_command`.1805 1806 To customize the command class used, set the1807 :attr:`command_class` attribute.1808 1809+ .. versionchanged:: 8.11810+ This decorator can be applied without parentheses.1811+1812 .. versionchanged:: 8.01813 Added the :attr:`command_class` attribute.1814 """1815 from .decorators import command1816 1817 if self.command_class is not None and "cls" not in kwargs:1818 kwargs["cls"] = self.command_class1819 1820+ func: t.Optional[t.Callable] = None1821+1822+ if args and callable(args[0]):1823+ func = args[0]1824+ args = args[1:]1825+1826 def decorator(f: t.Callable[..., t.Any]) -> Command:1818- cmd = command(*args, **kwargs)(f)1827+ cmd: Command = command(*args, **kwargs)(f)1828 self.add_command(cmd)1829 return cmd1830 1831+ if func is not None:1832+ return decorator(func)1833+1834 return decorator1835 1836 def group(1837 self, *args: t.Any, **kwargs: t.Any1826- ) -> t.Callable[[t.Callable[..., t.Any]], "Group"]:1838+ ) -> t.Union[t.Callable[[t.Callable[..., t.Any]], "Group"], "Group"]:1839 """A shortcut decorator for declaring and attaching a group to1840 the group. This takes the same arguments as :func:`group` and1841 immediately registers the created group with this group by1842 calling :meth:`add_command`.1843 1844 To customize the group class used, set the :attr:`group_class`1845 attribute.1846 1847+ .. versionchanged:: 8.11848+ This decorator can be applied without parentheses.1849+1850 .. versionchanged:: 8.01851 Added the :attr:`group_class` attribute.1852 """1853 from .decorators import group1854 1855+ func: t.Optional[t.Callable] = None1856+1857+ if args and callable(args[0]):1858+ func = args[0]1859+ args = args[1:]1860+1861 if self.group_class is not None and "cls" not in kwargs:1862 if self.group_class is type:1863 kwargs["cls"] = type(self)1864 else:1865 kwargs["cls"] = self.group_class1866 1867 def decorator(f: t.Callable[..., t.Any]) -> "Group":1847- cmd = group(*args, **kwargs)(f)1868+ cmd: Group = group(*args, **kwargs)(f)1869 self.add_command(cmd)1870 return cmd1871 1872+ if func is not None:1873+ return decorator(func)1874+1875 return decorator1876 1877 def get_command(self, ctx: Context, cmd_name: str) -> t.Optional[Command]:1878 return self.commands.get(cmd_name)1055 unmodified linesmodified:src/click/decorators.py+57 added-1 removed
151 unmodified lines152 **attrs,153 )154 155 156+@t.overload157 def command(158 name: t.Optional[str] = None,159 cls: t.Optional[t.Type[Command]] = None,160 **attrs: t.Any,161 ) -> t.Callable[[F], Command]:162+ ...163+164+165+@t.overload166+def command(167+ name: t.Callable,168+ cls: t.Optional[t.Type[Command]] = None,169+ **attrs: t.Any,170+) -> Command:171+ ...172+173+174+def command(175+ name: t.Union[str, t.Callable, None] = None,176+ cls: t.Optional[t.Type[Command]] = None,177+ **attrs: t.Any,178+) -> t.Union[Command, t.Callable[[F], Command]]:179 r"""Creates a new :class:`Command` and uses the decorated function as180 callback. This will also automatically attach all decorated181 :func:`option`\s and :func:`argument`\s as parameters to the command.182 10 unmodified lines193 :param name: the name of the command. This defaults to the function194 name with underscores replaced by dashes.195 :param cls: the command class to instantiate. This defaults to196 :class:`Command`.197+198+ .. versionchanged:: 8.1199+ This decorator can be applied without parentheses.200 """201 if cls is None:202 cls = Command203 204+ func: t.Optional[t.Callable] = None205+206+ if callable(name):207+ func = name208+ name = None209+210 def decorator(f: t.Callable[..., t.Any]) -> Command:211 cmd = _make_command(f, name, attrs, cls) # type: ignore212 cmd.__doc__ = f.__doc__213 return cmd214 215+ if func is not None:216+ return decorator(func)217+218 return decorator219 220 191-def group(name: t.Optional[str] = None, **attrs: t.Any) -> t.Callable[[F], Group]:221+@t.overload222+def group(223+ name: t.Optional[str] = None,224+ **attrs: t.Any,225+) -> t.Callable[[F], Group]:226+ ...227+228+229+@t.overload230+def group(231+ name: t.Callable,232+ **attrs: t.Any,233+) -> Group:234+ ...235+236+237+def group(238+ name: t.Union[str, t.Callable, None] = None, **attrs: t.Any239+) -> t.Union[Group, t.Callable[[F], Group]]:240 """Creates a new :class:`Group` with a function as callback. This241 works otherwise the same as :func:`command` just that the `cls`242 parameter is set to :class:`Group`.243+244+ .. versionchanged:: 8.1245+ This decorator can be applied without parentheses.246 """247 attrs.setdefault("cls", Group)248+249+ if callable(name):250+ grp: t.Callable[[F], Group] = t.cast(Group, command(**attrs))251+ return grp(name)252+253 return t.cast(Group, command(name, **attrs))254 255 256 def _param_memo(f: FC, param: Parameter) -> None:236 unmodified linesadded:tests/test_command_decorators.py+37 added
1+import click2+3+4+def test_command_no_parens(runner):5+ @click.command6+ def cli():7+ click.echo("hello")8+9+ result = runner.invoke(cli)10+ assert result.exception is None11+ assert result.output == "hello\n"12+13+14+def test_group_no_parens(runner):15+ @click.group16+ def grp():17+ click.echo("grp1")18+19+ @grp.command20+ def cmd1():21+ click.echo("cmd1")22+23+ @grp.group24+ def grp2():25+ click.echo("grp2")26+27+ @grp2.command28+ def cmd2():29+ click.echo("cmd2")30+31+ result = runner.invoke(grp, ["cmd1"])32+ assert result.exception is None33+ assert result.output == "grp1\ncmd1\n"34+35+ result = runner.invoke(grp, ["grp2", "cmd2"])36+ assert result.exception is None37+ assert result.output == "grp1\ngrp2\ncmd2\n"modified:CHANGES.rst+2 added
18 unmodified lines19 encoding instead of ASCII for misconfigured systems. Click's UTF-820 locale detection is removed. :issue:`2198`21 - Single options boolean flags with ``show_default=True`` only show22 the default if it is ``True``. :issue:`1971`23+- The ``command`` and ``group`` decorators can be applied with or24+ without parentheses. :issue:`1359`25 26 27 Version 8.0.428 -------------948 unmodified lines