Merge branch '3.x-maintenance'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parents 5cf7f2d, 20c9ccb · Browse files · 6 files·+114 added−51 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+114 added−51 removed
- tests:+30 added−2 removed
- other:+84 added−49 removed
Provenance
- Commit
733d5fa- Tree
7c8eb22- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent 1
5cf7f2d- Parent 2
20c9ccb
6 files changed+114 added−51 removed
modified:click/core.py+43 added-40 removed
180 unmodified lines181 and parent.default_map is not None:182 default_map = parent.default_map.get(info_name)183 self.default_map = default_map184 185- #: This is similar to :attr:`invoked_subcommand` but it holds a186- #: list of multiple subcommand names. This is useful if multi command187- #: chaining is enabled.185+ #: This flag indicates if a subcommand is going to be executed. A186+ #: group callback can use this information to figure out if it's187+ #: being executed directly or because the execution flow passes188+ #: onwards to a subcommand. By default it's None, but it can be189+ #: the name of the subcommand to execute.190 #:189- #: This property and :attr:`invoked_subcommand` are synchronized.190- #:191- #: .. versionadded:: 3.0192- self.invoked_subcommands = []191+ #: If chaining is enabled this will be set to ``'*'`` in case192+ #: any commands are executed. It is however not possible to193+ #: figure out which ones. If you require this knowledge you194+ #: should use a :func:`resultcallback`.195+ self.invoked_subcommand = None196 197 if terminal_width is None and parent is not None:198 terminal_width = parent.terminal_width199 #: The width of the terminal (None is autodetection).65 unmodified lines265 self._depth -= 1266 if self._depth == 0:267 self.close()268 266- def _get_invoked_subcommand(self):267- """This flag indicates if a subcommand is going to be executed. A268- group callback can use this information to figure out if it's269- being executed directly or because the execution flow passes270- onwards to a subcommand. By default it's `None`, but it can be271- the name of the subcommand to execute.269+ def _get_invoked_subcommands(self):270+ from warnings import warn271+ warn(Warning('This API does not work properly and has been largely '272+ 'removed in Click 3.2 to fix a regression the '273+ 'introduction of this API caused. Consult the '274+ 'upgrade documentation for more information. For '275+ 'more information about this see '276+ 'http://click.pocoo.org/upgrading/#upgrading-to-3.2'),277+ stacklevel=2)278+ if self.invoked_subcommand is None:279+ return []280+ return [self.invoked_subcommand]281+ def _set_invoked_subcommands(self, value):282+ self.invoked_subcommand = \283+ len(value) > 1 and '*' or value and value[0] or None284+ invoked_subcommands = property(_get_invoked_subcommands,285+ _set_invoked_subcommands)286+ del _get_invoked_subcommands, _set_invoked_subcommands287 273- If chaining is enabled and more than one subcommand is invoked274- then the value will be the string ``'*'``. For chaining you275- should be using :attr:`invoked_subcommands` instead.276- """277- if len(self.invoked_subcommands) == 1:278- return self.invoked_subcommands[0]279- if len(self.invoked_subcommands) > 1:280- return '*'281- def _set_invoked_subcommand(self, value):282- if value is not None:283- value = [value]284- self.invoked_subcommands = value285- invoked_subcommand = property(_get_invoked_subcommand,286- _set_invoked_subcommand)287- del _get_invoked_subcommand, _set_invoked_subcommand288-288 def make_formatter(self):289 """Creates the formatter for the help and usage output."""290 return HelpFormatter(width=self.terminal_width)291 649 unmodified lines941 if not self.chain:942 # Make sure the context is entered so we do not clean up943 # resources until the result processor has worked.944 with ctx:945+ cmd_name, cmd, args = self.resolve_command(ctx, args)946+ ctx.invoked_subcommand = cmd_name947 Command.invoke(self, ctx)947- sub_ctx = self.handle_subcommand(ctx, args)948- ctx.invoked_subcommands = [sub_ctx.info_name]948+ sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)949 with sub_ctx:950 return _process_result(sub_ctx.command.invoke(sub_ctx))951 952- # Make sure the context is entered so we do not clean up953- # resources until the result processor has worked.952+ # In chain mode we create the contexts step by step, but after the953+ # base command has been invoked. Because at that point we do not954+ # know the subcommands yet, the invoked subcommand attribute is955+ # set to ``*`` to inform the command that subcommands are executed956+ # but nothing else.957 with ctx:958+ ctx.invoked_subcommand = args and '*' or None959 Command.invoke(self, ctx)960 961 # Otherwise we make every single context and invoke them in a962 # chain. In that case the return value to the result processor963 # is the list of all invoked subcommand's results.964 contexts = []965 while args:966+ cmd_name, cmd, args = self.resolve_command(ctx, args)967+ sub_ctx = cmd.make_context(cmd_name, args, parent=ctx,962- sub_ctx = self.handle_subcommand(ctx, args, allow_extra_args=True,963- allow_interspersed_args=False)968+ allow_extra_args=True,969+ allow_interspersed_args=False)970 contexts.append(sub_ctx)971 args = sub_ctx.args972 967- # Now that we have all contexts, we can invoke them.968- ctx.invoked_subcommands = [x.info_name for x in contexts]969-973 rv = []974 for sub_ctx in contexts:975 with sub_ctx:976 rv.append(sub_ctx.command.invoke(sub_ctx))977 return _process_result(rv)978 976- def handle_subcommand(self, ctx, args, **extra):979+ def resolve_command(self, ctx, args):980 cmd_name = make_str(args[0])981 original_cmd_name = cmd_name982 983 # Get the command15 unmodified lines999 if split_opt(cmd_name)[0]:1000 self.parse_args(ctx, ctx.args)1001 ctx.fail('No such command "%s".' % original_cmd_name)1002 1000- return cmd.make_context(cmd_name, args[1:], parent=ctx, **extra)1003+ return cmd_name, cmd, args[1:]1004 1005 def get_command(self, ctx, cmd_name):1006 """Given a context and a command name, this returns a1007 :class:`Command` object if it exists or returns `None`.582 unmodified linesmodified:docs/commands.rst+8 added-9 removed
166 unmodified lines167 @cli.command()168 def sync():169 click.echo('The subcommand')170 171-And how it works in practice::171+And how it works in practice:172 173- $ python tool.py174- I was invoked without subcommand175- $ python tool.py sync176- I am about to invoke sync177- The subcommand173+.. click:run::174 175+ invoke(cli, prog_name='tool', args=[])176+ invoke(cli, prog_name='tool', args=['sync'])177+178 .. _custom-multi-commands:179 180 Custom Multi Commands181 ---------------------133 unmodified lines315 normal.316 317 Another note: the :attr:`Context.invoked_subcommand` attribute is a bit318 useless for multi commands as it will give ``'*'`` as value if more than320-one command is invoked. Instead you should be using the321-:attr:`Context.invoked_subcommands` attribute instead which is a list322-(note the trailing "s").319+one command is invoked. This is necessary because the handling of320+subcommands happens one after another so the exact subcommands that will321+be handled are not yet available when the callback fires.322 323 324 Multi Command Pipelines325 -----------------------227 unmodified linesmodified:docs/upgrading.rst+28 added
9 unmodified lines10 11 Upgrading to 3.212 ----------------13 14+Click 3.2 had to perform two changes to multi commands which were15+triggered by a change between Click 2 and Click 3 that had bigger16+consequences than anticipated.17+18+Context Invokes19+```````````````20+21 Click 3.2 contains a fix for the :meth:`Context.invoke` function when used22 with other commands. The original intention of this function was to23 invoke the other command as as if it came from the command line when it24 was passed a context object instead of a function. This use was only15 unmodified lines40 41 This also allowed us to fix the issue that defaults were not handled42 properly by this function.43 44+Multicommand Chaining API45+`````````````````````````46+47+Click 3 introduced multicommand chaning. This required a change in how48+Click internally dispatches. Unfortunately this change was not correctly49+implemented and it appeared that it was possible to provide an API that50+can inform the super command about all the subcommands that will be51+invoked.52+53+This assumption however does not work with one of the API guarantees that54+have been given in the past. As such this functionality has been removed55+in 3.2 as it was already broken. Instead the accidentally broken56+functionality of the :attr:`Context.invoked_subcommand` attribute was57+restored.58+59+If you do require the know which exact commands will be invoked there are60+different ways to cope with this. The first one is to let the subcommands61+all return functions and then to invoke the functions in a62+:meth:`Context.resultcallback`.63+64+65 .. _upgrade-to-2.0:66 67 Upgrading to 2.068 ----------------32 unmodified linesmodified:tests/test_chain.py+7 added-2 removed
101 unmodified lines102 'sdist called 2',103 ]104 105 106-def test_context_subcommand_info_sync():106+def test_context_subcommand_info_sync(recwarn):107 @click.command()108 def cli():109 pass110 111+ def _assert_warning():112+ assert 'removed in Click 3.2' in str(recwarn.pop(Warning).message)113+114 ctx = click.Context(cli, info_name='cli')115 116 assert ctx.invoked_subcommand is None117 10 unmodified lines128 assert ctx.invoked_subcommands == []129 130 ctx.invoked_subcommands = ['foo', 'bar']131 assert ctx.invoked_subcommand == '*'129- assert ctx.invoked_subcommands == ['foo', 'bar']132+ assert ctx.invoked_subcommands == ['*']133 134+ assert 'removed in Click 3.2' in str(recwarn.pop(Warning).message)135 136+137 def test_pipeline(runner):138 @click.group(chain=True, invoke_without_command=True)139 @click.option('-i', '--input', type=click.File('r'))140 def cli(input):45 unmodified linesmodified:tests/test_commands.py+23 added
227 unmodified lines228 229 result = runner.invoke(cli, [])230 assert not result.exception231 assert result.output == '42\n'232+233+234+def test_invoked_subcommand(runner):235+ @click.group(invoke_without_command=True)236+ @click.pass_context237+ def cli(ctx):238+ if ctx.invoked_subcommand is None:239+ click.echo('no subcommand, use default')240+ ctx.invoke(sync)241+ else:242+ click.echo('invoke subcommand')243+244+ @cli.command()245+ def sync():246+ click.echo('in subcommand')247+248+ result = runner.invoke(cli, ['sync'])249+ assert not result.exception250+ assert result.output == 'invoke subcommand\nin subcommand\n'251+252+ result = runner.invoke(cli)253+ assert not result.exception254+ assert result.output == 'no subcommand, use default\nin subcommand\n'modified:CHANGES+5 added
20 unmodified lines21 - Added missing `err` parameter forwarding to the `secho` function.22 - Fixed default parameters not being handled properly by the context23 invoke method. This is a backwards incompatible change if the function24 was used improperly. See :ref:`upgrade-to-3.2` for more information.25+- Removed the `invoked_subcommands` attribute largely. It is not possible26+ to provide it to work error free due to how the parsing works so this27+ API has been deprecated. See :ref:`upgrade-to-3.2` for more information.28+- Restored the functionality of `invoked_subcommand` which was broken as29+ a regression in 3.1.30 31 Version 3.132 -----------33 153 unmodified lines