Fix deprecated label formatting (#3509)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 648d7c4, 82f377c · Browse files · 4 files·+35 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+35 added−2 removed
- tests:+30 added−0 removed
- other:+5 added−2 removed
Provenance
- Commit
29bacc7- Tree
65d5967- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
648d7c4- Parent 2
82f377c
4 files changed+35 added−2 removed
modified:src/click/core.py+3 added-2 removed
1187 unmodified lines1188 else:1189 text = ""1190 1191 if self.deprecated:1192- text = f"{_(text)} {_format_deprecated_label(self.deprecated)}"1192+ label = _format_deprecated_label(self.deprecated)1193+ text = f"{_(text)} {label}" if text else label1194 1195 if text:1196 formatter.write_paragraph()1197 1628 unmodified lines2826 prompt_text = prompt2827 2828 if deprecated:2829 label = _format_deprecated_label(deprecated)2829- help = f"{help} {label}" if help is not None else label2830+ help = f"{help} {label}" if help else label2831 2832 self.prompt = prompt_text2833 self.confirmation_prompt = confirmation_prompt2834 self.prompt_required = prompt_required709 unmodified linesmodified:tests/test_commands.py+16 added
490 unmodified lines491 assert deprecated in result.output492 493 494 @pytest.mark.parametrize("deprecated", [True, "USE OTHER COMMAND INSTEAD"])495+@pytest.mark.parametrize("doc", ["", None])496+def test_deprecated_empty_help_no_leading_space(runner, doc, deprecated):497+ """A command with empty or missing help text must render the deprecation498+ label at the normal indentation, without a stray leading space.499+ """500+501+ @click.command(deprecated=deprecated, help=doc)502+ def cli():503+ pass504+505+ out = runner.invoke(cli, ["--help"]).output506+ assert "\n (DEPRECATED" in out507+ assert "\n (DEPRECATED" not in out508+509+510+@pytest.mark.parametrize("deprecated", [True, "USE OTHER COMMAND INSTEAD"])511 def test_deprecated_in_invocation(runner, deprecated):512 @click.command(deprecated=deprecated)513 def deprecated_cmd():514 pass109 unmodified linesmodified:tests/test_options.py+14 added
56 unmodified lines57 if isinstance(deprecated, str):58 assert deprecated in result.output59 60 61+@pytest.mark.parametrize(62+ ("deprecated", "expected"),63+ [(True, "(DEPRECATED)"), ("USE B INSTEAD", "(DEPRECATED: USE B INSTEAD)")],64+)65+@pytest.mark.parametrize("help_text", ["", None])66+def test_deprecated_empty_help_no_leading_space(help_text, deprecated, expected):67+ """An option with empty or missing help text must not gain a stray leading68+ space before the deprecation label.69+ """70+ opt = click.Option(["--foo"], help=help_text, deprecated=deprecated)71+ ctx = click.Context(click.Command("cli"))72+ assert opt.get_help_record(ctx)[1] == expected73+74+75 @pytest.mark.parametrize("deprecated", [True, "USE B INSTEAD"])76 def test_deprecated_warning(runner, deprecated):77 @click.command()78 @click.option(3439 unmodified linesmodified:CHANGES.rst+2 added
7 unmodified lines8 - Fix Fish shell completion broken in ``8.4.0`` by :pr:`3126`. Newlines and9 tabs in option help text are now escaped, keeping the original completion10 format while still supporting multi-line help. :issue:`3502`11 :issue:`3043` :pr:`3504` :pr:`3508`12+- Deprecated commands and options with empty or missing help text no longer13+ render a stray leading space before the ``(DEPRECATED)`` label. :pr:`3509`14 15 16 Version 8.4.117 -------------1476 unmodified lines