Skip to content
Myr

Fix default handling to defer UNSET normalization

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

When options are being parsed, multiple options may evaluate to have defaults of `UNSET`, and some may evaluate to have explicitly set defaults of `None`. The normalization of `UNSET -> None` is now deferred until all options have been parsed, so that it is possible to accurately evaluate and save the first non-UNSET default value. A new test exercises this functionality by pointing two flags at the same parameter declaration, and showing that their behavior is not order sensitive when only one of the flags has a default. resolves #3071

authored by Stephen Rosen · committed by Stephen Rosen · parent 6a1c0d0 · Browse files · 2 files+37 added−5 removed
This commit

State

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

Diff stats

2 files+37 added−5 removed

  • tests:+26 added−0 removed
  • other:+11 added−5 removed

2 files+37 added−5 removed

modified:src/click/core.py+11 added-5 removed
1224 unmodified lines
12251226 for param in iter_params_for_processing(param_order, self.get_params(ctx)):1227 _, args = param.handle_parse_result(ctx, opts, args)12281229 # after handling *all* parameters, do a pass over the params to rewrite1230 # `UNSET`1231 for name, value in ctx.params.items():1232 if value is UNSET:1233 ctx.params[name] = None12341235 if args and not ctx.allow_extra_args and not ctx.resilient_parsing:1236 ctx.fail(1237 ngettext(1238 "Got unexpected extra argument ({args})",
1304 unmodified lines
2543 self.expose_value2544 # We skip adding the value if it was previously set by another parameter2545 # targeting the same variable name. This prevents parameters competing for2546 # the same name to override each other.2541 and self.name not in ctx.params2547 and (self.name not in ctx.params or ctx.params[self.name] is UNSET)2548 ):2549 # Click is logically enforcing that the name is None if the parameter is2550 # not to be exposed. We still assert it here to please the type checker.2551 assert self.name is not None, (2552 f"{self!r} parameter's name should not be None when exposing value."2553 )2548 # Normalize UNSET values to None, as we're about to pass them to the2549 # command function and move them to the pure-Python realm of user-written2550 # code.2551 ctx.params[self.name] = value if value is not UNSET else None2554 # Do not normalize UNSET values to None at this point2555 # It needs to be done, but doing so too early impacts multiple parameters2556 # targeting the same variable name2557 ctx.params[self.name] = value25582559 return value, args25602561 def get_help_record(self, ctx: Context) -> tuple[str, str] | None:
802 unmodified lines
modified:tests/test_defaults.py+26 added
82 unmodified lines
83 assert "True" in result.output8485 result = runner.invoke(cli, ["foo", "--help"], default_map={"foo": {"name": True}})86 assert "default: name" in result.output878889def test_shared_param_prefers_first_default(runner):90 """test that the first default is chosen when multiple flags share a param name"""9192 @click.command93 @click.option("--red", "color", flag_value="red")94 @click.option("--green", "color", flag_value="green", default=True)95 def prefers_green(color):96 click.echo(color)9798 @click.command99 @click.option("--red", "color", flag_value="red", default=True)100 @click.option("--green", "color", flag_value="green")101 def prefers_red(color):102 click.echo(color)103104 result = runner.invoke(prefers_green, [])105 assert "green" in result.output106 result = runner.invoke(prefers_green, ["--red"])107 assert "red" in result.output108109 result = runner.invoke(prefers_red, [])110 assert "red" in result.output111 result = runner.invoke(prefers_red, ["--green"])112 assert "green" in result.output