Skip to content
Myr

Merge pull request #2086 from pallets/default-type-cast

Before adoption: this commit came in with the history the project adopted, so no landing records it.

getting default value doesn't perform type cast

authored by David Lord · committed by GitHub · parents f74dbbb, f82ee1f · Browse files · 3 files+25 added−16 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+25 added−16 removed

  • tests:+17 added−2 removed
  • other:+8 added−14 removed

3 files+25 added−16 removed

modified:src/click/core.py+6 added-14 removed
2190 unmodified lines
2191 :param ctx: Current context.2192 :param call: If the default is a callable, call it. Disable to2193 return the callable instead.21942195 .. versionchanged:: 8.0.22196 Type casting is no longer performed when getting a default.21972198 .. versionchanged:: 8.0.12199 Type casting can fail in resilient parsing mode. Invalid2200 defaults will not prevent showing help text.2201
7 unmodified lines
22092210 if value is None:2211 value = self.default22122210 if callable(value):2211 if not call:2212 # Don't type cast the callable.2213 return value22142213 if call and callable(value):2214 value = value()22152217 try:2218 return self.type_cast_value(ctx, value)2219 except BadParameter:2220 if ctx.resilient_parsing:2221 return value2216 return value22172223 raise22242218 def add_to_parser(self, parser: OptionParser, ctx: Context) -> None:2219 raise NotImplementedError()22202221 def consume_value(
75 unmodified lines
22972298 return False22992300 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)23022303 if self.required and self.value_is_missing(value):2304 raise MissingParameter(ctx=ctx, param=self)2305
638 unmodified lines
modified:tests/test_options.py+17 added-2 removed
250 unmodified lines
251 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"),)256257258def test_parse_multiple_default_composite_type(runner):259 @click.command()
62 unmodified lines
322323324def test_count_default_type_help():325 """A count option with the default type should not show >=0 in help."""326 option = click.Option(["--couunt"], count=True, help="some words")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"330331332def 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.335336 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 result345346347def test_toupper_envvar_prefix(runner):348 @click.command()349 @click.option("--arg")350 def cmd(arg):
468 unmodified lines
modified:CHANGES.rst+2 added
20 unmodified lines
21- Fix overline and italic styles, which were incorrectly added when22 adding underline. :pr:`2058`23- An option with ``count=True`` will not show "[x>=0]" in help text.24 :issue:`2072`25- Default values are not cast to the parameter type twice during26 processing. :issue:`2085`272829Version 8.0.130-------------
889 unmodified lines