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

rename resultcallback to result_callback #1848

Merged
merged 1 commit into from
Apr 13, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ Unreleased
- Add all kwargs passed to ``Context.invoke()`` to ``ctx.params``.
Fixes an inconsistency when nesting ``Context.forward()`` calls.
:issue:`1568`
- The ``MultiCommand.resultcallback`` decorator is renamed to
``result_callback``. The old name is deprecated. :issue:`1160`


Version 7.1.2
Expand Down
4 changes: 2 additions & 2 deletions docs/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ how to do its processing. At that point it then returns a processing
function and returns.

Where do the returned functions go? The chained multicommand can register
a callback with :meth:`MultiCommand.resultcallback` that goes over all
a callback with :meth:`MultiCommand.result_callback` that goes over all
these functions and then invoke them.

To make this a bit more concrete consider this example:
Expand All @@ -363,7 +363,7 @@ To make this a bit more concrete consider this example:
def cli(input):
pass

@cli.resultcallback()
@cli.result_callback()
def process_pipeline(processors, input):
iterator = (x.rstrip('\r\n') for x in input)
for processor in processors:
Expand Down
2 changes: 1 addition & 1 deletion docs/upgrading.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ restored.
If you do require the know which exact commands will be invoked there are
different ways to cope with this. The first one is to let the subcommands
all return functions and then to invoke the functions in a
:meth:`Context.resultcallback`.
:meth:`Context.result_callback`.


.. _upgrade-to-2.0:
Expand Down
2 changes: 1 addition & 1 deletion examples/imagepipe/imagepipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def cli():
"""


@cli.resultcallback()
@cli.result_callback()
def process_commands(processors):
"""This result callback is invoked with an iterable of all the chained
subcommands. As in this example each subcommand returns a function
Expand Down
42 changes: 29 additions & 13 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def __init__(
#: If chaining is enabled this will be set to ``'*'`` in case
#: any commands are executed. It is however not possible to
#: figure out which ones. If you require this knowledge you
#: should use a :func:`resultcallback`.
#: should use a :func:`result_callback`.
self.invoked_subcommand = None

if terminal_width is None and parent is not None:
Expand Down Expand Up @@ -1363,8 +1363,9 @@ class MultiCommand(Command):
is enabled. This restricts the form of commands in that
they cannot have optional arguments but it allows
multiple commands to be chained together.
:param result_callback: the result callback to attach to this multi
command.
:param result_callback: The result callback to attach to this multi
command. This can be set or changed later with the
:meth:`result_callback` decorator.
"""

allow_extra_args = True
Expand Down Expand Up @@ -1392,9 +1393,9 @@ def __init__(
subcommand_metavar = SUBCOMMAND_METAVAR
self.subcommand_metavar = subcommand_metavar
self.chain = chain
#: The result callback that is stored. This can be set or
#: overridden with the :func:`resultcallback` decorator.
self.result_callback = result_callback
# The result callback that is stored. This can be set or
# overridden with the :func:`result_callback` decorator.
self._result_callback = result_callback

if self.chain:
for param in self.params:
Expand Down Expand Up @@ -1427,7 +1428,7 @@ def format_options(self, ctx, formatter):
super().format_options(ctx, formatter)
self.format_commands(ctx, formatter)

def resultcallback(self, replace=False):
def result_callback(self, replace=False):
"""Adds a result callback to the command. By default if a
result callback is already registered this will chain them but
this can be disabled with the `replace` parameter. The result
Expand All @@ -1443,30 +1444,45 @@ def resultcallback(self, replace=False):
def cli(input):
return 42

@cli.resultcallback()
@cli.result_callback()
def process_result(result, input):
return result + input

:param replace: if set to `True` an already existing result
callback will be removed.

.. versionchanged:: 8.0
Renamed from ``resultcallback``.

.. versionadded:: 3.0
"""

def decorator(f):
old_callback = self.result_callback
old_callback = self._result_callback

if old_callback is None or replace:
self.result_callback = f
self._result_callback = f
return f

def function(__value, *args, **kwargs):
return f(old_callback(__value, *args, **kwargs), *args, **kwargs)

self.result_callback = rv = update_wrapper(function, f)
self._result_callback = rv = update_wrapper(function, f)
return rv

return decorator

def resultcallback(self, replace=False):
import warnings

warnings.warn(
"'resultcallback' has been renamed to 'result_callback'."
" The old name will be removed in Click 8.1.",
DeprecationWarning,
stacklevel=2,
)
return self.result_callback(replace=replace)

def format_commands(self, ctx, formatter):
"""Extra format methods for multi methods that adds all the commands
after the options.
Expand Down Expand Up @@ -1512,8 +1528,8 @@ def parse_args(self, ctx, args):

def invoke(self, ctx):
def _process_result(value):
if self.result_callback is not None:
value = ctx.invoke(self.result_callback, value, **ctx.params)
if self._result_callback is not None:
value = ctx.invoke(self._result_callback, value, **ctx.params)
return value

if not ctx.protected_args:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_no_command_result_callback(runner, chain, expect):
def cli():
pass

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

Expand Down Expand Up @@ -133,7 +133,7 @@ def test_pipeline(runner):
def cli(input):
pass

@cli.resultcallback()
@cli.result_callback()
def process_pipeline(processors, input):
iterator = (x.rstrip("\r\n") for x in input)
for processor in processors:
Expand Down