Skip to content
Myr

Merge branch '3.x-maintenance'

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

authored by Armin Ronacher · committed by Armin Ronacher · parents 8691636, bb42d2e · Browse files · 4 files+113 added−9 removed
This commit

State

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

Diff stats

4 files+113 added−9 removed

  • tests:+36 added−1 removed
  • other:+77 added−8 removed

4 files+113 added−9 removed

modified:click/core.py+46 added-8 removed
367 unmodified lines
368 """369 return self.command.get_help(self)370371 def invoke(*args, **kwargs):372 """Invokes a command callback in exactly the way it expects.372 """Invokes a command callback in exactly the way it expects. There373 are two ways to invoke this method:374375 1. the first argument can be a callback and all other arguments and376 keyword arguments are forwarded directly to the function.377 2. the first argument is a click command object. In that case all378 arguments are forwarded as well but proper click parameters379 (options and click arguments) must be keyword arguments and Click380 will fill in defaults.381382 Note that before Click 3.2 keyword arguments were not properly filled383 in against the intention of this code and no context was created. For384 more information about this change and why it was done in a bugfix385 release see :ref:`upgrade-to-3.2`.386 """387 self, callback = args[:2]388 ctx = self389390 # This is just to improve the error message in cases where old391 # code incorrectly invoked this method. This will eventually be392 # removed.393 injected_arguments = False394395 # It's also possible to invoke another command which might or377 # might not have a callback.396 # might not have a callback. In that case we also fill397 # in defaults and make a new context for this command.398 if isinstance(callback, Command):379 callback = callback.callback399 other_cmd = callback400 callback = other_cmd.callback401 ctx = Context(other_cmd, info_name=other_cmd.name, parent=self)402 if callback is None:403 raise TypeError('The given command does not have a '404 'callback that can be invoked.')405406 for param in other_cmd.params:407 if param.name not in kwargs and param.expose_value:408 kwargs[param.name] = param.get_default(ctx)409 injected_arguments = True410411 args = args[2:]412 if getattr(callback, '__click_pass_context__', False):386 args = (self,) + args413 args = (ctx,) + args414 with augment_usage_errors(self):415 try:416 with ctx:388 return callback(*args, **kwargs)417 return callback(*args, **kwargs)418 except TypeError as e:419 if not injected_arguments:420 raise421 if 'got multiple values for keyword argument' in str(e):422 raise RuntimeError(423 'You called .invoke() on the context with a command '424 'but provided parameters as positional arguments. '425 'This is not supported but sometimes worked by chance '426 'in older versions of Click. To fix this see '427 'http://click.pocoo.org/upgrading/#upgrading-to-3.2')428429 def forward(*args, **kwargs):391 """Similar to :meth:`forward` but fills in default keyword430 """Similar to :meth:`invoke` but fills in default keyword431 arguments from the current context if the other command expects432 it. This cannot invoke callbacks directly, only other commands.433 """434 self, cmd = args[:2]
3 unmodified lines
438 if not isinstance(cmd, Command):439 raise TypeError('Callback is not a command.')440441 for param in self.params:403 if param in self.params and \404 param not in kwargs:442 if param not in kwargs:443 kwargs[param] = self.params[param]444445 return self.invoke(cmd, **kwargs)446
1121 unmodified lines
modified:docs/upgrading.rst+28 added
4 unmodified lines
5this is not entirely possible. In case we need to break backwards6compatibility this document gives you information about how to upgrade or7handle backwards compatibility properly.89.. _upgrade-to-3.2:1011Upgrading to 3.212----------------1314Click 3.2 contains a fix for the :meth:`Context.invoke` function when used15with other commands. The original intention of this function was to16invoke the other command as as if it came from the command line when it17was passed a context object instead of a function. This use was only18documented in a single place in the documentation before and there was no19proper explanation for the method in the API documentation.2021The core issue is that before 3.2 this call worked against intentions::2223 ctx.invoke(other_command, 'arg1', 'arg2')2425This was never intended to work as it does not allow Click to operate on26the parameters. Given that this pattern was never documented and ill27intended the decision was made to change this behavior in a bugfix release28before it spreads by accident and developers depend on it.2930The correct invocation for the above command is the following::3132 ctx.invoke(other_command, name_of_arg1='arg1', name_of_arg2='arg2')3334This also allowed us to fix the issue that defaults were not handled35properly by this function.3637.. _upgrade-to-2.0:3839Upgrading to 2.040----------------
32 unmodified lines
modified:tests/test_commands.py+36 added-1 removed
5 unmodified lines
6def test_other_command_invoke(runner):7 @click.command()8 @click.pass_context9 def cli(ctx):10 return ctx.invoke(other_cmd, 42)10 return ctx.invoke(other_cmd, arg=42)1112 @click.command()13 @click.argument('arg', type=click.INT)14 def other_cmd(arg):
3 unmodified lines
18 assert not result.exception19 assert result.output == '42\n'202122def test_other_command_invoke_invalid_custom_error(runner):23 @click.command()24 @click.pass_context25 def cli(ctx):26 return ctx.invoke(other_cmd, 42)2728 @click.command()29 @click.argument('arg', type=click.INT)30 def other_cmd(arg):31 click.echo(arg)3233 result = runner.invoke(cli, [])34 assert isinstance(result.exception, RuntimeError)35 assert 'upgrading-to-3.2' in str(result.exception)36 assert click.__version__ < '5.0'373839def test_other_command_forward(runner):40 cli = click.Group()4142 @cli.command()
167 unmodified lines
210211 result = runner.invoke(cli, ['sync'])212 assert result.exception is None213 assert result.output == 'Debug is off\n'214215216def test_other_command_invoke_with_defaults(runner):217 @click.command()218 @click.pass_context219 def cli(ctx):220 return ctx.invoke(other_cmd)221222 @click.command()223 @click.option('--foo', type=click.INT, default=42)224 @click.pass_context225 def other_cmd(ctx, foo):226 assert ctx.info_name == 'other_cmd'227 click.echo(foo)228229 result = runner.invoke(cli, [])230 assert not result.exception231 assert result.output == '42\n'
modified:CHANGES+3 added
12 unmodified lines
1314(bugfix release, release date to be decided)1516- Added missing `err` parameter forwarding to the `secho` function.17- Fixed default parameters not being handled properly by the context18 invoke method. This is a backwards incompatible change if the function19 was used improperly. See :ref:`upgrade-to-3.2` for more information.2021Version 3.122-----------23
153 unmodified lines