Merge pull request #1916 from pallets/default-resilient
Before adoption: this commit came in with the history the project adopted, so no landing records it.
show help text with invalid default
authored by David Lord · committed by GitHub · parents 1b49159, 985ca16 · Browse files · 4 files·+44 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+44 added−5 removed
- tests:+18 added−1 removed
- other:+26 added−4 removed
Provenance
- Commit
fcf8205- Tree
916c2b9- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
1b49159- Parent 2
985ca16
4 files changed+44 added−5 removed
modified:src/click/core.py+24 added-4 removed
2194 unmodified lines2195 :param ctx: Current context.2196 :param call: If the default is a callable, call it. Disable to2197 return the callable instead.2198 2199+ .. versionchanged:: 8.0.12200+ Type casting can fail in resilient parsing mode. Invalid2201+ defaults will not prevent showing help text.2202+2203 .. versionchanged:: 8.02204 Looks at ``ctx.default_map`` first.2205 2206 .. versionchanged:: 8.010 unmodified lines2217 return value2218 2219 value = value()2220 2221+ try:2217- return self.type_cast_value(ctx, value)2222+ return self.type_cast_value(ctx, value)2223+ except BadParameter:2224+ if ctx.resilient_parsing:2225+ return value2226 2227+ raise2228+2229 def add_to_parser(self, parser: OptionParser, ctx: Context) -> None:2230 raise NotImplementedError()2231 2232 def consume_value(476 unmodified lines2709 else ", ".join(str(d) for d in envvar)2710 )2711 extra.append(_("env var: {var}").format(var=var_str))2712 2713+ # Temporarily enable resilient parsing to avoid type casting2714+ # failing for the default. Might be possible to extend this to2715+ # help formatting in general.2716+ resilient = ctx.resilient_parsing2717+ ctx.resilient_parsing = True2718+2719+ try:2703- default_value = self.get_default(ctx, call=False)2720+ default_value = self.get_default(ctx, call=False)2721+ finally:2722+ ctx.resilient_parsing = resilient2723+2724 show_default_is_str = isinstance(self.show_default, str)2725 2726 if show_default_is_str or (2727 default_value is not None and (self.show_default or ctx.show_default)2728 ):2729 if show_default_is_str:2710- default_string: t.Union[str, t.Any] = f"({self.show_default})"2730+ default_string = f"({self.show_default})"2731 elif isinstance(default_value, (list, tuple)):2732 default_string = ", ".join(str(d) for d in default_value)2733 elif callable(default_value):2734 default_string = _("(dynamic)")3 unmodified lines2738 default_string = split_opt(2739 (self.opts if self.default else self.secondary_opts)[0]2740 )[1]2741 else:2722- default_string = default_value2742+ default_string = str(default_value)2743 2744 extra.append(_("default: {default}").format(default=default_string))2745 2746 if isinstance(self.type, types._NumberRangeBase):204 unmodified linesmodified:tests/test_basic.py+17 added
545 unmodified lines546 547 result = runner.invoke(cli, ["--help"])548 assert "Summary line without period" in result.output549 assert "Here is a sentence." not in result.output550+551+552+def test_help_invalid_default(runner):553+ cli = click.Command(554+ "cli",555+ params=[556+ click.Option(557+ ["-a"],558+ type=click.Path(exists=True),559+ default="not found",560+ show_default=True,561+ ),562+ ],563+ )564+ result = runner.invoke(cli, ["--help"])565+ assert result.exit_code == 0566+ assert "default: not found" in result.outputmodified:tests/test_options.py+1 added-1 removed
551 unmodified lines552 553 554 def test_argument_custom_class(runner):555 class CustomArgument(click.Argument):556- def get_default(self, ctx):556+ def get_default(self, ctx, call=True):557 """a dumb override of a default value for testing"""558 return "I am a default"559 560 @click.command()205 unmodified linesmodified:CHANGES.rst+2 added
23 unmodified lines24 ``command.name``, fixing an unintended change to help text and25 ``default_map`` lookups. When using patterns like ``AliasedGroup``,26 override ``resolve_command`` to change the name that is returned if27 needed. :issue:`1895`28+- If a default value is invalid, it does not prevent showing help29+ text. :issue:`1889`30 31 32 Version 8.0.033 -------------856 unmodified lines