Ensure that `type_cast_value` never gets `UNSET` (#3090)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents 06f52a4, 6b1278f · Browse files · 4 files·+102 added−9 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+102 added−9 removed
- tests:+73 added−0 removed
- other:+29 added−9 removed
Provenance
- Commit
8d4051e- Tree
7863a78- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
06f52a4- Parent 2
6b1278f
4 files changed+102 added−9 removed
modified:src/click/core.py+27 added-9 removed
2336 unmodified lines2337 def type_cast_value(self, ctx: Context, value: t.Any) -> t.Any:2338 """Convert and validate a value against the parameter's2339 :attr:`type`, :attr:`multiple`, and :attr:`nargs`.2340 """2341- if value in (None, UNSET):2341+ if value is None:2342 if self.multiple or self.nargs == -1:2343 return ()2344 else:2345 return value74 unmodified lines2420 the :attr:`UNSET` sentinel.2421 2422 :meta private:2423 """2424+ # shelter `type_cast_value` from ever seeing an `UNSET` value by handling the2425+ # cases in which `UNSET` gets special treatment explicitly at this layer2426+ #2427+ # Refs:2428+ # https://github.com/pallets/click/issues/30692429+ if value is UNSET:2430+ if self.multiple or self.nargs == -1:2431+ value = ()2432+ else:2424- value = self.type_cast_value(ctx, value)2433+ value = self.type_cast_value(ctx, value)2434 2435 if self.required and self.value_is_missing(value):2436 raise MissingParameter(ctx=ctx, param=self)2437 826 unmodified lines3264 source = ParameterSource.PROMPT3265 3266 return value, source3267 3259- def type_cast_value(self, ctx: Context, value: t.Any) -> t.Any:3260- if self.is_flag and not self.required:3261- if value is UNSET:3262- if self.is_bool_flag:3263- # If the flag is a boolean flag, we return False if it is not set.3264- value = False3265- return super().type_cast_value(ctx, value)3268+ def process_value(self, ctx: Context, value: t.Any) -> t.Any:3269+ # process_value has to be overridden on Options in order to capture3270+ # `value == UNSET` cases before `type_cast_value()` gets called.3271+ #3272+ # Refs:3273+ # https://github.com/pallets/click/issues/30693274+ if self.is_flag and not self.required and self.is_bool_flag and value is UNSET:3275+ value = False3276 3277+ if self.callback is not None:3278+ value = self.callback(ctx, self, value)3279 3280+ return value3281+3282+ # in the normal case, rely on Parameter.process_value3283+ return super().process_value(ctx, value)3284+3285+3286 class Argument(Parameter):3287 """Arguments are positional parameters to a command. They generally3288 provide fewer features than options but can have infinite ``nargs``3289 and are required by default.96 unmodified linesmodified:tests/test_arguments.py+37 added
577 unmodified lines578 pass579 580 with pytest.warns(UserWarning):581 runner.invoke(cli, [])582+583+584+@pytest.mark.parametrize(585+ ("argument_kwargs", "pass_argv"),586+ (587+ # there is a large potential parameter space to explore here588+ # this is just a very small sample of it589+ ({}, ["myvalue"]),590+ ({"nargs": -1}, []),591+ ({"nargs": -1}, ["myvalue"]),592+ ({"default": None}, ["myvalue"]),593+ ({"required": False}, []),594+ ({"required": False}, ["myvalue"]),595+ ),596+)597+def test_argument_custom_class_can_override_type_cast_value_and_never_sees_unset(598+ runner, argument_kwargs, pass_argv599+):600+ """601+ Test that overriding type_cast_value is supported602+603+ In particular, the argument is never passed an UNSET sentinel value.604+ """605+606+ class CustomArgument(click.Argument):607+ def type_cast_value(self, ctx, value):608+ assert value is not UNSET609+ return value610+611+ @click.command()612+ @click.argument("myarg", **argument_kwargs, cls=CustomArgument)613+ def cmd(myarg):614+ click.echo("ok")615+616+ result = runner.invoke(cmd, pass_argv)617+ assert not result.exception618+ assert result.exit_code == 0modified:tests/test_options.py+36 added
1010 unmodified lines1011 assert "I am a help text" in result.output1012 assert "you wont see me" not in result.output1013 1014 1015+@pytest.mark.parametrize(1016+ ("param_decl", "option_kwargs", "pass_argv"),1017+ (1018+ # there is a large potential parameter space to explore here1019+ # this is just a very small sample of it1020+ ("--opt", {}, []),1021+ ("--opt", {"multiple": True}, []),1022+ ("--opt", {"is_flag": True}, []),1023+ ("--opt/--no-opt", {"is_flag": True, "default": None}, []),1024+ ("--req", {"is_flag": True, "required": True}, ["--req"]),1025+ ),1026+)1027+def test_option_custom_class_can_override_type_cast_value_and_never_sees_unset(1028+ runner, param_decl, option_kwargs, pass_argv1029+):1030+ """1031+ Test that overriding type_cast_value is supported1032+1033+ In particular, the option is never passed an UNSET sentinel value.1034+ """1035+1036+ class CustomOption(click.Option):1037+ def type_cast_value(self, ctx, value):1038+ assert value is not UNSET1039+ return value1040+1041+ @click.command()1042+ @click.option("myparam", param_decl, **option_kwargs, cls=CustomOption)1043+ def cmd(myparam):1044+ click.echo("ok")1045+1046+ result = runner.invoke(cmd, pass_argv)1047+ assert not result.exception1048+ assert result.exit_code == 01049+1050+1051 def test_option_custom_class_reusable(runner):1052 """Ensure we can reuse a custom class option. See Issue #926"""1053 1054 class CustomOption(click.Option):1223 unmodified linesmodified:CHANGES.rst+2 added
11 unmodified lines12 - Fix conversion of ``Sentinel.UNSET`` happening too early, which caused incorrect13 behavior for multiple parameters using the same name. :issue:`3071` :pr:`3079`14 - Fix rendering when ``prompt`` and ``confirm`` parameter ``prompt_suffix`` is15 empty. :issue:`3019` :pr:`3021`16+- When ``Sentinel.UNSET`` is found during parsing, it will skip calls to17+ ``type_cast_value``. :issue:`3069` :pr:`3090`18 19 Version 8.3.020 --------------21 1287 unmodified lines