Skip to content
Myr

Merge pull request #1840 from MLH-Fellowship/context-forward

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

Fixed inconsistency in forwarding params in Context.forward

authored by David Lord · committed by GitHub · parents 6c76244, 0435112 · Browse files · 3 files+37 added−0 removed
This commit

State

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

Diff stats

3 files+37 added−0 removed

  • tests:+22 added−0 removed
  • other:+15 added−0 removed

3 files+37 added−0 removed

modified:src/click/core.py+12 added
676 unmodified lines
677 Note that before Click 3.2 keyword arguments were not properly filled678 in against the intention of this code and no context was created. For679 more information about this change and why it was done in a bugfix680 release see :ref:`upgrade-to-3.2`.681682 .. versionchanged:: 8.0683 All ``kwargs`` are tracked in :attr:`params` so they will be684 passed if :meth:`forward` is called at multiple levels.685 """686 self, callback = args[:2]687 ctx = self688
14 unmodified lines
703 for param in other_cmd.params:704 if param.name not in kwargs and param.expose_value:705 kwargs[param.name] = param.get_default(ctx)706707 # Track all kwargs as params, so that forward() will pass708 # them on in subsequent calls.709 ctx.params.update(kwargs)710711 args = args[2:]712 with augment_usage_errors(self):713 with ctx:714 return callback(*args, **kwargs)
1 unmodified line
716 def forward(*args, **kwargs): # noqa: B902717 """Similar to :meth:`invoke` but fills in default keyword718 arguments from the current context if the other command expects719 it. This cannot invoke callbacks directly, only other commands.720721 .. versionchanged:: 8.0722 All ``kwargs`` are tracked in :attr:`params` so they will be723 passed if ``forward`` is called at multiple levels.724 """725 self, cmd = args[:2]726727 # Can only forward to other commands, not direct callbacks.
1915 unmodified lines
modified:tests/test_commands.py+22 added
37 unmodified lines
38 assert not result.exception39 assert result.output == "Count: 1\nCount: 42\n"404142def test_forwarded_params_consistency(runner):43 cli = click.Group()4445 @cli.command()46 @click.option("-a")47 @click.pass_context48 def first(ctx, **kwargs):49 click.echo(f"{ctx.params}")5051 @cli.command()52 @click.option("-a")53 @click.option("-b")54 @click.pass_context55 def second(ctx, **kwargs):56 click.echo(f"{ctx.params}")57 ctx.forward(first)5859 result = runner.invoke(cli, ["second", "-a", "foo", "-b", "bar"])60 assert not result.exception61 assert result.output == "{'a': 'foo', 'b': 'bar'}\n{'a': 'foo', 'b': 'bar'}\n"626364def test_auto_shorthelp(runner):65 @click.group()66 def cli():67 pass
271 unmodified lines
modified:CHANGES.rst+3 added
201 unmodified lines
202- Writing invalid characters to ``stderr`` when using the test runner203 does not raise a ``UnicodeEncodeError``. :issue:`848`204- Fix an issue where ``readline`` would clear the entire ``prompt()``205 line instead of only the input when pressing backspace. :issue:`665`206- Add all kwargs passed to ``Context.invoke()`` to ``ctx.params``.207 Fixes an inconsistency when nesting ``Context.forward()`` calls.208 :issue:`1568`209210211Version 7.1.2212-------------
635 unmodified lines