summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2021-10-10 10:54:41 -0700
committerDavid Lord <davidism@gmail.com>2021-10-10 10:59:02 -0700
commit662a30e4ee3fee7e92edc56a2cd03bbf3200dc4b (patch)
tree1a72eda0119de00d864ba865d38ebf4906f39b89
parent3dde6c51c5015b7eba552348488c2f7bcaa16c69 (diff)
downloadclick-662a30e4ee3fee7e92edc56a2cd03bbf3200dc4b.tar.gz
invoke type casts default values
-rw-r--r--CHANGES.rst2
-rw-r--r--src/click/core.py4
-rw-r--r--tests/test_commands.py16
3 files changed, 14 insertions, 8 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index e6e8e9b..f7a5c6e 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 77a536a..f226354 100644
--- a/src/click/core.py
+++ b/src/click/core.py
@@ -739,7 +739,9 @@ class Context:
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 9ebf612..788398c 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -246,15 +246,17 @@ def test_other_command_invoke_with_defaults(runner):
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):