Fix flag value set when is_flag=True and type is provided (#2930)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parents 011b9f9, 4fd2fea · Browse files · 3 files·+44 added−2 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+44 added−2 removed
- tests:+39 added−0 removed
- other:+5 added−2 removed
Provenance
- Commit
884af5c- Tree
2aae790- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
011b9f9- Parent 2
4fd2fea
3 files changed+44 added−2 removed
modified:src/click/core.py+3 added-2 removed
2608 unmodified lines2609 self.default = ()2610 else:2611 self.default = False2612 2613+ if is_flag and flag_value is None:2614+ flag_value = not self.default2615+2616 self.type: types.ParamType2617 if is_flag and type is None:2615- if flag_value is None:2616- flag_value = not self.default2618 # Re-guess the type from the flag value instead of the2619 # default.2620 self.type = types.convert_type(None, flag_value)2621 514 unmodified linesmodified:tests/test_options.py+39 added
1006 unmodified lines1007 assert param.type is click.INT1008 1009 1010 @pytest.mark.parametrize(1011+ ("opts", "pass_flag", "expected"),1012+ [1013+ pytest.param({"type": bool}, False, "False"),1014+ pytest.param({"type": bool}, True, "True"),1015+ pytest.param({"type": bool, "default": True}, False, "True"),1016+ pytest.param({"type": bool, "default": True}, True, "False"),1017+ pytest.param({"type": click.BOOL}, False, "False"),1018+ pytest.param({"type": click.BOOL}, True, "True"),1019+ pytest.param({"type": click.BOOL, "default": True}, False, "True"),1020+ pytest.param({"type": click.BOOL, "default": True}, True, "False"),1021+ pytest.param({"type": str}, False, ""),1022+ pytest.param({"type": str}, True, "True"),1023+ ],1024+)1025+def test_flag_value_is_correctly_set(runner, opts, pass_flag, expected):1026+ @click.command()1027+ @click.option("--foo", is_flag=True, **opts)1028+ def cmd(foo):1029+ click.echo(foo)1030+1031+ result = runner.invoke(cmd, ["--foo"] if pass_flag else [])1032+ assert result.output == f"{expected}\n"1033+1034+1035+@pytest.mark.parametrize(1036 ("option", "expected"),1037 [1038 # Not boolean flags1039 pytest.param(Option(["-a"], type=int), False, id="int option"),24 unmodified lines1064 1065 assert message in str(e.value)1066 1067 1068+def test_non_flag_with_non_negatable_default(runner):1069+ class NonNegatable:1070+ def __bool__(self):1071+ raise ValueError("Cannot negate this object")1072+1073+ @click.command()1074+ @click.option("--foo", default=NonNegatable())1075+ def cmd(foo):1076+ pass1077+1078+ result = runner.invoke(cmd)1079+ assert result.exit_code == 01080+1081+1082 @pytest.mark.parametrize(1083 ("choices", "metavars"),1084 [1085 pytest.param(["foo", "bar"], "[TEXT]", id="text choices"),56 unmodified linesmodified:CHANGES.rst+2 added
1 unmodified line2 3 Version 8.2.14 -------------5 6+- Fix flag value handling for flag options with a provided type. :issue:`2894`7+ :issue:`2897` :pr:`2930`8 9 Version 8.2.010 -------------11 1231 unmodified lines