diff --git a/CHANGES.md b/CHANGES.md index c466d22cf..a07231649 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ Unreleased +- `BOOL` converts `0` and `1` to `False` and `True` again instead of raising + `AttributeError`. Regression introduced in `8.2.2` by {pr}`2956`. + {issue}`3846` + ## Version 8.5.0 Released 2026-08-24 diff --git a/src/click/types.py b/src/click/types.py index 0eb84621d..0b0bb4ae4 100644 --- a/src/click/types.py +++ b/src/click/types.py @@ -852,9 +852,9 @@ def str_to_bool(value: str | bool) -> bool | None: Returns `None` if the value does not match any known boolean state. """ - if isinstance(value, bool): - return value - return BoolParamType.bool_states.get(value.strip().lower()) + if value in {False, True}: + return bool(value) + return BoolParamType.bool_states.get(str(value).strip().lower()) def convert( self, value: t.Any, param: Parameter | None, ctx: Context | None diff --git a/tests/test_basic.py b/tests/test_basic.py index 3ab9654b1..5d0e50200 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -321,6 +321,8 @@ def cli(on): (True, [], True), (False, ["--f"], True), (False, [], False), + (1, [], True), + (0, [], False), # Boolean flags have a 3-states logic. # See: https://github.com/pallets/click/issues/3024#issue-3285556668 (None, ["--f"], True),