Fix rendering of Enum choices' default (#3004)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents a1235aa, fe3677f · Browse files · 3 files·+92 added−1 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+92 added−1 removed
- tests:+87 added−1 removed
- other:+5 added−0 removed
Provenance
- Commit
eaf33a9- Tree
b6577ff- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
a1235aa- Parent 2
fe3677f
3 files changed+92 added−1 removed
modified:src/click/core.py+2 added
2870 unmodified lines2871 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 linesmodified:tests/test_options.py+87 added-1 removed
1+import enum2 import os3 import re4+import sys5 6+if sys.version_info < (3, 11):7+ enum.StrEnum = enum.Enum # type: ignore[assignment]8+9 import pytest10 11 import click12 from click import Option1291 unmodified lines1304 result = runner.invoke(cmd)1305 assert result.exit_code == 01306 1307 1308+class HashType(enum.Enum):1309+ MD5 = enum.auto()1310+ SHA1 = enum.auto()1311+1312+1313+class Number(enum.IntEnum):1314+ ONE = enum.auto()1315+ TWO = enum.auto()1316+1317+1318+class Letter(enum.StrEnum):1319+ A = enum.auto()1320+ B = enum.auto()1321+1322+1323+class Color(enum.Flag):1324+ RED = enum.auto()1325+ GREEN = enum.auto()1326+ BLUE = enum.auto()1327+1328+1329+class ColorInt(enum.IntFlag):1330+ RED = enum.auto()1331+ GREEN = enum.auto()1332+ BLUE = enum.auto()1333+1334+1335 @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 )1350 def test_usage_show_choices(runner, choices, metavars):1351 """When show_choices=False is set, the --help output14 unmodified lines1366 def cli_without_choices(g):1367 pass1368 1369 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+ )1374 1375 result = runner.invoke(cli_without_choices, ["--help"])1376 assert metavars in result.output1377 1378 1379 @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+)1408+def 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+ pass1413+1414+ # Check that the default value is kept normalized to the type of the choice.1415+ assert cli_with_choices.params[0].default == default1416+1417+ 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.output1423+1424+1425+@pytest.mark.parametrize(1426 "opts_one,opts_two",1427 [1428 # No duplicate shortnames1429 (19 unmodified linesmodified:CHANGES.rst+3 added
6 unmodified lines7 8 - Fix reconciliation of `default`, `flag_value` and `type` parameters for9 flag options, as well as parsing and normalization of environment variables.10 :issue:`2952` :pr:`2956`11+- Use the value of ``Enum`` choices to render their default value in help12+ screen. Refs :issue:`2911` :pr:`3004`13 - Fix completion for the Z shell (``zsh``) for completion items containing14 colons. :issue:`2703` :pr:`2846`15 16+17 Version 8.2.118 -------------19 20 Released 2025-05-201241 unmodified lines