Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Version 8.2.1
-------------

- Fix flag value handling for flag options with a provided type. :issue:`2894`
:issue:`2897` :pr:`2930`

Version 8.2.0
-------------
Expand Down
5 changes: 3 additions & 2 deletions src/click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2610,10 +2610,11 @@ def __init__(
else:
self.default = False

if is_flag and flag_value is None:
flag_value = not self.default

self.type: types.ParamType
if is_flag and type is None:
if flag_value is None:
flag_value = not self.default
# Re-guess the type from the flag value instead of the
# default.
self.type = types.convert_type(None, flag_value)
Expand Down
39 changes: 39 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,31 @@ def test_type_from_flag_value():
assert param.type is click.INT


@pytest.mark.parametrize(
("opts", "pass_flag", "expected"),
[
pytest.param({"type": bool}, False, "False"),
pytest.param({"type": bool}, True, "True"),
pytest.param({"type": bool, "default": True}, False, "True"),
pytest.param({"type": bool, "default": True}, True, "False"),
pytest.param({"type": click.BOOL}, False, "False"),
pytest.param({"type": click.BOOL}, True, "True"),
pytest.param({"type": click.BOOL, "default": True}, False, "True"),
pytest.param({"type": click.BOOL, "default": True}, True, "False"),
pytest.param({"type": str}, False, ""),
pytest.param({"type": str}, True, "True"),
],
)
def test_flag_value_is_correctly_set(runner, opts, pass_flag, expected):
@click.command()
@click.option("--foo", is_flag=True, **opts)
def cmd(foo):
click.echo(foo)

result = runner.invoke(cmd, ["--foo"] if pass_flag else [])
assert result.output == f"{expected}\n"


@pytest.mark.parametrize(
("option", "expected"),
[
Expand Down Expand Up @@ -1040,6 +1065,20 @@ def test_invalid_flag_combinations(runner, kwargs, message):
assert message in str(e.value)


def test_non_flag_with_non_negatable_default(runner):
class NonNegatable:
def __bool__(self):
raise ValueError("Cannot negate this object")

@click.command()
@click.option("--foo", default=NonNegatable())
def cmd(foo):
pass

result = runner.invoke(cmd)
assert result.exit_code == 0


@pytest.mark.parametrize(
("choices", "metavars"),
[
Expand Down