Skip to content

Commit

Permalink
Merge pull request #2095 from pallets/invoke-cast-default
Browse files Browse the repository at this point in the history
invoke type casts default values
  • Loading branch information
davidism committed Oct 10, 2021
2 parents 3dde6c5 + 662a30e commit ba0e9dd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
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

0 comments on commit ba0e9dd

Please sign in to comment.