Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

group without command passes return value to result callback #2217

Merged
merged 1 commit into from Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -45,6 +45,9 @@ Version 8.1.0
:issue:`2131`.
- ``@command`` decorator is annotated as returning the correct type if
a ``cls`` argument is used. :issue:`2211`
- A ``Group`` with ``invoke_without_command=True`` and ``chain=False``
will invoke its result callback with the group function's return
value. :issue:`2124`


Version 8.0.4
Expand Down
8 changes: 4 additions & 4 deletions src/click/core.py
Expand Up @@ -1626,11 +1626,11 @@ def _process_result(value: t.Any) -> t.Any:
if not ctx.protected_args:
if self.invoke_without_command:
# No subcommand was invoked, so the result callback is
# invoked with None for regular groups, or an empty list
# for chained groups.
# invoked with the group return value for regular
# groups, or an empty list for chained groups.
with ctx:
super().invoke(ctx)
return _process_result([] if self.chain else None)
rv = super().invoke(ctx)
return _process_result([] if self.chain else rv)
ctx.fail(_("Missing command."))

# Fetch args back out
Expand Down
8 changes: 4 additions & 4 deletions tests/test_chain.py
Expand Up @@ -85,20 +85,20 @@ def bdist(format):
assert result.output.splitlines() == ["bdist called 1", "sdist called 2"]


@pytest.mark.parametrize(("chain", "expect"), [(False, "None"), (True, "[]")])
@pytest.mark.parametrize(("chain", "expect"), [(False, "1"), (True, "[]")])
def test_no_command_result_callback(runner, chain, expect):
"""When a group has ``invoke_without_command=True``, the result
callback is always invoked. A regular group invokes it with
``None``, a chained group with ``[]``.
its return value, a chained group with ``[]``.
"""

@click.group(invoke_without_command=True, chain=chain)
def cli():
pass
return 1

@cli.result_callback()
def process_result(result):
click.echo(str(result), nl=False)
click.echo(result, nl=False)

result = runner.invoke(cli, [])
assert result.output == expect
Expand Down