Move common `help` argument from `Option` and `Argument` to `Parameter` (#3861)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 86949d1, 405a548 · Browse files · 3 files·+61 added−31 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+61 added−31 removed
- tests:+33 added−3 removed
- other:+28 added−28 removed
Provenance
- Commit
6d5bac5- Tree
6be3fac- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
86949d1- Parent 2
405a548
3 files changed+61 added−31 removed
modified:src/click/core.py+25 added-28 removed
2257 unmodified lines2258 indicating that the argument is deprecated and highlights2259 its deprecation in --help. The message can be customized2260 by using a string as the value. A deprecated parameter2261 cannot be required, a ValueError will be raised otherwise.2262+ :param help: the help string. It is dedented and get a deprecated label if2263+ appropriate.2264 2265+ .. versionchanged:: 8.5.12266+ New ``help`` parameter to replace the one from :class:`Option` and2267+ :class:`Argument`.2268+2269 .. versionchanged:: 8.2.02270 Introduction of ``deprecated``.2271 2272 .. versionchanged:: 8.255 unmodified lines2328 _custom_shell_complete: (2329 t.Callable[[Context, Parameter, str], list[CompletionItem] | list[str]] | None2330 )2331 deprecated: bool | str2332+ help: str | None2333 2334 def __init__(2335 self,2336 param_decls: cabc.Sequence[str] | None = None,21 unmodified lines2358 [Context, Parameter, str], list[CompletionItem] | list[str]2359 ]2360 | None = None,2361 deprecated: bool | str = False,2362+ help: str | None = None,2363 ) -> None:2364 self.name, self.opts, self.secondary_opts = self._parse_decls(2365 param_decls or (), expose_value2366 )24 unmodified lines2391 self.envvar = envvar2392 self._custom_shell_complete = shell_complete2393 self.deprecated = deprecated2394 2395+ if help:2396+ help = inspect.cleandoc(help)2397+2398+ if deprecated:2399+ label = _format_deprecated_label(deprecated)2400+ help = f"{help} {label}" if help else label2401+2402+ self.help = help2403+2404 if __debug__:2405 if self.type.is_composite and nargs != self.type.arity:2406 raise ValueError(2407 f"'nargs' must be {self.type.arity} (or None) for"37 unmodified lines2445 "nargs": self.nargs,2446 "multiple": self.multiple,2447 "default": self._hide_unset(self.default),2448 "envvar": self.envvar,2449+ "help": self.help,2450 }2451 2452 def __repr__(self) -> str:2453 return f"<{self.__class__.__name__} {self.name}>"534 unmodified lines2988 default: t.Any | t.Callable[[], t.Any] | None2989 2990 count: bool2991 allow_from_autoenv: bool2974- help: str | None2992 show_default: bool | str | None2993 show_choices: bool2994 show_envvar: bool2995 17 unmodified lines3013 show_envvar: bool = False,3014 deprecated: bool | str = False,3015 **attrs: t.Any,3016 ) -> None:3000- if help:3001- help = inspect.cleandoc(help)3002-3017 super().__init__(3004- param_decls, type=type, multiple=multiple, deprecated=deprecated, **attrs3018+ param_decls,3019+ type=type,3020+ multiple=multiple,3021+ deprecated=deprecated,3022+ help=help,3023+ **attrs,3024 )3025 3026 # Phase 1: prompt-related attributes. ``_infer_flag_kind`` reads ``self.prompt``3027 # and ``self.prompt_required`` so this must run first.6 unmodified lines3034 prompt_text = None3035 else:3036 prompt_text = prompt3037 3019- if deprecated:3020- label = _format_deprecated_label(deprecated)3021- help = f"{help} {label}" if help else label3022-3038 self.prompt = prompt_text3039 self.confirmation_prompt = confirmation_prompt3040 self.prompt_required = prompt_required3041 self.hide_input = hide_input21 unmodified lines3063 if count and self.default is UNSET:3064 self.default = 03065 3066 self.allow_from_autoenv = allow_from_autoenv3052- self.help = help3067 self.show_default = show_default3068 self.show_choices = show_choices3069 self.show_envvar = show_envvar3070 153 unmodified lines3224 """3225 info_dict = super().to_info_dict()3226 info_dict.update(3227 default=self._hide_unset(self._resolve_lazy_default(self.default)),3214- help=self.help,3228 prompt=self.prompt,3229 is_flag=self.is_flag,3230 flag_value=self.flag_activation_value,3231 count=self.count,514 unmodified lines3746 3747 if "multiple" in attrs:3748 raise TypeError("__init__() got an unexpected keyword argument 'multiple'.")3749 3737- deprecated = attrs.get("deprecated", False)3750+ super().__init__(param_decls, required=required, help=help, **attrs)3751 3739- if help:3740- help = inspect.cleandoc(help)3741-3742- if deprecated:3743- label = _format_deprecated_label(deprecated)3744- help = f"{help} {label}" if help else label3745-3746- self.help = help3747-3748- super().__init__(param_decls, required=required, **attrs)3749-3750- def to_info_dict(self) -> dict[str, t.Any]:3751- info_dict = super().to_info_dict()3752- info_dict.update(help=self.help)3753- return info_dict3754-3752 @property3753 def human_readable_name(self) -> str:3754 if self.metavar is not None:3755 return self.metavar80 unmodified linesmodified:tests/test_info_dict.py+33 added-3 removed
274 unmodified lines275 276 assert TestType().to_info_dict()["name"] == "TestType"277 278 279+PARAM_KINDS = [280+ pytest.param(lambda **kwargs: click.Option(["--name"], **kwargs), id="option"),281+ pytest.param(lambda **kwargs: click.Argument(["name"], **kwargs), id="argument"),282+]283+"""Both concrete kinds of parameter, built from the same keyword arguments."""284+285+286+@pytest.mark.parametrize("make_param", PARAM_KINDS)287 @pytest.mark.parametrize(288 ("help_in", "help_out"),289 [290 pytest.param(None, None, id="None"),5 unmodified lines296 id="multi-line",297 ),298 ],299 )292-def test_argument_to_info_dict_help(help_in, help_out):293- arg = click.Argument(["name"], help=help_in)294- assert arg.to_info_dict()["help"] == help_out300+def test_param_to_info_dict_help(make_param, help_in, help_out):301+ """`Parameter` owns the help string, so every kind dedents it the same way."""302+ assert make_param(help=help_in).to_info_dict()["help"] == help_out303 304 305+@pytest.mark.parametrize("make_param", PARAM_KINDS)306+@pytest.mark.parametrize(307+ ("help_in", "deprecated", "help_out"),308+ [309+ pytest.param(None, True, "(DEPRECATED)", id="no-help"),310+ pytest.param(311+ "Pack the basket.", True, "Pack the basket. (DEPRECATED)", id="help"312+ ),313+ pytest.param(314+ "Pack the basket.",315+ "USE THE CRATE",316+ "Pack the basket. (DEPRECATED: USE THE CRATE)",317+ id="custom-label",318+ ),319+ ],320+)321+def test_param_to_info_dict_deprecated_help(make_param, help_in, deprecated, help_out):322+ """A deprecated parameter of any kind gets the same label appended."""323+ param = make_param(help=help_in, required=False, deprecated=deprecated)324+ assert param.to_info_dict()["help"] == help_out325+326+327 def test_argument_to_info_dict_nargs():328 arg = click.Argument(["files"], nargs=-1, help="files to process")329 info = arg.to_info_dict()330 assert info["nargs"] == -115 unmodified linesmodified:CHANGES.md+3 added
11 unmodified lines12 - Document which types are inferred from `default`, and what an unrecognized13 `type` callable does to a command-line value. {issue}`3036` {pr}`3808`14 - `path_type` narrows the converted value's type for `convert` and `prompt`.15 {issue}`3822` {pr}`3858`16+- {class}`Parameter` accepts a new `help` argument and {class}`Option` and {class}`Argument`17+ inherits it. {meth}`Parameter.to_info_dict` now reports `help` for every kind of parameter.18+ {pr}`3861`19 20 ## Version 8.5.021 22 Released 2026-08-241655 unmodified lines