Skip to content
Myr

Hide `Sentinel.UNSET` values for defaults processed by `Context.invoke` (#3068)

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

authored by Rowlando13 · committed by GitHub · parents 7d71836, 12fdbbe · Browse files · 3 files+53 added−14 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+53 added−14 removed

  • tests:+37 added−9 removed
  • other:+16 added−5 removed

3 files+53 added−14 removed

modified:src/click/core.py+11 added-1 removed
797 unmodified lines
798 ctx = self._make_sub_context(other_cmd)799800 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 )815816 # Track all kwargs as params, so that forward() will pass817 # them on in subsequent calls.
2540 unmodified lines
modified:tests/test_commands.py+37 added-9 removed
226 unmodified lines
227 assert result.exception is None228 assert result.output == "Debug is off\n"229230231@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)231def test_other_command_invoke_with_defaults(runner):262def 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)267268 @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, a273274 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, ())275276 assert result.return_value == ("other", expected)277278279def test_invoked_subcommand(runner):280 @click.group(invoke_without_command=True)281 @click.pass_context282 def cli(ctx):
293 unmodified lines
modified:CHANGES.rst+5 added-4 removed
3 unmodified lines
4--------------56Unreleased78- Don't discard pager arguments by correctly using subprocess.Popen. :issue:`3039` :pr:`3055`8- 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`12101113Version 8.3.014--------------1516Released 2025-09-17
15 unmodified lines
32- Fix handling of quoted and escaped parameters in Fish autocompletion. :issue:`2995` :pr:`3013`33- Lazily import ``shutil``. :pr:`3023`34- Properly forward exception information to resources registered with35 ``click.core.Context.with_resource()``. :issue:`2447` :pr:`3058`35- Fix regression related to EOF handling in CliRunner. :issue:`2939` :pr:`2940`36- Fix regression related to EOF handling in ``CliRunner``. :issue:`2939` :pr:`2940`3738Version 8.2.239-------------40
1262 unmodified lines