Skip to content
Myr

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

3 files+34 added−8 removed

modified:src/click/decorators.py+12 added-8 removed
327 unmodified lines
328329 f.__click_params__.append(param) # type: ignore330331332def argument(*param_decls: str, **attrs: t.Any) -> _Decorator[FC]:332def 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 manually
7 unmodified lines
346 :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 = Argument352353 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 f356357 return decorator358359357def option(*param_decls: str, **attrs: t.Any) -> _Decorator[FC]:360def 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 manually
7 unmodified lines
374 :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 = Option380381 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 f384385 return decorator386
184 unmodified lines
modified:tests/test_arguments.py+20 added
379 unmodified lines
380381 result = runner.invoke(cli, ["arg1", "cmd", "arg2", "subcmd", "--help"])382 assert not result.exception383 assert "Usage: cli ARG1 cmd ARG2 subcmd [OPTIONS]" in result.output384385386def test_when_argument_decorator_is_used_multiple_times_cls_is_preserved():387 class CustomArgument(click.Argument):388 pass389390 reusable_argument = click.argument("art", cls=CustomArgument)391392 @click.command()393 @reusable_argument394 def foo(arg):395 pass396397 @click.command()398 @reusable_argument399 def bar(arg):400 pass401402 assert isinstance(foo.params[0], CustomArgument)403 assert isinstance(bar.params[0], CustomArgument)
modified:CHANGES.rst+2 added
22 unmodified lines
23- ``@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`293031Version 8.1.332-------------
1040 unmodified lines