getting default value doesn't perform type cast
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 f74dbbb · 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
Provenance
- Commit
f82ee1f- Tree
485c822- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
f74dbbb
3 files changed+25 added−16 removed
modified:src/click/core.py+6 added-14 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 638 unmodified linesmodified:tests/test_options.py+17 added-2 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()62 unmodified lines322 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(["--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"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):468 unmodified linesmodified:CHANGES.rst+2 added
20 unmodified lines21 - 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`27 28 29 Version 8.0.130 -------------889 unmodified lines