diff --git a/CHANGES.rst b/CHANGES.rst index e6e8e9bf3..f7a5c6ef9 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -9,6 +9,8 @@ Unreleased paths. :issue:`2088` - Importing ``readline`` does not cause the ``confirm()`` prompt to disappear when pressing backspace. :issue:`2092` +- Any default values injected by ``invoke()`` are cast to the + corresponding parameter's type. :issue:`2089, 2090` Version 8.0.2 diff --git a/src/click/core.py b/src/click/core.py index 77a536aa3..f2263544a 100644 --- a/src/click/core.py +++ b/src/click/core.py @@ -739,7 +739,9 @@ def invoke( for param in other_cmd.params: if param.name not in kwargs and param.expose_value: - kwargs[param.name] = param.get_default(ctx) # type: ignore + kwargs[param.name] = param.type_cast_value( # type: ignore + ctx, param.get_default(ctx) + ) # Track all kwargs as params, so that forward() will pass # them on in subsequent calls. diff --git a/tests/test_commands.py b/tests/test_commands.py index 9ebf6121c..788398c3a 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -246,15 +246,17 @@ def cli(ctx): return ctx.invoke(other_cmd) @click.command() - @click.option("--foo", type=click.INT, default=42) + @click.option("-a", type=click.INT, default=42) + @click.option("-b", type=click.INT, default="15") + @click.option("-c", multiple=True) @click.pass_context - def other_cmd(ctx, foo): - assert ctx.info_name == "other-cmd" - click.echo(foo) + def other_cmd(ctx, a, b, c): + return ctx.info_name, a, b, c - result = runner.invoke(cli, []) - assert not result.exception - assert result.output == "42\n" + result = runner.invoke(cli, standalone_mode=False) + # invoke should type cast default values, str becomes int, empty + # multiple should be empty tuple instead of None + assert result.return_value == ("other-cmd", 42, 15, ()) def test_invoked_subcommand(runner):