Skip to content

Commit

Permalink
Only try to set flag_value if is_flag is true (#2829)
Browse files Browse the repository at this point in the history
Co-authored-by: Andreas Backx <[email protected]>
  • Loading branch information
lpsinger and AndreasBackx authored Dec 22, 2024
1 parent 88328fc commit b5464b7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ Unreleased
- Add a ``catch_exceptions`` parameter to :class:`CliRunner`. If
``catch_exceptions`` is not passed to :meth:`CliRunner.invoke`,
the value from :class:`CliRunner`. :issue:`2817` :pr:`2818`
- ``Option.flag_value`` will no longer have a default value set based on
``Option.default`` if ``Option.is_flag`` is ``False``. This results in
``Option.default`` not needing to implement `__bool__`. :pr:`2829`

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

if 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
2 changes: 1 addition & 1 deletion tests/test_info_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"help": None,
"prompt": None,
"is_flag": False,
"flag_value": False,
"flag_value": None,
"count": False,
"hidden": False,
},
Expand Down

0 comments on commit b5464b7

Please sign in to comment.