Skip to content
Myr

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

3 files+61 added−31 removed

modified:src/click/core.py+25 added-28 removed
2257 unmodified lines
2258 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.22642265 .. versionchanged:: 8.5.12266 New ``help`` parameter to replace the one from :class:`Option` and2267 :class:`Argument`.22682269 .. versionchanged:: 8.2.02270 Introduction of ``deprecated``.22712272 .. versionchanged:: 8.2
55 unmodified lines
2328 _custom_shell_complete: (2329 t.Callable[[Context, Parameter, str], list[CompletionItem] | list[str]] | None2330 )2331 deprecated: bool | str2332 help: str | None23332334 def __init__(2335 self,2336 param_decls: cabc.Sequence[str] | None = None,
21 unmodified lines
2358 [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 lines
2391 self.envvar = envvar2392 self._custom_shell_complete = shell_complete2393 self.deprecated = deprecated23942395 if help:2396 help = inspect.cleandoc(help)23972398 if deprecated:2399 label = _format_deprecated_label(deprecated)2400 help = f"{help} {label}" if help else label24012402 self.help = help24032404 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 lines
2445 "nargs": self.nargs,2446 "multiple": self.multiple,2447 "default": self._hide_unset(self.default),2448 "envvar": self.envvar,2449 "help": self.help,2450 }24512452 def __repr__(self) -> str:2453 return f"<{self.__class__.__name__} {self.name}>"
534 unmodified lines
2988 default: t.Any | t.Callable[[], t.Any] | None29892990 count: bool2991 allow_from_autoenv: bool2974 help: str | None2992 show_default: bool | str | None2993 show_choices: bool2994 show_envvar: bool2995
17 unmodified lines
3013 show_envvar: bool = False,3014 deprecated: bool | str = False,3015 **attrs: t.Any,3016 ) -> None:3000 if help:3001 help = inspect.cleandoc(help)30023017 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 )30253026 # Phase 1: prompt-related attributes. ``_infer_flag_kind`` reads ``self.prompt``3027 # and ``self.prompt_required`` so this must run first.
6 unmodified lines
3034 prompt_text = None3035 else:3036 prompt_text = prompt30373019 if deprecated:3020 label = _format_deprecated_label(deprecated)3021 help = f"{help} {label}" if help else label30223038 self.prompt = prompt_text3039 self.confirmation_prompt = confirmation_prompt3040 self.prompt_required = prompt_required3041 self.hide_input = hide_input
21 unmodified lines
3063 if count and self.default is UNSET:3064 self.default = 030653066 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 lines
3224 """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 lines
37463747 if "multiple" in attrs:3748 raise TypeError("__init__() got an unexpected keyword argument 'multiple'.")37493737 deprecated = attrs.get("deprecated", False)3750 super().__init__(param_decls, required=required, help=help, **attrs)37513739 if help:3740 help = inspect.cleandoc(help)37413742 if deprecated:3743 label = _format_deprecated_label(deprecated)3744 help = f"{help} {label}" if help else label37453746 self.help = help37473748 super().__init__(param_decls, required=required, **attrs)37493750 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_dict37543752 @property3753 def human_readable_name(self) -> str:3754 if self.metavar is not None:3755 return self.metavar
80 unmodified lines
modified:tests/test_info_dict.py+33 added-3 removed
274 unmodified lines
275276 assert TestType().to_info_dict()["name"] == "TestType"277278279PARAM_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."""284285286@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 lines
296 id="multi-line",297 ),298 ],299)292def 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_out300def 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_out303304305@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)321def 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_out325326327def 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"] == -1
15 unmodified lines
modified:CHANGES.md+3 added
11 unmodified lines
12- 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`1920## Version 8.5.02122Released 2026-08-24
1655 unmodified lines