-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
group return value is ignored #2124
Comments
Thanks for opening the issue, I ran a git bisect to see where this behavior changed. |
It seems related to Lines 1633 to 1638 in d5932d6
where return value of super().invoke(ctx) is replaced with [] or None before passed to _process_result . Return value propagation (without subcommand invocation) stops here with invoke_without_command=True , regardless of standalone_mode .
|
Not sure what to do with this one. The difference in behavior between groups and commands wasn't documented, and it's not clear why group returns weren't used when adding support for command return values. I think it could make sense to use the group's return value as well though. The problem is making sure existing code continues to work after the change, at least for a deprecation period. The return value of a command is the return value, if any. The return value of a group is the result of calling its
A possible solution that sidesteps the questions above: only use the group return value if invoked without subcommand. That is, if the group is treated like a command itself, return its value as if it were a command. Regardless of the solution, the behavior of return values needs to be much better documented. For now, you could add a result callback to the group that returns what you would have returned from the group if it was used without a subcommand. @main.result_callback
@click.pass_context
def group_result(ctx, value):
if ctx.invoked_subcommand is None:
return 1 |
For #1178, in 8.0 we changed the behavior so that the result callback was called even if there was no subcommand, passing Previously, the group return value was returned, as long as the group wasn't in chain mode and was called without a subcommand. In chain mode, the group return was always ignored since the intention in that mode was to have a pipeline of return values. The behavior before 8.0 was basically what I just described as a possible solution to this issue. Now that I've looked over this more, I think that makes more sense, I think the change should be reverted in 8.1. |
A compromise that should satisfy that issue and this one would be to pass the group return value to the result callback if it's invoked directly. As shown in the workaround I posted above, you can tell whether the value is from a subcommand or not based on |
Calling a command defined as
@click.group()
withstandalone_mode=False
does not propagate the return value as expected.For instance:
It does work when the command is defined with
@click.command()
, hence I suppose this is a bug.Environment:
The text was updated successfully, but these errors were encountered: