Skip to content

Commit 7d3a871

Browse files
committed
group without command passes return value to result callback
1 parent bf9da48 commit 7d3a871

3 files changed

Lines changed: 11 additions & 8 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ Version 8.1.0
4545
:issue:`2131`.
4646
- ``@command`` decorator is annotated as returning the correct type if
4747
a ``cls`` argument is used. :issue:`2211`
48+
- A ``Group`` with ``invoke_without_command=True`` and ``chain=False``
49+
will invoke its result callback with the group function's return
50+
value. :issue:`2124`
4851

4952

5053
Version 8.0.4

src/click/core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,11 +1626,11 @@ def _process_result(value: t.Any) -> t.Any:
16261626
if not ctx.protected_args:
16271627
if self.invoke_without_command:
16281628
# No subcommand was invoked, so the result callback is
1629-
# invoked with None for regular groups, or an empty list
1630-
# for chained groups.
1629+
# invoked with the group return value for regular
1630+
# groups, or an empty list for chained groups.
16311631
with ctx:
1632-
super().invoke(ctx)
1633-
return _process_result([] if self.chain else None)
1632+
rv = super().invoke(ctx)
1633+
return _process_result([] if self.chain else rv)
16341634
ctx.fail(_("Missing command."))
16351635

16361636
# Fetch args back out

tests/test_chain.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,20 @@ def bdist(format):
8585
assert result.output.splitlines() == ["bdist called 1", "sdist called 2"]
8686

8787

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

9595
@click.group(invoke_without_command=True, chain=chain)
9696
def cli():
97-
pass
97+
return 1
9898

9999
@cli.result_callback()
100100
def process_result(result):
101-
click.echo(str(result), nl=False)
101+
click.echo(result, nl=False)
102102

103103
result = runner.invoke(cli, [])
104104
assert result.output == expect

0 commit comments

Comments
 (0)