Skip to content
20 changes: 20 additions & 0 deletions stdlib/@tests/test_cases/builtins/check_bool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing_extensions import Literal, assert_type

assert_type(bool(), Literal[False])
assert_type(bool(False), Literal[False])

Check failure on line 4 in stdlib/@tests/test_cases/builtins/check_bool.py

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"assert_type" mismatch: expected "Literal[False]" but received "bool" (reportAssertTypeFailure)
assert_type(bool(True), Literal[True])

Check failure on line 5 in stdlib/@tests/test_cases/builtins/check_bool.py

View workflow job for this annotation

GitHub Actions / pyright: Run test cases (Linux, 3.13)

"assert_type" mismatch: expected "Literal[True]" but received "bool" (reportAssertTypeFailure)
assert_type(bool(42), bool)


class Truthy:
def __bool__(self) -> Literal[True]:
return True


class Falsy:
def __bool__(self) -> Literal[False]:
return False


assert_type(bool(Truthy()), Literal[True])
assert_type(bool(Falsy()), Literal[False])
17 changes: 16 additions & 1 deletion stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -966,9 +966,24 @@ class memoryview(Sequence[_I]):
if sys.version_info >= (3, 14):
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...

class _Truthy(Protocol):
def __bool__(self) -> Literal[True]: ...

class _Falsy(Protocol):
def __bool__(self) -> Literal[False]: ...

@final
class bool(int):
def __new__(cls, o: object = False, /) -> Self: ...
@overload
def __new__(cls, o: _Truthy, /) -> Literal[True]: ...
@overload
def __new__(cls, o: _Falsy = False, /) -> Literal[False]: ...
@overload
def __new__(cls, o: object, /) -> Self: ...
@overload
def __bool__(self: Literal[True]) -> Literal[True]: ...
@overload
def __bool__(self: Literal[False]) -> Literal[False]: ...
# The following overloads could be represented more elegantly with a TypeVar("_B", bool, int),
# however mypy has a bug regarding TypeVar constraints (https://github.com/python/mypy/issues/11880).
@overload
Expand Down
Loading