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
Provenance
- Commit
dd51a1d- Tree
9c4d1ca- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent 1
8691636- Parent 2
bb42d2e
4 files changed+113 added−9 removed
modified:click/core.py+46 added-8 removed
367 unmodified lines368 """369 return self.command.get_help(self)370 371 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:374+375+ 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.381+382+ 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 = self389 390+ # 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 = False394+395 # 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.')405 406+ 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 = True410+411 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')428 429 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 lines438 if not isinstance(cmd, Command):439 raise TypeError('Callback is not a command.')440 441 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]444 445 return self.invoke(cmd, **kwargs)446 1121 unmodified linesmodified:docs/upgrading.rst+28 added
4 unmodified lines5 this is not entirely possible. In case we need to break backwards6 compatibility this document gives you information about how to upgrade or7 handle backwards compatibility properly.8 9+.. _upgrade-to-3.2:10+11+Upgrading to 3.212+----------------13+14+Click 3.2 contains a fix for the :meth:`Context.invoke` function when used15+with other commands. The original intention of this function was to16+invoke the other command as as if it came from the command line when it17+was passed a context object instead of a function. This use was only18+documented in a single place in the documentation before and there was no19+proper explanation for the method in the API documentation.20+21+The core issue is that before 3.2 this call worked against intentions::22+23+ ctx.invoke(other_command, 'arg1', 'arg2')24+25+This was never intended to work as it does not allow Click to operate on26+the parameters. Given that this pattern was never documented and ill27+intended the decision was made to change this behavior in a bugfix release28+before it spreads by accident and developers depend on it.29+30+The correct invocation for the above command is the following::31+32+ ctx.invoke(other_command, name_of_arg1='arg1', name_of_arg2='arg2')33+34+This also allowed us to fix the issue that defaults were not handled35+properly by this function.36+37 .. _upgrade-to-2.0:38 39 Upgrading to 2.040 ----------------32 unmodified linesmodified:tests/test_commands.py+36 added-1 removed
5 unmodified lines6 def 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)11 12 @click.command()13 @click.argument('arg', type=click.INT)14 def other_cmd(arg):3 unmodified lines18 assert not result.exception19 assert result.output == '42\n'20 21 22+def 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)27+28+ @click.command()29+ @click.argument('arg', type=click.INT)30+ def other_cmd(arg):31+ click.echo(arg)32+33+ 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'37+38+39 def test_other_command_forward(runner):40 cli = click.Group()41 42 @cli.command()167 unmodified lines210 211 result = runner.invoke(cli, ['sync'])212 assert result.exception is None213 assert result.output == 'Debug is off\n'214+215+216+def test_other_command_invoke_with_defaults(runner):217+ @click.command()218+ @click.pass_context219+ def cli(ctx):220+ return ctx.invoke(other_cmd)221+222+ @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)228+229+ result = runner.invoke(cli, [])230+ assert not result.exception231+ assert result.output == '42\n'modified:CHANGES+3 added
12 unmodified lines13 14 (bugfix release, release date to be decided)15 16 - 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.20 21 Version 3.122 -----------23 153 unmodified lines