Merge branch '8.0.x'
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 · parents 655918a, 96146c9 · Browse files · 3 files·+75 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
3 files+75 added−19 removed
- tests:+49 added−3 removed
- other:+26 added−16 removed
Provenance
- Commit
0a81393- Tree
8ccab42- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent 1
655918a- Parent 2
96146c9
3 files changed+75 added−19 removed
modified:src/click/core.py+19 added-15 removed
2190 unmodified lines2191 :param ctx: Current context.2192 :param call: If the default is a callable, call it. Disable to2193 return the callable instead.2194 2195+ .. versionchanged:: 8.0.22196+ Type casting is no longer performed when getting a default.2197+2198 .. versionchanged:: 8.0.12199 Type casting can fail in resilient parsing mode. Invalid2200 defaults will not prevent showing help text.2201 7 unmodified lines2209 2210 if value is None:2211 value = self.default2212 2210- if callable(value):2211- if not call:2212- # Don't type cast the callable.2213- return value2214-2213+ if call and callable(value):2214 value = value()2215 2217- try:2218- return self.type_cast_value(ctx, value)2219- except BadParameter:2220- if ctx.resilient_parsing:2221- return value2216+ return value2217 2223- raise2224-2218 def add_to_parser(self, parser: OptionParser, ctx: Context) -> None:2219 raise NotImplementedError()2220 2221 def consume_value(75 unmodified lines2297 2298 return False2299 2300 def process_value(self, ctx: Context, value: t.Any) -> t.Any:2308- if value is not None:2309- value = self.type_cast_value(ctx, value)2301+ value = self.type_cast_value(ctx, value)2302 2303 if self.required and self.value_is_missing(value):2304 raise MissingParameter(ctx=ctx, param=self)2305 432 unmodified lines2738 2739 if default_string:2740 extra.append(_("default: {default}").format(default=default_string))2741 2750- if isinstance(self.type, types._NumberRangeBase):2742+ if (2743+ isinstance(self.type, types._NumberRangeBase)2744+ # skip count with default range type2745+ and not (self.count and self.type.min == 0 and self.type.max is None)2746+ ):2747 range_str = self.type._describe_range()2748 2749 if range_str:2750 extra.append(range_str)108 unmodified lines2859 else:2860 value = self.flag_value2861 source = ParameterSource.COMMANDLINE2862 2863+ elif (2864+ self.multiple2865+ and value is not None2866+ and any(v is _flag_needs_value for v in value)2867+ ):2868+ value = [self.flag_value if v is _flag_needs_value else v for v in value]2869+ source = ParameterSource.COMMANDLINE2870+2871 # The value wasn't set, or used the param's default, prompt if2872 # prompting is enabled.2873 elif (2874 source in {None, ParameterSource.DEFAULT}84 unmodified linesmodified:tests/test_options.py+49 added-3 removed
250 unmodified lines251 assert opt.multiple252 assert isinstance(opt.type, click.Tuple)253 assert opt.type.types == [click.INT, click.STRING]254 ctx = click.Context(click.Command("test"))255- assert opt.get_default(ctx) == ((1, "a"),)255+ assert opt.type_cast_value(ctx, opt.get_default(ctx)) == ((1, "a"),)256 257 258 def test_parse_multiple_default_composite_type(runner):259 @click.command()53 unmodified lines313 (click.IntRange(1), "x>=1"),314 (click.IntRange(max=32), "x<=32"),315 ],316 )317-def test_intrange_default_help_text(runner, type, expect):318- option = click.Option(["--count"], type=type, show_default=True, default=2)317+def test_intrange_default_help_text(type, expect):318+ option = click.Option(["--num"], type=type, show_default=True, default=2)319 context = click.Context(click.Command("test"))320 result = option.get_help_record(context)[1]321 assert expect in result322 323 324+def test_count_default_type_help():325+ """A count option with the default type should not show >=0 in help."""326+ option = click.Option(["--count"], count=True, help="some words")327+ context = click.Context(click.Command("test"))328+ result = option.get_help_record(context)[1]329+ assert result == "some words"330+331+332+def test_file_type_help_default():333+ """The default for a File type is a filename string. The string334+ should be displayed in help, not an open file object.335+336+ Type casting is only applied to defaults in processing, not when337+ getting the default value.338+ """339+ option = click.Option(340+ ["--in"], type=click.File(), default=__file__, show_default=True341+ )342+ context = click.Context(click.Command("test"))343+ result = option.get_help_record(context)[1]344+ assert __file__ in result345+346+347 def test_toupper_envvar_prefix(runner):348 @click.command()349 @click.option("--arg")350 def cmd(arg):461 unmodified lines812 result = runner.invoke(cli, args, standalone_mode=False, catch_exceptions=False)813 assert result.return_value == expect814 815 816+def test_multiple_option_with_optional_value(runner):817+ cli = click.Command(818+ "cli",819+ params=[820+ click.Option(["-f"], is_flag=False, flag_value="flag", multiple=True),821+ click.Option(["-a"]),822+ click.Argument(["b"], nargs=-1),823+ ],824+ callback=lambda **kwargs: kwargs,825+ )826+ result = runner.invoke(827+ cli,828+ ["-f", "-f", "other", "-f", "-a", "1", "a", "b"],829+ standalone_mode=False,830+ catch_exceptions=False,831+ )832+ assert result.return_value == {833+ "f": ("flag", "other", "flag"),834+ "a": "1",835+ "b": ("a", "b"),836+ }837+838+839 def test_type_from_flag_value():840 param = click.Option(["-a", "x"], default=True, flag_value=4)841 assert param.type is click.INT842 param = click.Option(["-b", "x"], flag_value=8)20 unmodified linesmodified:CHANGES.rst+7 added-1 removed
10 unmodified lines11 12 Version 8.0.213 -------------14 15-Unreleased15+Released 2021-10-0816 17 - ``is_bool_flag`` is not set to ``True`` if ``is_flag`` is ``False``.18 :issue:`1925`19 - Bash version detection is locale independent. :issue:`1940`8 unmodified lines28 - Fix type annotation for ``type`` argument in ``prompt`` function.29 :issue:`2062`30 - Fix overline and italic styles, which were incorrectly added when31 adding underline. :pr:`2058`32+- An option with ``count=True`` will not show "[x>=0]" in help text.33+ :issue:`2072`34+- Default values are not cast to the parameter type twice during35+ processing. :issue:`2085`36+- Options with ``multiple`` and ``flag_value`` use the flag value37+ instead of leaving an internal placeholder. :issue:`2001`38 39 40 Version 8.0.141 -------------889 unmodified lines