Always return a help record for an `Argument`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kevin Deldycke · committed by Kevin Deldycke · parent f25f697 · Browse files · 3 files·+29 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+29 added−8 removed
- tests:+15 added−0 removed
- other:+14 added−8 removed
Provenance
- Commit
05f6fd0- Tree
6a69e98- Author
Kevin Deldycke <kevin@deldycke.com>- Committer
Kevin Deldycke <kevin@deldycke.com>- Parent
f25f697
3 files changed+29 added−8 removed
modified:src/click/core.py+12 added-8 removed
1317 unmodified lines1318 """1319 args = [param for param in self.get_params(ctx) if isinstance(param, Argument)]1320 1321 if any(arg.help is not None for arg in args):1322- records = [1323- arg.get_help_record(ctx) or (arg.make_metavar(ctx), "") for arg in args1324- ]1322 with formatter.section(_("Positional arguments")):1326- formatter.write_dl(records)1323+ formatter.write_dl([arg.get_help_record(ctx) for arg in args])1324 1325 def format_epilog(self, ctx: Context, formatter: HelpFormatter) -> None:1326 """Writes the epilog into the formatter if it exists."""1327 if self.epilog:2471 unmodified lines3799 3800 def get_usage_pieces(self, ctx: Context) -> list[str]:3801 return [self.make_metavar(ctx)]3802 3806- def get_help_record(self, ctx: Context) -> tuple[str, str] | None:3807- if self.help is None:3808- return None3803+ def get_help_record(self, ctx: Context) -> tuple[str, str]:3804+ """Returns the argument's help row: its metavar and its help text.3805 3810- return self.make_metavar(ctx), self.help3806+ Unlike :meth:`Option.get_help_record`, this never returns ``None``. An3807+ argument cannot be hidden, so an undocumented one still gets a row, with3808+ an empty description.3809 3810+ .. versionchanged:: 8.5.13811+ Always returns a tuple. It used to return ``None`` when ``help`` was3812+ not set.3813+ """3814+ return self.make_metavar(ctx), self.help or ""3815+3816 def get_error_hint(self, ctx: Context | None) -> str:3817 if ctx is not None:3818 return f"'{self.make_metavar(ctx)}'"3819 return f"'{self.human_readable_name}'"27 unmodified linesmodified:tests/test_arguments.py+15 added
400 unmodified lines401 " DST",402 ]403 404 405+@pytest.mark.parametrize(406+ ("help_text", "expected"),407+ [408+ pytest.param(None, ("SRC", ""), id="None"),409+ pytest.param("", ("SRC", ""), id="empty"),410+ pytest.param("Source path", ("SRC", "Source path"), id="documented"),411+ ],412+)413+def test_argument_get_help_record_never_none(help_text, expected):414+ """Every argument gets a row, even an undocumented one."""415+ arg = click.Argument(["src"], help=help_text)416+ ctx = click.Context(click.Command("cli"))417+ assert arg.get_help_record(ctx) == expected418+419+420 def test_argument_help_optional_metavar(runner):421 @click.command()422 @click.argument("name", required=False, default="", help="The name to print")423 def cli(name):410 unmodified linesmodified:CHANGES.md+2 added
10 unmodified lines11 returns `None` for hidden options, so help screens are unchanged. {pr}`3821`12 - The `Positional arguments` help section lists every argument of the command,13 not only the documented ones. An argument with no `help` gets an empty14 description, the same way an option with no `help` does. {pr}`3860`15+- {meth}`Argument.get_help_record` always returns a tuple. It used to return16+ `None` for an argument with no `help`. {pr}`3860`17 - Document which types are inferred from `default`, and what an unrecognized18 `type` callable does to a command-line value. {issue}`3036` {pr}`3808`19 - `path_type` narrows the converted value's type for `convert` and `prompt`.20 {issue}`3822` {pr}`3858`1659 unmodified lines