Skip to content
Myr

Backport #3504 from `main` to `stable` branch (#3508)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kevin Deldycke · committed by GitHub · parents 5b1aba8, ddf20d0 · Browse files · 3 files+29 added−66 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−66 removed

  • tests:+10 added−48 removed
  • other:+19 added−18 removed

3 files+29 added−66 removed

modified:src/click/shell_completion.py+14 added-18 removed
179 unmodified lines
180 set -l response (env %(complete_var)s=fish_complete COMP_WORDS=(commandline -cp) \181COMP_CWORD=(commandline -t) %(prog_name)s);182183 for completion in $response;184 set -l metadata (string split \n $completion);184 set -l metadata (string split "," $completion);185186 if test $metadata[1] = "dir";187 __fish_complete_directories $metadata[2];188 else if test $metadata[1] = "file";189 __fish_complete_path $metadata[2];190 else if test $metadata[1] = "plain";191 if test $metadata[3] != "_";192 echo $metadata[2]\t$metadata[3];193 else;194 echo $metadata[2];191 echo $metadata[2];195 end;192 end;193 end;194end;195
222 unmodified lines
418 return args, incomplete419420 def format_completion(self, item: CompletionItem) -> str:421 """426 .. versionchanged:: 8.4.0427 Escape newlines in value and help to fix completion errors with428 multi-line help strings.422 .. versionchanged:: 8.4.2423 Escape newlines and replace tabs with spaces in the help text to424 fix completion errors with multi-line help strings.425 """430 # The fish completion script splits each response line on literal431 # newlines, so any newline in the value or help would corrupt the432 # frame. Replace them with the two-character escape "\n" so the text433 # round-trips through fish without breaking the format. The "_"434 # sentinel for missing help mirrors :class:`ZshComplete`.435 help_ = item.help or "_"436 value = item.value.replace("\n", r"\n")437 help_escaped = help_.replace("\n", r"\n")438 return f"{item.type}\n{value}\n{help_escaped}"426 # According to https://fishshell.com/docs/current/cmds/complete.html427 # Command substitutions found in ARGUMENTS should return a newline-428 # separated list of arguments, and each argument may optionally have a tab429 # character followed by the argument description.430 if item.help:431 help_ = item.help.replace("\n", "\\n").replace("\t", " ")432 return f"{item.type},{item.value}\t{help_}"433434 return f"{item.type},{item.value}"435436437ShellCompleteType = t.TypeVar("ShellCompleteType", bound="type[ShellComplete]")438439440_available_shells: dict[str, type[ShellComplete]] = {
236 unmodified lines
modified:tests/test_shell_completion.py+10 added-48 removed
10 unmodified lines
11from click.core import Group12from click.core import Option13from click.shell_completion import add_completion_class14from click.shell_completion import CompletionItem15from click.shell_completion import FishComplete16from click.shell_completion import shell_complete17from click.shell_completion import ShellComplete18from click.types import Choice19from click.types import File
339 unmodified lines
359 ("bash", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain,a\nplain,b\n"),360 ("bash", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain,b\n"),361 ("zsh", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain\na\n_\nplain\nb\nbee\n"),362 ("zsh", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain\nb\nbee\n"),362 ("fish", {"COMP_WORDS": "", "COMP_CWORD": ""}, "plain\na\n_\nplain\nb\nbee\n"),363 ("fish", {"COMP_WORDS": "a b", "COMP_CWORD": "b"}, "plain\nb\nbee\n"),364 ("fish", {"COMP_WORDS": 'a "b', "COMP_CWORD": '"b'}, "plain\nb\nbee\n"),363 ("fish", {"COMP_WORDS": "", "COMP_CWORD": ""}, "plain,a\nplain,b\tbee\n"),364 ("fish", {"COMP_WORDS": "a b", "COMP_CWORD": "b"}, "plain,b\tbee\n"),365 ("fish", {"COMP_WORDS": 'a "b', "COMP_CWORD": '"b'}, "plain,b\tbee\n"),366 ],367)368@pytest.mark.usefixtures("_patch_for_completion")369def test_full_complete(runner, shell, env, expect):
208 unmodified lines
578 _get_completions(cli, args=[], incomplete="")579 assert not current_warnings, "There should be no warnings after either"580581581@pytest.mark.usefixtures("_patch_for_completion")582def test_fish_multiline_help_complete(runner):583 """Test Fish completion with multi-line help text doesn't cause errors."""584 cli = Command(585 "cli",586 params=[587 Option(588 ["--at", "--attachment-type"],589 type=(str, str),590 multiple=True,591 help=(592 "\b\nAttachment with explicit mimetype,\n--at image.jpg image/jpeg"593 ),594 ),595 Option(["--other"], help="Normal help"),596 ],597 )598599 result = runner.invoke(600 cli,601 env={602 "COMP_WORDS": "cli --",603 "COMP_CWORD": "--",604 "_CLI_COMPLETE": "fish_complete",605 },606 )607608 # Should not fail609 assert result.exit_code == 0610611 # Output should contain escaped newlines, not literal newlines612 # Fish expects: plain\n--at\n{help_with_\\n}613 lines = result.output.split("\n")614615 # Find the --at completion block (3 lines: type, value, help)616 for i in range(0, len(lines) - 2, 3):617 if lines[i] == "plain" and lines[i + 1] in ("--at", "--attachment-type"):618 help_line = lines[i + 2]619 # Help should have escaped newlines (\\n), not actual newlines620 assert "\\n" in help_line621 # Should contain the example text622 assert "image.jpg" in help_line.replace("\\n", " ")623 break624 else:625 pytest.fail("--at completion not found in output")582def test_fish_format_completion_escapes_help():583 fc = FishComplete(Command("x"), {}, "x", "_X_COMPLETE")584 item = CompletionItem("--at", help="first\nsecond\tthird")585 # The newline is escaped to the literal characters backslash-n and the tab586 # becomes a space, so each completion stays on one line for fish.587 assert fc.format_completion(item) == "plain,--at\tfirst\\nsecond third"
modified:CHANGES.rst+5 added
3 unmodified lines
4-------------56Unreleased78- 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`121314Version 8.4.115-------------1617Released 2026-05-21
1474 unmodified lines