Fix default handling to defer `UNSET` normalization (#3079)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 2ed395b, 189068a · Browse files · 3 files·+43 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
3 files+43 added−5 removed
- tests:+26 added−0 removed
- other:+17 added−5 removed
Provenance
- Commit
27aaed3- Tree
2c26e50- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
2ed395b- Parent 2
189068a
3 files changed+43 added−5 removed
modified:src/click/core.py+15 added-5 removed
1224 unmodified lines1225 1226 for param in iter_params_for_processing(param_order, self.get_params(ctx)):1227 _, args = param.handle_parse_result(ctx, opts, args)1228 1229+ # We now have all parameters' values into `ctx.params`, but the data may contain1230+ # the `UNSET` sentinel.1231+ # Convert `UNSET` to `None` to ensure that the user doesn't see `UNSET`.1232+ #1233+ # Waiting until after the initial parse to convert allows us to treat `UNSET`1234+ # more like a missing value when multiple params use the same name.1235+ # Refs:1236+ # https://github.com/pallets/click/issues/30711237+ # https://github.com/pallets/click/pull/30791238+ for name, value in ctx.params.items():1239+ if value is UNSET:1240+ ctx.params[name] = None1241+1242 if args and not ctx.allow_extra_args and not ctx.resilient_parsing:1243 ctx.fail(1244 ngettext(1245 "Got unexpected extra argument ({args})",1304 unmodified lines2550 self.expose_value2551 # We skip adding the value if it was previously set by another parameter2552 # targeting the same variable name. This prevents parameters competing for2553 # the same name to override each other.2541- and self.name not in ctx.params2554+ and (self.name not in ctx.params or ctx.params[self.name] is UNSET)2555 ):2556 # Click is logically enforcing that the name is None if the parameter is2557 # not to be exposed. We still assert it here to please the type checker.2558 assert self.name is not None, (2559 f"{self!r} parameter's name should not be None when exposing value."2560 )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 None2561+ ctx.params[self.name] = value2562 2563 return value, args2564 2565 def get_help_record(self, ctx: Context) -> tuple[str, str] | None:802 unmodified linesmodified:tests/test_defaults.py+26 added
82 unmodified lines83 assert "True" in result.output84 85 result = runner.invoke(cli, ["foo", "--help"], default_map={"foo": {"name": True}})86 assert "default: name" in result.output87+88+89+def test_shared_param_prefers_first_default(runner):90+ """test that the first default is chosen when multiple flags share a param name"""91+92+ @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)97+98+ @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)103+104+ result = runner.invoke(prefers_green, [])105+ assert "green" in result.output106+ result = runner.invoke(prefers_green, ["--red"])107+ assert "red" in result.output108+109+ result = runner.invoke(prefers_red, [])110+ assert "red" in result.output111+ result = runner.invoke(prefers_red, ["--green"])112+ assert "green" in result.outputmodified:CHANGES.rst+2 added
7 unmodified lines8 - Don't discard pager arguments by correctly using ``subprocess.Popen``. :issue:`3039`9 :pr:`3055`10 - Replace ``Sentinel.UNSET`` default values by ``None`` as they're passed through11 the ``Context.invoke()`` method. :issue:`3066` :issue:`3065` :pr:`3068`12+- Fix conversion of ``Sentinel.UNSET`` happening too early, which caused incorrect13+ behavior for multiple parameters using the same name. :issue:`3071` :pr:`3079`14 15 Version 8.3.016 --------------17 1287 unmodified lines