Skip to content
Myr

Merge pull request #1896 from angus-lherrou/1359-uncalled-decorators

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

Add support for optional decorator parentheses

authored by David Lord · committed by GitHub · parents 7796147, 74e7f70 · 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

4 files+124 added−5 removed

modified:src/click/core.py+28 added-4 removed
1795 unmodified lines
1796 self.commands[name] = cmd17971798 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`.18051806 To customize the command class used, set the1807 :attr:`command_class` attribute.18081809 .. versionchanged:: 8.11810 This decorator can be applied without parentheses.18111812 .. versionchanged:: 8.01813 Added the :attr:`command_class` attribute.1814 """1815 from .decorators import command18161817 if self.command_class is not None and "cls" not in kwargs:1818 kwargs["cls"] = self.command_class18191820 func: t.Optional[t.Callable] = None18211822 if args and callable(args[0]):1823 func = args[0]1824 args = args[1:]18251826 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 cmd18301831 if func is not None:1832 return decorator(func)18331834 return decorator18351836 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`.18431844 To customize the group class used, set the :attr:`group_class`1845 attribute.18461847 .. versionchanged:: 8.11848 This decorator can be applied without parentheses.18491850 .. versionchanged:: 8.01851 Added the :attr:`group_class` attribute.1852 """1853 from .decorators import group18541855 func: t.Optional[t.Callable] = None18561857 if args and callable(args[0]):1858 func = args[0]1859 args = args[1:]18601861 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_class18661867 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 cmd18711872 if func is not None:1873 return decorator(func)18741875 return decorator18761877 def get_command(self, ctx: Context, cmd_name: str) -> t.Optional[Command]:1878 return self.commands.get(cmd_name)
1055 unmodified lines
modified:src/click/decorators.py+57 added-1 removed
151 unmodified lines
152 **attrs,153 )154155156@t.overload157def 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 ...163164165@t.overload166def command(167 name: t.Callable,168 cls: t.Optional[t.Type[Command]] = None,169 **attrs: t.Any,170) -> Command:171 ...172173174def 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 lines
193 :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`.197198 .. versionchanged:: 8.1199 This decorator can be applied without parentheses.200 """201 if cls is None:202 cls = Command203204 func: t.Optional[t.Callable] = None205206 if callable(name):207 func = name208 name = None209210 def decorator(f: t.Callable[..., t.Any]) -> Command:211 cmd = _make_command(f, name, attrs, cls) # type: ignore212 cmd.__doc__ = f.__doc__213 return cmd214215 if func is not None:216 return decorator(func)217218 return decorator219220191def group(name: t.Optional[str] = None, **attrs: t.Any) -> t.Callable[[F], Group]:221@t.overload222def group(223 name: t.Optional[str] = None,224 **attrs: t.Any,225) -> t.Callable[[F], Group]:226 ...227228229@t.overload230def group(231 name: t.Callable,232 **attrs: t.Any,233) -> Group:234 ...235236237def 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`.243244 .. versionchanged:: 8.1245 This decorator can be applied without parentheses.246 """247 attrs.setdefault("cls", Group)248249 if callable(name):250 grp: t.Callable[[F], Group] = t.cast(Group, command(**attrs))251 return grp(name)252253 return t.cast(Group, command(name, **attrs))254255256def _param_memo(f: FC, param: Parameter) -> None:
236 unmodified lines
added:tests/test_command_decorators.py+37 added
1import click234def test_command_no_parens(runner):5 @click.command6 def cli():7 click.echo("hello")89 result = runner.invoke(cli)10 assert result.exception is None11 assert result.output == "hello\n"121314def test_group_no_parens(runner):15 @click.group16 def grp():17 click.echo("grp1")1819 @grp.command20 def cmd1():21 click.echo("cmd1")2223 @grp.group24 def grp2():25 click.echo("grp2")2627 @grp2.command28 def cmd2():29 click.echo("cmd2")3031 result = runner.invoke(grp, ["cmd1"])32 assert result.exception is None33 assert result.output == "grp1\ncmd1\n"3435 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 lines
19 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`252627Version 8.0.428-------------
948 unmodified lines