Hide UNSET values in Context.invoke
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kevin Deldycke · committed by Kevin Deldycke · parent 7d71836 · Browse files · 3 files·+50 added−12 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+50 added−12 removed
- tests:+37 added−9 removed
- other:+13 added−3 removed
Provenance
- Commit
5fc3423- Tree
7f5dd42- Author
Kevin Deldycke <kevin@deldycke.com>- Committer
Kevin Deldycke <kevin@deldycke.com>- Parent
7d71836
3 files changed+50 added−12 removed
modified:src/click/core.py+11 added-1 removed
797 unmodified lines798 ctx = self._make_sub_context(other_cmd)799 800 for param in other_cmd.params:801 if param.name not in kwargs and param.expose_value:802+ default_value = param.get_default(ctx)803+ # We explicitly hide the :attr:`UNSET` value to the user, as we804+ # choose to make it an implementation detail. And because ``invoke``805+ # has been designed as part of Click public API, we return ``None``806+ # instead. Refs:807+ # https://github.com/pallets/click/issues/3066808+ # https://github.com/pallets/click/issues/3065809+ # https://github.com/pallets/click/pull/3068810+ if default_value is UNSET:811+ default_value = None812 kwargs[param.name] = param.type_cast_value( # type: ignore803- ctx, param.get_default(ctx)813+ ctx, default_value814 )815 816 # Track all kwargs as params, so that forward() will pass817 # them on in subsequent calls.2540 unmodified linesmodified:tests/test_commands.py+37 added-9 removed
226 unmodified lines227 assert result.exception is None228 assert result.output == "Debug is off\n"229 230 231+@pytest.mark.parametrize(232+ ("opt_params", "expected"),233+ (234+ # Original tests.235+ ({"type": click.INT, "default": 42}, 42),236+ ({"type": click.INT, "default": "15"}, 15),237+ ({"multiple": True}, ()),238+ # SENTINEL value tests.239+ ({"default": None}, None),240+ ({"type": click.STRING}, None), # No default specified, should be None.241+ ({"type": click.BOOL, "default": False}, False),242+ ({"type": click.BOOL, "default": True}, True),243+ ({"type": click.FLOAT, "default": 3.14}, 3.14),244+ # Multiple with default.245+ ({"multiple": True, "default": [1, 2, 3]}, (1, 2, 3)),246+ ({"multiple": True, "default": ()}, ()),247+ # Required option without value should use SENTINEL behavior.248+ ({"required": False}, None),249+ # Choice type with default.250+ ({"type": click.Choice(["a", "b", "c"]), "default": "b"}, "b"),251+ # Path type with default.252+ ({"type": click.Path(), "default": "/tmp"}, "/tmp"),253+ # Flag options.254+ ({"is_flag": True, "default": False}, False),255+ ({"is_flag": True, "default": True}, True),256+ # Count option.257+ ({"count": True}, 0),258+ # Hidden option.259+ ({"hidden": True, "default": "secret"}, "secret"),260+ ),261+)231-def test_other_command_invoke_with_defaults(runner):262+def test_other_command_invoke_with_defaults(runner, opt_params, expected):263 @click.command()264 @click.pass_context265 def cli(ctx):266 return ctx.invoke(other_cmd)267 268 @click.command()238- @click.option("-a", type=click.INT, default=42)239- @click.option("-b", type=click.INT, default="15")240- @click.option("-c", multiple=True)269+ @click.option("-a", **opt_params)270 @click.pass_context242- def other_cmd(ctx, a, b, c):243- return ctx.info_name, a, b, c271+ def other_cmd(ctx, a):272+ return ctx.info_name, a273 274 result = runner.invoke(cli, standalone_mode=False)246- # invoke should type cast default values, str becomes int, empty247- # multiple should be empty tuple instead of None248- assert result.return_value == ("other", 42, 15, ())275 276+ assert result.return_value == ("other", expected)277 278+279 def test_invoked_subcommand(runner):280 @click.group(invoke_without_command=True)281 @click.pass_context282 def cli(ctx):293 unmodified linesmodified:CHANGES.rst+2 added-2 removed
4 unmodified lines5 6 Unreleased7 8 - Don't discard pager arguments by correctly using subprocess.Popen. :issue:`3039` :pr:`3055`9+- Replace ``Sentinel.UNSET`` default values by ``None`` as they're passed through10+ the ``Context.invoke()`` method. :issue:`3066` :issue:`3065` :pr:`3068`11 10-11-12 Version 8.3.013 --------------14 15 Released 2025-09-171286 unmodified lines