Sort help args to make them reproducible (#3728)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 00e592c, 047adef · Browse files · 3 files·+63 added−4 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+63 added−4 removed
- tests:+50 added−0 removed
- other:+13 added−4 removed
Provenance
- Commit
9c4dfda- Tree
664ff69- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
00e592c- Parent 2
047adef
3 files changed+63 added−4 removed
modified:src/click/core.py+11 added-4 removed
1173 unmodified lines1174 1175 return rv1176 1177 def get_help_option_names(self, ctx: Context) -> list[str]:1178- """Returns the names for the help option."""1179- all_names = set(ctx.help_option_names)1178+ """Returns the names for the help option.1179+1180+ Drops duplicates and names already reserved by another parameter. Order of1181+ :attr:`Context.help_option_names` is preserved, so the result is stable.1182+1183+ .. versionchanged:: 8.5.01184+ Names keep their declaration order.1185+ """1186+ all_names = dict.fromkeys(ctx.help_option_names)1187 for param in self.params:1181- all_names.difference_update(param.opts)1182- all_names.difference_update(param.secondary_opts)1188+ for name in (*param.opts, *param.secondary_opts):1189+ all_names.pop(name, None)1190 return list(all_names)1191 1192 def get_help_option(self, ctx: Context) -> Option | None:1193 """Returns the help option object.2606 unmodified linesmodified:tests/test_commands.py+50 added
417 unmodified lines418 == expected_params419 )420 421 422+@pytest.mark.parametrize(423+ ("help_option_names", "params", "expected"),424+ [425+ # A single default name is returned unchanged.426+ (["--help"], [], ["--help"]),427+ # Declaration order is preserved verbatim, whatever it is.428+ (["-h", "--help"], [], ["-h", "--help"]),429+ (["--help", "-h"], [], ["--help", "-h"]),430+ (["--help", "-h", "-?"], [], ["--help", "-h", "-?"]),431+ (["-?", "--help", "-h"], [], ["-?", "--help", "-h"]),432+ # Duplicate names collapse to their first occurrence, keeping order.433+ (["--help", "--help"], [], ["--help"]),434+ (["-h", "--help", "-h"], [], ["-h", "--help"]),435+ # A name already claimed by another parameter's option is dropped.436+ (["-h", "--help"], [["--help"]], ["-h"]),437+ (["-h", "--help"], [["-h", "--verbose"]], ["--help"]),438+ # Both options of a feature-switch flag are removed too.439+ (["--shout", "--help"], [["--shout/--no-shout"]], ["--help"]),440+ (["--no-shout", "--help"], [["--shout/--no-shout"]], ["--help"]),441+ # Filtering every name out yields an empty list.442+ (["-h", "--help"], [["-h"], ["--help"]], []),443+ # Deduplication and conflict removal combine, order still preserved.444+ (["-h", "--help", "-h", "--assist"], [["--assist"]], ["-h", "--help"]),445+ ],446+ ids=[447+ "default",448+ "order-short-long",449+ "order-long-short",450+ "order-three",451+ "order-three-shuffled",452+ "dedupe-adjacent",453+ "dedupe-spread",454+ "conflict-long",455+ "conflict-short",456+ "conflict-flag-on",457+ "conflict-flag-off",458+ "all-removed",459+ "dedupe-and-conflict",460+ ],461+)462+def test_get_help_option_names(help_option_names, params, expected):463+ """Check help option names are deduplicated but keep stable order.464+465+ https://github.com/pallets/click/pull/3728466+ """467+ cli = click.Command("cli", params=[click.Option(decls) for decls in params])468+ ctx = click.Context(cli, help_option_names=help_option_names)469+ assert cli.get_help_option_names(ctx) == expected470+471+472 def test_help_param_priority(runner):473 """Cover the edge-case in which the eagerness of help option was not474 respected, because it was internally generated multiple times.475 225 unmodified linesmodified:CHANGES.md+2 added
61 unmodified lines62 `type`, `value_proc`, or a matching `default` instead of `Any`.63 {class}`ParamType` takes a second optional type parameter describing the64 input value it accepts (`ParamType[int, str]` for a type converting65 strings to integers), defaulting to `Any`. {pr}`3407`66+- {meth}`Command.get_help_option_names` returns the help option names in the67+ order they were declared. {pr}`3728`68 69 ## Version 8.4.270 71 Released 2026-06-241561 unmodified lines