Skip to content
Myr

Merge branch 'stable' into fix-typing-bad-param

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

authored by Rowlando13 · committed by GitHub · parents 3155dca, eaf33a9 · Browse files · 5 files+189 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

5 files+189 added−2 removed

  • tests:+162 added−1 removed
  • other:+27 added−1 removed

5 files+189 added−2 removed

modified:src/click/core.py+2 added
2870 unmodified lines
2871 if show_default_is_str:2872 default_string = f"({self.show_default})"2873 elif isinstance(default_value, (list, tuple)):2874 default_string = ", ".join(str(d) for d in default_value)2875 elif isinstance(default_value, enum.Enum):2876 default_string = default_value.name2877 elif inspect.isfunction(default_value):2878 default_string = _("(dynamic)")2879 elif self.is_bool_flag and self.secondary_opts:2880 # For boolean flags that have distinct True/False opts,
298 unmodified lines
modified:src/click/shell_completion.py+21 added-1 removed
122 unmodified lines
123124%(complete_func)s_setup;125"""126127# See ZshComplete.format_completion below, and issue #2703, before128# changing this script.129#130# (TL;DR: _describe is picky about the format, but this Zsh script snippet131# is already widely deployed. So freeze this script, and use clever-ish132# handling of colons in ZshComplet.format_completion.)133_SOURCE_ZSH = """\134#compdef %(prog_name)s135136%(complete_func)s() {
241 unmodified lines
378379 return args, incomplete380381 def format_completion(self, item: CompletionItem) -> str:376 return f"{item.type}\n{item.value}\n{item.help if item.help else '_'}"382 help_ = item.help or "_"383 # The zsh completion script uses `_describe` on items with help384 # texts (which splits the item help from the item value at the385 # first unescaped colon) and `compadd` on items without help386 # text (which uses the item value as-is and does not support387 # colon escaping). So escape colons in the item value if and388 # only if the item help is not the sentinel "_" value, as used389 # by the completion script.390 #391 # (The zsh completion script is potentially widely deployed, and392 # thus harder to fix than this method.)393 #394 # See issue #1812 and issue #2703 for further context.395 value = item.value.replace(":", r"\:") if help_ != "_" else item.value396 return f"{item.type}\n{value}\n{help_}"397398399class FishComplete(ShellComplete):400 """Shell completion for Fish."""
264 unmodified lines
modified:tests/test_options.py+87 added-1 removed
1import enum2import os3import re4import sys56if sys.version_info < (3, 11):7 enum.StrEnum = enum.Enum # type: ignore[assignment]89import pytest1011import click12from click import Option
1291 unmodified lines
1304 result = runner.invoke(cmd)1305 assert result.exit_code == 0130613071308class HashType(enum.Enum):1309 MD5 = enum.auto()1310 SHA1 = enum.auto()131113121313class Number(enum.IntEnum):1314 ONE = enum.auto()1315 TWO = enum.auto()131613171318class Letter(enum.StrEnum):1319 A = enum.auto()1320 B = enum.auto()132113221323class Color(enum.Flag):1324 RED = enum.auto()1325 GREEN = enum.auto()1326 BLUE = enum.auto()132713281329class ColorInt(enum.IntFlag):1330 RED = enum.auto()1331 GREEN = enum.auto()1332 BLUE = enum.auto()133313341335@pytest.mark.parametrize(1336 ("choices", "metavars"),1337 [1338 pytest.param(["foo", "bar"], "[TEXT]", id="text choices"),1339 pytest.param([1, 2], "[INTEGER]", id="int choices"),1340 pytest.param([1.0, 2.0], "[FLOAT]", id="float choices"),1341 pytest.param([True, False], "[BOOLEAN]", id="bool choices"),1342 pytest.param(["foo", 1], "[TEXT|INTEGER]", id="text/int choices"),1343 pytest.param(HashType, "[HASHTYPE]", id="enum choices"),1344 pytest.param(Number, "[NUMBER]", id="int enum choices"),1345 pytest.param(Letter, "[LETTER]", id="str enum choices"),1346 pytest.param(Color, "[COLOR]", id="flag enum choices"),1347 pytest.param(ColorInt, "[COLORINT]", id="int flag enum choices"),1348 ],1349)1350def test_usage_show_choices(runner, choices, metavars):1351 """When show_choices=False is set, the --help output
14 unmodified lines
1366 def cli_without_choices(g):1367 pass13681369 result = runner.invoke(cli_with_choices, ["--help"])1333 assert f"[{'|'.join([str(i) for i in choices])}]" in result.output1370 assert (1371 f"[{'|'.join(i.name if isinstance(i, enum.Enum) else str(i) for i in choices)}]"1372 in result.output1373 )13741375 result = runner.invoke(cli_without_choices, ["--help"])1376 assert metavars in result.output137713781379@pytest.mark.parametrize(1380 ("choices", "default", "default_string"),1381 [1382 (["foo", "bar"], "bar", "bar"),1383 # The default value is not enforced to be in the choices.1384 (["foo", "bar"], "random", "random"),1385 # None cannot be a default value as-is: it left the default value as unset.1386 (["foo", "bar"], None, None),1387 ([0, 1], 0, "0"),1388 # Values are not coerced to the type of the choice, even if equivalent.1389 ([0, 1], 0.0, "0.0"),1390 ([1, 2], 2, "2"),1391 ([1.0, 2.0], 2, "2"),1392 ([1.0, 2.0], 2.0, "2.0"),1393 ([True, False], True, "True"),1394 ([True, False], False, "False"),1395 (["foo", 1], "foo", "foo"),1396 (["foo", 1], 1, "1"),1397 # Enum choices are rendered as their names.1398 # See: https://github.com/pallets/click/issues/29111399 (HashType, HashType.SHA1, "SHA1"),1400 # Enum choices allow defaults strings that are their names.1401 (HashType, "SHA1", "SHA1"),1402 (Number, Number.TWO, "TWO"),1403 (Letter, Letter.B, "B"),1404 (Color, Color.GREEN, "GREEN"),1405 (ColorInt, ColorInt.GREEN, "GREEN"),1406 ],1407)1408def test_choice_default_rendering(runner, choices, default, default_string):1409 @click.command()1410 @click.option("-g", type=click.Choice(choices), default=default, show_default=True)1411 def cli_with_choices(g):1412 pass14131414 # Check that the default value is kept normalized to the type of the choice.1415 assert cli_with_choices.params[0].default == default14161417 result = runner.invoke(cli_with_choices, ["--help"])1418 extra_usage = f"[default: {default_string}]"1419 if default_string is None:1420 assert extra_usage not in result.output1421 else:1422 assert extra_usage in result.output142314241425@pytest.mark.parametrize(1426 "opts_one,opts_two",1427 [1428 # No duplicate shortnames1429 (
19 unmodified lines
modified:tests/test_shell_completion.py+75 added
1import textwrap2import warnings3from collections.abc import Mapping45import pytest67import click.shell_completion
347 unmodified lines
355 result = runner.invoke(cli, env=env)356 assert result.output == expect357358359@pytest.mark.parametrize(360 ("env", "expect"),361 [362 (363 {"COMP_WORDS": "", "COMP_CWORD": "0"},364 textwrap.dedent(365 """\366 plain367 a368 _369 plain370 b371 bee372 plain373 c\\:d374 cee:dee375 plain376 c:e377 _378 """379 ),380 ),381 (382 {"COMP_WORDS": "a c", "COMP_CWORD": "1"},383 textwrap.dedent(384 """\385 plain386 c\\:d387 cee:dee388 plain389 c:e390 _391 """392 ),393 ),394 (395 {"COMP_WORDS": "a c:", "COMP_CWORD": "1"},396 textwrap.dedent(397 """\398 plain399 c\\:d400 cee:dee401 plain402 c:e403 _404 """405 ),406 ),407 ],408)409@pytest.mark.usefixtures("_patch_for_completion")410def test_zsh_full_complete_with_colons(411 runner, env: Mapping[str, str], expect: str412) -> None:413 cli = Group(414 "cli",415 commands=[416 Command("a"),417 Command("b", help="bee"),418 Command("c:d", help="cee:dee"),419 Command("c:e"),420 ],421 )422 result = runner.invoke(423 cli,424 env={425 **env,426 "_CLI_COMPLETE": "zsh_complete",427 },428 )429 assert result.output == expect430431432@pytest.mark.usefixtures("_patch_for_completion")433def test_context_settings(runner):434 def complete(ctx, param, incomplete):435 return ctx.obj["choices"]436
111 unmodified lines
modified:CHANGES.rst+4 added
9 unmodified lines
10 :issue:`2952` :pr:`2956`11- Fix typing issue in ``BadParameter`` and ``MissingParameter`` exceptions for the12 parameter ``param_hint`` that did not allow for a sequence of string where the13 underlying functino ``_join_param_hints`` allows for it. :issue:`2777` :pr:`2990`14- Use the value of ``Enum`` choices to render their default value in help15 screen. Refs :issue:`2911` :pr:`3004`16- Fix completion for the Z shell (``zsh``) for completion items containing17 colons. :issue:`2703` :pr:`2846`1819Version 8.2.120-------------21
1242 unmodified lines