File size: 22,991 Bytes
517fae7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
{
  "reward": 1.0,
  "done": true,
  "content": null,
  "info": {
    "diff_stat": {
      "files": [
        "CHANGES.md",
        "src/click/_utils.py",
        "src/click/core.py",
        "src/click/termui.py",
        "tests/test_help_defaults_multiple.py"
      ],
      "added": 133,
      "removed": 9
    },
    "graded_diff": "diff --git a/CHANGES.md b/CHANGES.md\nindex 8853860..4976b6c 100644\n--- a/CHANGES.md\n+++ b/CHANGES.md\n@@ -11,6 +11,10 @@ Unreleased\n \n Unreleased\n \n+- The `[default: ...]` shown in help for an option with `multiple=True`\n+  renders each value the way a single value is rendered: `Enum` members by\n+  name, empty strings as `\"\"`. The prompt of an option lists its choices and\n+  previews its default the same way, so what it shows can be typed back.\n - Fix `copy.deepcopy()` and `pickle` on a `Parameter`, `Option` or `Command`. {pr}`3805`\n - A `KeyboardInterrupt` arriving while `Command.main()` reports an abort or an error,\n   or while it exits, no longer escapes as an unhandled traceback. The command still\ndiff --git a/src/click/_utils.py b/src/click/_utils.py\nindex 2d4e8f2..e2811bb 100644\n--- a/src/click/_utils.py\n+++ b/src/click/_utils.py\n@@ -1,7 +1,9 @@\n from __future__ import annotations\n \n import enum\n+import inspect\n import typing as t\n+from gettext import gettext as _\n \n \n class Sentinel(enum.Enum):\n@@ -43,3 +45,33 @@ T_UNSET: t.TypeAlias = t.Literal[Sentinel.UNSET]\n \n T_FLAG_NEEDS_VALUE: t.TypeAlias = t.Literal[Sentinel.FLAG_NEEDS_VALUE]\n \"\"\"Type hint for the :data:`FLAG_NEEDS_VALUE` sentinel value.\"\"\"\n+\n+\n+def _format_default_value(value: t.Any) -> str:\n+    \"\"\"Render a value the way ``[default: ...]`` and prompts show it.\n+\n+    A list or tuple renders as its values, comma separated, each rendered by\n+    the same rules as a single value: an Enum member by its name, an empty\n+    string as ``\"\"``, a plain function as ``(dynamic)``, anything else with\n+    ``str()``.\n+\n+    :meta private:\n+    \"\"\"\n+    if isinstance(value, (list, tuple)):\n+        return \", \".join(_format_default_value(item) for item in value)\n+    if isinstance(value, enum.Enum):\n+        return value.name\n+    if inspect.isfunction(value):\n+        return _(\"(dynamic)\")\n+    if isinstance(value, str) and value == \"\":\n+        return '\"\"'\n+    return str(value)\n+\n+\n+def _format_choice(choice: t.Any) -> str:\n+    \"\"\"Render one choice the way prompts and help list it: an Enum member by\n+    its name, anything else with ``str()``.\n+\n+    :meta private:\n+    \"\"\"\n+    return choice.name if isinstance(choice, enum.Enum) else str(choice)\ndiff --git a/src/click/core.py b/src/click/core.py\nindex 1f9323a..e82eaa0 100644\n--- a/src/click/core.py\n+++ b/src/click/core.py\n@@ -23,6 +23,7 @@ from types import TracebackType\n \n from . import types\n from ._utils import FLAG_NEEDS_VALUE\n+from ._utils import _format_default_value\n from ._utils import UNSET\n from .exceptions import Abort\n from .exceptions import BadParameter\n@@ -3564,12 +3565,13 @@ class Option(Parameter):\n         ):\n             if show_default_is_str:\n                 default_string = f\"({self.show_default})\"\n-            elif isinstance(default_value, (list, tuple)):\n-                default_string = \", \".join(str(d) for d in default_value)\n-            elif isinstance(default_value, enum.Enum):\n-                default_string = default_value.name\n-            elif inspect.isfunction(default_value):\n-                default_string = _(\"(dynamic)\")\n+            elif isinstance(default_value, (list, tuple)) or (\n+                isinstance(default_value, enum.Enum)\n+                or inspect.isfunction(default_value)\n+            ):\n+                # Multi-valued defaults render each value the way the same\n+                # value renders for a single-valued option.\n+                default_string = _format_default_value(default_value)\n             elif self.is_bool_flag and self.secondary_opts:\n                 # For boolean flags that have distinct True/False opts,\n                 # use the opt without prefix instead of the value.\ndiff --git a/src/click/termui.py b/src/click/termui.py\nindex 8fe0085..0caa7cd 100644\n--- a/src/click/termui.py\n+++ b/src/click/termui.py\n@@ -15,6 +15,8 @@ from gettext import gettext as _\n from . import _compat\n from ._compat import isatty\n from ._compat import strip_ansi\n+from ._utils import _format_choice\n+from ._utils import _format_default_value\n from .exceptions import Abort\n from .exceptions import UsageError\n from .globals import resolve_color_default\n@@ -115,7 +117,9 @@ def _build_prompt(\n ) -> str:\n     prompt = text\n     if type is not None and show_choices and isinstance(type, Choice):\n-        prompt += f\" ({', '.join(map(str, type.choices))})\"\n+        # Each choice is listed the way it can be typed back, which for an\n+        # Enum member is its name, as the help and the error message show it.\n+        prompt += f\" ({', '.join(_format_choice(choice) for choice in type.choices)})\"\n     default_preview = \"\"\n     if show_default:\n         if isinstance(show_default, str):\n@@ -132,7 +136,9 @@ def _format_default(default: V) -> V | str:\n         if name is not None:\n             return str(name)\n \n-    return default\n+    # The default is previewed the way the help renders it, so that what is\n+    # shown is what the user can type back.\n+    return _format_default_value(default)\n \n \n @t.overload\n@@ -250,6 +256,15 @@ def prompt(\n     prompt = _build_prompt(\n         text, prompt_suffix, show_default, default, show_choices, type\n     )\n+    # The default exactly as the prompt shows it. Typing that text back\n+    # selects the default itself, as pressing Enter does, so that a value\n+    # rendered for display (a quoted empty string, a list of values) can be\n+    # typed back as shown.\n+    shown_default = (\n+        str(_format_default(default))\n+        if default is not None and show_default and not isinstance(show_default, str)\n+        else None\n+    )\n \n     if confirmation_prompt:\n         if confirmation_prompt is True:\n@@ -269,7 +284,9 @@ def prompt(\n                 value = t.cast(\"str\", default)\n                 break\n         try:\n-            result = value_proc(value)\n+            result = value_proc(\n+                default if shown_default is not None and value == shown_default else value\n+            )\n         except UsageError as e:\n             message = _mask_hidden_input(e.message, value) if hide_input else e.message\n             echo(_(\"Error: {message}\").format(message=message), err=err)\ndiff --git a/tests/test_help_defaults_multiple.py b/tests/test_help_defaults_multiple.py\nnew file mode 100644\nindex 0000000..cb6eb77\n--- /dev/null\n+++ b/tests/test_help_defaults_multiple.py\n@@ -0,0 +1,69 @@\n+import enum\n+\n+import click\n+from click.testing import CliRunner\n+\n+\n+class Color(enum.Enum):\n+    Red = \"r\"\n+    Blue = \"b\"\n+\n+\n+def _help(**kwargs):\n+    @click.command()\n+    @click.option(\"--opt\", show_default=True, **kwargs)\n+    def cmd(opt):\n+        pass\n+\n+    return CliRunner().invoke(cmd, [\"--help\"]).output\n+\n+\n+def test_multiple_enum_default_shows_names():\n+    assert \"[default: Red, Blue]\" in _help(\n+        type=click.Choice(Color), multiple=True, default=[Color.Red, Color.Blue]\n+    )\n+\n+\n+def test_multiple_empty_string_default_is_quoted():\n+    assert '[default: \"\", x]' in _help(multiple=True, default=[\"\", \"x\"])\n+\n+\n+def test_nargs_with_multiple_renders_every_value():\n+    assert \"[default: 1, 2, 3, 4]\" in _help(nargs=2, multiple=True, default=[(1, 2), (3, 4)])\n+\n+\n+def _prompt_line(input_text=\"\\n\", **kwargs):\n+    @click.command()\n+    @click.option(\"--opt\", prompt=\"Value\", **kwargs)\n+    def cmd(opt):\n+        pass\n+\n+    return CliRunner().invoke(cmd, [], input=input_text).output.split(\"\\n\", 1)[0]\n+\n+\n+def test_prompt_lists_enum_choices_and_default_by_name():\n+    line = _prompt_line(type=click.Choice(Color), default=Color.Blue)\n+    assert \"(Red, Blue)\" in line\n+    assert \"[Blue]\" in line\n+\n+\n+def test_prompt_previews_empty_string_default_quoted():\n+    assert '[\"\"]' in _prompt_line(default=\"\")\n+\n+\n+def test_prompt_default_typed_back_as_shown():\n+    line = CliRunner().invoke(\n+        _typed_back_cmd(multiple=True, default=[\"\", \"x\"]), [], input='\"\", x\\n'\n+    ).output\n+    assert \"GOT=('', 'x')\" in line\n+    line = CliRunner().invoke(_typed_back_cmd(default=\"\"), [], input='\"\"\\n').output\n+    assert \"GOT=''\" in line\n+\n+\n+def _typed_back_cmd(**kwargs):\n+    @click.command()\n+    @click.option(\"--opt\", prompt=\"Value\", **kwargs)\n+    def cmd(opt):\n+        click.echo(f\"GOT={opt!r}\")\n+\n+    return cmd\n"
  },
  "isError": false,
  "subscores": [
    {
      "name": "multi_enum",
      "weight": 0.1875,
      "value": 1.0,
      "children": null,
      "info": {
        "exit_code": 0,
        "stdout": "PASS multi_enum\n",
        "stderr": "",
        "_parameters": {
          "command": "bash /hud/grader/run_grading.sh multi_enum",
          "cwd": "/workspace/repo",
          "timeout_seconds": 600
        }
      }
    },
    {
      "name": "multi_empty_string",
      "weight": 0.125,
      "value": 1.0,
      "children": null,
      "info": {
        "exit_code": 0,
        "stdout": "PASS multi_empty_string\n",
        "stderr": "",
        "_parameters": {
          "command": "bash /hud/grader/run_grading.sh multi_empty_string",
          "cwd": "/workspace/repo",
          "timeout_seconds": 600
        }
      }
    },
    {
      "name": "nested_values",
      "weight": 0.0625,
      "value": 1.0,
      "children": null,
      "info": {
        "exit_code": 0,
        "stdout": "PASS nested_values\n",
        "stderr": "",
        "_parameters": {
          "command": "bash /hud/grader/run_grading.sh nested_values",
          "cwd": "/workspace/repo",
          "timeout_seconds": 600
        }
      }
    },
    {
      "name": "prompt_rendering",
      "weight": 0.125,
      "value": 1.0,
      "children": null,
      "info": {
        "exit_code": 0,
        "stdout": "PASS prompt_rendering\n",
        "stderr": "",
        "_parameters": {
          "command": "bash /hud/grader/run_grading.sh prompt_rendering",
          "cwd": "/workspace/repo",
          "timeout_seconds": 600
        }
      }
    },
    {
      "name": "untouched_paths",
      "weight": 0.125,
      "value": 1.0,
      "children": null,
      "info": {
        "exit_code": 0,
        "stdout": "PASS untouched_paths\n",
        "stderr": "",
        "_parameters": {
          "command": "bash /hud/grader/run_grading.sh untouched_paths",
          "cwd": "/workspace/repo",
          "timeout_seconds": 600
        }
      }
    },
    {
      "name": "prompt_function",
      "weight": 0.0625,
      "value": 1.0,
      "children": null,
      "info": {
        "exit_code": 0,
        "stdout": "PASS prompt_function\n",
        "stderr": "",
        "_parameters": {
          "command": "bash /hud/grader/run_grading.sh prompt_function",
          "cwd": "/workspace/repo",
          "timeout_seconds": 600
        }
      }
    },
    {
      "name": "typeback_single",
      "weight": 0.0625,
      "value": 1.0,
      "children": null,
      "info": {
        "exit_code": 0,
        "stdout": "PASS typeback_single\n",
        "stderr": "",
        "_parameters": {
          "command": "bash /hud/grader/run_grading.sh typeback_single",
          "cwd": "/workspace/repo",
          "timeout_seconds": 600
        }
      }
    },
    {
      "name": "typeback_multi",
      "weight": 0.0625,
      "value": 1.0,
      "children": null,
      "info": {
        "exit_code": 0,
        "stdout": "PASS typeback_multi\n",
        "stderr": "",
        "_parameters": {
          "command": "bash /hud/grader/run_grading.sh typeback_multi",
          "cwd": "/workspace/repo",
          "timeout_seconds": 600
        }
      }
    },
    {
      "name": "no_regression",
      "weight": 0.0625,
      "value": 1.0,
      "children": null,
      "info": {
        "exit_code": 0,
        "stdout": "PASS no_regression\n",
        "stderr": "",
        "_parameters": {
          "command": "bash /hud/grader/run_grading.sh no_regression",
          "cwd": "/workspace/repo",
          "timeout_seconds": 900
        }
      }
    },
    {
      "name": "test_quality",
      "weight": 0.0625,
      "value": 1.0,
      "children": null,
      "info": {
        "exit_code": 0,
        "stdout": "  [grader] agent test files: tests/test_help_defaults_multiple.py\nPASS test_quality\n",
        "stderr": "",
        "_parameters": {
          "command": "bash /hud/grader/run_grading.sh test_quality",
          "cwd": "/workspace/repo",
          "timeout_seconds": 600
        }
      }
    },
    {
      "name": "maintainer_review",
      "weight": 0.0625,
      "value": 1.0,
      "children": [
        {
          "name": "The diff changes how the [default: ...] text of an option's help is rendered, in src/click/core.py or in a helper that code calls; a change that only adds comments, whitespace or formatting does not count.",
          "weight": 1.0,
          "value": 1.0,
          "children": null,
          "info": {
            "reason": "core.py's list/tuple branch for the help [default: ...] now calls the new `_format_default_value` helper in `_utils.py`, which renders each item recursively (Enum by name, empty string as \"\"). This is a real behavioral change to the help default rendering."
          }
        },
        {
          "name": "The diff changes how a prompt lists an option's choices or previews its default, in src/click/termui.py or in a helper that code calls; a change that only adds comments, whitespace or formatting does not count.",
          "weight": 1.0,
          "value": 1.0,
          "children": null,
          "info": {
            "reason": "The diff changes termui.py's _build_prompt to list choices via _format_choice (Enum by name) and changes _format_default to return _format_default_value (quoted empty strings, joined lists), with new helpers in _utils.py that termui calls. These are behavioral changes, not just comments or formatting."
          }
        },
        {
          "name": "Look only at the files under src/click that the diff changes. Answer No only if one of those changes has nothing to do with any of these: rendering a default value or a choice for the help text or a prompt; a prompt accepting its default typed back exactly as it was shown (for example comparing the typed input with the shown default and using the default when they match); or code that this directly relies on (for example a shared helper it calls, or an import it needs). Changes to CHANGES.md, to test files and to any file outside src/click are not judged here and never count as unrelated edits.",
          "weight": 1.0,
          "value": 1.0,
          "children": null,
          "info": {
            "reason": "Every change under src/click (_utils.py, core.py, termui.py) concerns rendering defaults or choices for help or prompts, the typed-back-default comparison in prompt(), or the helpers and imports they rely on. No unrelated edits appear."
          }
        },
        {
          "name": "The diff leaves every test file that existed before it unchanged. Test files that the diff creates are new, not changed, and count as Yes.",
          "weight": 1.0,
          "value": 1.0,
          "children": null,
          "info": {
            "reason": "The only test file in the diff, tests/test_help_defaults_multiple.py, is newly created. No pre-existing test file is modified."
          }
        },
        {
          "name": "Look at every method or property name that the lines the diff adds define inside a class under src/click, and at every parameter the diff adds to such a method. Answer No only if one of those names does not start with an underscore and is not already defined before the diff, under the same name, on the class being changed or a class it inherits from. Names that start with an underscore count as Yes; this includes special methods such as __init__, whose names start with two underscores.",
          "weight": 1.0,
          "value": 1.0,
          "children": null,
          "info": {
            "reason": "The diff adds no new public method/property or parameter inside any class under src/click; the only new definitions are underscore-prefixed module-level functions, and the existing Option method only has its body modified. No violation exists, so the answer is Yes."
          }
        },
        {
          "name": "The diff does not remove, rename or make required any parameter of an existing public function or method under src/click, and does not change the type of value such a function or method returns (for example a function that returned a str still returns a str). Returning different text, as the ticket requires for the rendered defaults and choices, is a change of behaviour, not of type, and counts as Yes. A function or method is public when its name does not start with an underscore. Changes to functions or methods whose names start with an underscore, and new optional parameters whose names start with an underscore, count as Yes.",
          "weight": 1.0,
          "value": 1.0,
          "children": null,
          "info": {
            "reason": "No public function or method under src/click has its signature or return type changed; only private helpers are added or modified (_format_default_value, _format_choice, _format_default, _build_prompt) and bodies of public functions (prompt, Option help extra) change behavior only."
          }
        },
        {
          "name": "At least one test file added by the diff invokes a command's --help (for example through click.testing.CliRunner) for an option that has multiple=True (or nargs greater than 1 together with multiple) and a default containing an Enum member or an empty string, and asserts on the [default: ...] text in the output. Tests that only assert on a single-valued default do not meet this criterion.",
          "weight": 1.0,
          "value": 1.0,
          "children": null,
          "info": {
            "reason": "The added test file has tests that invoke --help via CliRunner for options with multiple=True and defaults containing Enum members ([Color.Red, Color.Blue]) and an empty string (['', 'x']), asserting on the '[default: ...]' text."
          }
        },
        {
          "name": "The diff adds an entry to CHANGES.md under a version that is not yet released.",
          "weight": 1.0,
          "value": 1.0,
          "children": null,
          "info": {
            "reason": "The diff adds a bullet to CHANGES.md directly under an \"Unreleased\" version heading, i.e. a version that is not yet released."
          }
        },
        {
          "name": "The entry the diff adds to CHANGES.md describes the change to how the help-text default of a multi-valued option is rendered.",
          "weight": 1.0,
          "value": 1.0,
          "children": null,
          "info": {
            "reason": "The CHANGES.md entry states that the `[default: ...]` help text for `multiple=True` options now renders each value as a single value is rendered (Enum members by name, empty strings as `\"\"`), which is the required description."
          }
        }
      ],
      "info": {
        "model": "anthropic/claude-sonnet-5.5",
        "_parameters": {
          "answer": "(answer elided; 8241 chars)",
          "criteria": [
            "The diff changes how the [default: ...] text of an option's help is rendered, in src/click/core.py or in a helper that code calls; a change that only adds comments, whitespace or formatting does not count.",
            "The diff changes how a prompt lists an option's choices or previews its default, in src/click/termui.py or in a helper that code calls; a change that only adds comments, whitespace or formatting does not count.",
            "Look only at the files under src/click that the diff changes. Answer No only if one of those changes has nothing to do with any of these: rendering a default value or a choice for the help text or a prompt; a prompt accepting its default typed back exactly as it was shown (for example comparing the typed input with the shown default and using the default when they match); or code that this directly relies on (for example a shared helper it calls, or an import it needs). Changes to CHANGES.md, to test files and to any file outside src/click are not judged here and never count as unrelated edits.",
            "The diff leaves every test file that existed before it unchanged. Test files that the diff creates are new, not changed, and count as Yes.",
            "Look at every method or property name that the lines the diff adds define inside a class under src/click, and at every parameter the diff adds to such a method. Answer No only if one of those names does not start with an underscore and is not already defined before the diff, under the same name, on the class being changed or a class it inherits from. Names that start with an underscore count as Yes; this includes special methods such as __init__, whose names start with two underscores.",
            "The diff does not remove, rename or make required any parameter of an existing public function or method under src/click, and does not change the type of value such a function or method returns (for example a function that returned a str still returns a str). Returning different text, as the ticket requires for the rendered defaults and choices, is a change of behaviour, not of type, and counts as Yes. A function or method is public when its name does not start with an underscore. Changes to functions or methods whose names start with an underscore, and new optional parameters whose names start with an underscore, count as Yes.",
            "At least one test file added by the diff invokes a command's --help (for example through click.testing.CliRunner) for an option that has multiple=True (or nargs greater than 1 together with multiple) and a default containing an Enum member or an empty string, and asserts on the [default: ...] text in the output. Tests that only assert on a single-valued default do not meet this criterion.",
            "The diff adds an entry to CHANGES.md under a version that is not yet released.",
            "The entry the diff adds to CHANGES.md describes the change to how the help-text default of a multi-valued option is rendered."
          ],
          "question": "(question elided; 529 chars)",
          "model": "anthropic/claude-sonnet-5.5"
        }
      }
    }
  ]
}