Fixed inconsistency in forwarding params
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Saif807380 · committed by David Lord · parent 6c76244 · 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
Provenance
- Commit
0435112- Tree
8c165fb- Author
Saif807380 <saif1204kazi@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
6c76244
3 files changed+37 added−0 removed
modified:src/click/core.py+12 added
676 unmodified lines677 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`.681+682+ .. 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 lines703 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)706 707+ # Track all kwargs as params, so that forward() will pass708+ # them on in subsequent calls.709+ ctx.params.update(kwargs)710+711 args = args[2:]712 with augment_usage_errors(self):713 with ctx:714 return callback(*args, **kwargs)1 unmodified line716 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.720+721+ .. 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]726 727 # Can only forward to other commands, not direct callbacks.1915 unmodified linesmodified:tests/test_commands.py+22 added
37 unmodified lines38 assert not result.exception39 assert result.output == "Count: 1\nCount: 42\n"40 41 42+def test_forwarded_params_consistency(runner):43+ cli = click.Group()44+45+ @cli.command()46+ @click.option("-a")47+ @click.pass_context48+ def first(ctx, **kwargs):49+ click.echo(f"{ctx.params}")50+51+ @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)58+59+ 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"62+63+64 def test_auto_shorthelp(runner):65 @click.group()66 def cli():67 pass271 unmodified linesmodified:CHANGES.rst+3 added
201 unmodified lines202 - 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`209 210 211 Version 7.1.2212 -------------635 unmodified lines