rename resultcallback to result_callback
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent e3594e7 · Browse files · 6 files·+37 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+37 added−19 removed
- tests:+2 added−2 removed
- other:+35 added−17 removed
Provenance
- Commit
b1e7858- Tree
446a881- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
e3594e7
6 files changed+37 added−19 removed
modified:docs/commands.rst+2 added-2 removed
349 unmodified lines350 how to do its processing. At that point it then returns a processing351 function and returns.352 353 Where do the returned functions go? The chained multicommand can register354-a callback with :meth:`MultiCommand.resultcallback` that goes over all354+a callback with :meth:`MultiCommand.result_callback` that goes over all355 these functions and then invoke them.356 357 To make this a bit more concrete consider this example:358 3 unmodified lines362 @click.option('-i', '--input', type=click.File('r'))363 def cli(input):364 pass365 366- @cli.resultcallback()366+ @cli.result_callback()367 def process_pipeline(processors, input):368 iterator = (x.rstrip('\r\n') for x in input)369 for processor in processors:370 iterator = processor(iterator)201 unmodified linesmodified:docs/upgrading.rst+1 added-1 removed
88 unmodified lines89 90 If you do require the know which exact commands will be invoked there are91 different ways to cope with this. The first one is to let the subcommands92 all return functions and then to invoke the functions in a93-:meth:`Context.resultcallback`.93+:meth:`Context.result_callback`.94 95 96 .. _upgrade-to-2.0:97 34 unmodified linesmodified:examples/imagepipe/imagepipe.py+1 added-1 removed
18 unmodified lines19 imagepipe open -i example02.jpg blur save20 """21 22 23-@cli.resultcallback()23+@cli.result_callback()24 def process_commands(processors):25 """This result callback is invoked with an iterable of all the chained26 subcommands. As in this example each subcommand returns a function27 we can chain them together to feed one into the other, similar to how261 unmodified linesmodified:src/click/core.py+29 added-13 removed
307 unmodified lines308 #:309 #: If chaining is enabled this will be set to ``'*'`` in case310 #: any commands are executed. It is however not possible to311 #: figure out which ones. If you require this knowledge you312- #: should use a :func:`resultcallback`.312+ #: should use a :func:`result_callback`.313 self.invoked_subcommand = None314 315 if terminal_width is None and parent is not None:316 terminal_width = parent.terminal_width1045 unmodified lines1362 :param chain: if this is set to `True` chaining of multiple subcommands1363 is enabled. This restricts the form of commands in that1364 they cannot have optional arguments but it allows1365 multiple commands to be chained together.1366- :param result_callback: the result callback to attach to this multi1367- command.1366+ :param result_callback: The result callback to attach to this multi1367+ command. This can be set or changed later with the1368+ :meth:`result_callback` decorator.1369 """1370 1371 allow_extra_args = True1372 allow_interspersed_args = False19 unmodified lines1392 else:1393 subcommand_metavar = SUBCOMMAND_METAVAR1394 self.subcommand_metavar = subcommand_metavar1395 self.chain = chain1395- #: The result callback that is stored. This can be set or1396- #: overridden with the :func:`resultcallback` decorator.1397- self.result_callback = result_callback1396+ # The result callback that is stored. This can be set or1397+ # overridden with the :func:`result_callback` decorator.1398+ self._result_callback = result_callback1399 1400 if self.chain:1401 for param in self.params:1402 if isinstance(param, Argument) and not param.required:24 unmodified lines1427 def format_options(self, ctx, formatter):1428 super().format_options(ctx, formatter)1429 self.format_commands(ctx, formatter)1430 1430- def resultcallback(self, replace=False):1431+ def result_callback(self, replace=False):1432 """Adds a result callback to the command. By default if a1433 result callback is already registered this will chain them but1434 this can be disabled with the `replace` parameter. The result1435 callback is invoked with the return value of the subcommand7 unmodified lines1443 @click.option('-i', '--input', default=23)1444 def cli(input):1445 return 421446 1446- @cli.resultcallback()1447+ @cli.result_callback()1448 def process_result(result, input):1449 return result + input1450 1451 :param replace: if set to `True` an already existing result1452 callback will be removed.1453 1454+ .. versionchanged:: 8.01455+ Renamed from ``resultcallback``.1456+1457 .. versionadded:: 3.01458 """1459 1460 def decorator(f):1457- old_callback = self.result_callback1461+ old_callback = self._result_callback1462+1463 if old_callback is None or replace:1459- self.result_callback = f1464+ self._result_callback = f1465 return f1466 1467 def function(__value, *args, **kwargs):1468 return f(old_callback(__value, *args, **kwargs), *args, **kwargs)1469 1465- self.result_callback = rv = update_wrapper(function, f)1470+ self._result_callback = rv = update_wrapper(function, f)1471 return rv1472 1473 return decorator1474 1475+ def resultcallback(self, replace=False):1476+ import warnings1477+1478+ warnings.warn(1479+ "'resultcallback' has been renamed to 'result_callback'."1480+ " The old name will be removed in Click 8.1.",1481+ DeprecationWarning,1482+ stacklevel=2,1483+ )1484+ return self.result_callback(replace=replace)1485+1486 def format_commands(self, ctx, formatter):1487 """Extra format methods for multi methods that adds all the commands1488 after the options.1489 """37 unmodified lines1527 return ctx.args1528 1529 def invoke(self, ctx):1530 def _process_result(value):1515- if self.result_callback is not None:1516- value = ctx.invoke(self.result_callback, value, **ctx.params)1531+ if self._result_callback is not None:1532+ value = ctx.invoke(self._result_callback, value, **ctx.params)1533 return value1534 1535 if not ctx.protected_args:1536 if self.invoke_without_command:1122 unmodified linesmodified:tests/test_chain.py+2 added-2 removed
97 unmodified lines98 @click.group(invoke_without_command=True, chain=chain)99 def cli():100 pass101 102- @cli.resultcallback()102+ @cli.result_callback()103 def process_result(result):104 click.echo(str(result), nl=False)105 106 result = runner.invoke(cli, [])25 unmodified lines132 @click.option("-i", "--input", type=click.File("r"))133 def cli(input):134 pass135 136- @cli.resultcallback()136+ @cli.result_callback()137 def process_pipeline(processors, input):138 iterator = (x.rstrip("\r\n") for x in input)139 for processor in processors:140 iterator = processor(iterator)108 unmodified linesmodified:CHANGES.rst+2 added
204 unmodified lines205 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+- The ``MultiCommand.resultcallback`` decorator is renamed to210+ ``result_callback``. The old name is deprecated. :issue:`1160`211 212 213 Version 7.1.2214 -------------635 unmodified lines