Add `cls` to signature of `@argument` and `@option` (#2312)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parents c77966e, b4df3b9 · Browse files · 3 files·+34 added−8 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+34 added−8 removed
- tests:+20 added−0 removed
- other:+14 added−8 removed
Provenance
- Commit
d3530d7- Tree
2290007- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
c77966e- Parent 2
b4df3b9
3 files changed+34 added−8 removed
modified:src/click/decorators.py+12 added-8 removed
327 unmodified lines328 329 f.__click_params__.append(param) # type: ignore330 331 332-def argument(*param_decls: str, **attrs: t.Any) -> _Decorator[FC]:332+def argument(333+ *param_decls: str, cls: t.Optional[t.Type[Argument]] = None, **attrs: t.Any334+) -> _Decorator[FC]:335 """Attaches an argument to the command. All positional arguments are336 passed as parameter declarations to :class:`Argument`; all keyword337 arguments are forwarded unchanged (except ``cls``).338 This is equivalent to creating an :class:`Argument` instance manually7 unmodified lines346 :param param_decls: Passed as positional arguments to the constructor of347 ``cls``.348 :param attrs: Passed as keyword arguments to the constructor of ``cls``.349 """350+ if cls is None:351+ cls = Argument352 353 def decorator(f: FC) -> FC:350- ArgumentClass = attrs.pop("cls", None) or Argument351- _param_memo(f, ArgumentClass(param_decls, **attrs))354+ _param_memo(f, cls(param_decls, **attrs))355 return f356 357 return decorator358 359 357-def option(*param_decls: str, **attrs: t.Any) -> _Decorator[FC]:360+def option(361+ *param_decls: str, cls: t.Optional[t.Type[Option]] = None, **attrs: t.Any362+) -> _Decorator[FC]:363 """Attaches an option to the command. All positional arguments are364 passed as parameter declarations to :class:`Option`; all keyword365 arguments are forwarded unchanged (except ``cls``).366 This is equivalent to creating an :class:`Option` instance manually7 unmodified lines374 :param param_decls: Passed as positional arguments to the constructor of375 ``cls``.376 :param attrs: Passed as keyword arguments to the constructor of ``cls``.377 """378+ if cls is None:379+ cls = Option380 381 def decorator(f: FC) -> FC:375- # Issue 926, copy attrs, so pre-defined options can re-use the same cls=376- option_attrs = attrs.copy()377- OptionClass = option_attrs.pop("cls", None) or Option378- _param_memo(f, OptionClass(param_decls, **option_attrs))382+ _param_memo(f, cls(param_decls, **attrs))383 return f384 385 return decorator386 184 unmodified linesmodified:tests/test_arguments.py+20 added
379 unmodified lines380 381 result = runner.invoke(cli, ["arg1", "cmd", "arg2", "subcmd", "--help"])382 assert not result.exception383 assert "Usage: cli ARG1 cmd ARG2 subcmd [OPTIONS]" in result.output384+385+386+def test_when_argument_decorator_is_used_multiple_times_cls_is_preserved():387+ class CustomArgument(click.Argument):388+ pass389+390+ reusable_argument = click.argument("art", cls=CustomArgument)391+392+ @click.command()393+ @reusable_argument394+ def foo(arg):395+ pass396+397+ @click.command()398+ @reusable_argument399+ def bar(arg):400+ pass401+402+ assert isinstance(foo.params[0], CustomArgument)403+ assert isinstance(bar.params[0], CustomArgument)modified:CHANGES.rst+2 added
22 unmodified lines23 - ``@group.command`` does not fail if the group was created with a custom24 ``command_class``. :issue:`2416`25 - ``multiple=True`` is allowed for flag options again and does not require26 setting ``default=()``. :issue:`2246, 2292, 2295`27+- Make the decorators returned by ``@argument()`` and ``@option()`` reusable when the28+ ``cls`` parameter is used. :issue:`2294`29 30 31 Version 8.1.332 -------------1040 unmodified lines