Skip to content
Myr

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

3 files+44 added−2 removed

modified:src/click/core.py+3 added-2 removed
2608 unmodified lines
2609 self.default = ()2610 else:2611 self.default = False26122613 if is_flag and flag_value is None:2614 flag_value = not self.default26152616 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 lines
modified:tests/test_options.py+39 added
1006 unmodified lines
1007 assert param.type is click.INT100810091010@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)1025def 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)10301031 result = runner.invoke(cmd, ["--foo"] if pass_flag else [])1032 assert result.output == f"{expected}\n"103310341035@pytest.mark.parametrize(1036 ("option", "expected"),1037 [1038 # Not boolean flags1039 pytest.param(Option(["-a"], type=int), False, id="int option"),
24 unmodified lines
10641065 assert message in str(e.value)106610671068def test_non_flag_with_non_negatable_default(runner):1069 class NonNegatable:1070 def __bool__(self):1071 raise ValueError("Cannot negate this object")10721073 @click.command()1074 @click.option("--foo", default=NonNegatable())1075 def cmd(foo):1076 pass10771078 result = runner.invoke(cmd)1079 assert result.exit_code == 0108010811082@pytest.mark.parametrize(1083 ("choices", "metavars"),1084 [1085 pytest.param(["foo", "bar"], "[TEXT]", id="text choices"),
56 unmodified lines
modified:CHANGES.rst+2 added
1 unmodified line
23Version 8.2.14-------------56- Fix flag value handling for flag options with a provided type. :issue:`2894`7 :issue:`2897` :pr:`2930`89Version 8.2.010-------------11
1231 unmodified lines