Add `cls` to signature of `argument` and `option`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Gianluca Gippetto · committed by David Lord · parent 2a7543e · Browse files · 2 files·+14 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
2 files+14 added−8 removed
- other:+14 added−8 removed
Provenance
- Commit
b4df3b9- Tree
2290007- Author
Gianluca Gippetto <gianluca.gippetto@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
2a7543e
2 files changed+14 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: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