Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invoke type casts default values #2095

Merged
merged 1 commit into from Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/click/core.py
Expand Up @@ -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.
Expand Down
16 changes: 9 additions & 7 deletions tests/test_commands.py
Expand Up @@ -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):
Expand Down