Skip to content

[Experiment] Add more precise overloads for bool.__new__. #12940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 7 additions & 4 deletions stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,22 @@ class SupportsNext(Protocol[_T_co]):
class SupportsAnext(Protocol[_T_co]):
def __anext__(self) -> Awaitable[_T_co]: ...

class SupportsBool(Protocol):
def __bool__(self) -> bool: ...

# Comparison protocols

class SupportsDunderLT(Protocol[_T_contra]):
def __lt__(self, other: _T_contra, /) -> bool: ...
def __lt__(self, other: _T_contra, /) -> SupportsBool: ...

class SupportsDunderGT(Protocol[_T_contra]):
def __gt__(self, other: _T_contra, /) -> bool: ...
def __gt__(self, other: _T_contra, /) -> SupportsBool: ...

class SupportsDunderLE(Protocol[_T_contra]):
def __le__(self, other: _T_contra, /) -> bool: ...
def __le__(self, other: _T_contra, /) -> SupportsBool: ...

class SupportsDunderGE(Protocol[_T_contra]):
def __ge__(self, other: _T_contra, /) -> bool: ...
def __ge__(self, other: _T_contra, /) -> SupportsBool: ...

class SupportsAllComparisons(
SupportsDunderLT[Any], SupportsDunderGT[Any], SupportsDunderLE[Any], SupportsDunderGE[Any], Protocol
Expand Down
8 changes: 7 additions & 1 deletion stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ from _typeshed import (
SupportsAdd,
SupportsAiter,
SupportsAnext,
SupportsBool,
SupportsDivMod,
SupportsFlush,
SupportsIter,
Expand Down Expand Up @@ -908,7 +909,12 @@ class memoryview(Sequence[_I]):

@final
class bool(int):
def __new__(cls, o: object = ..., /) -> Self: ...
@overload
def __new__(cls) -> Literal[False]: ...
@overload
def __new__(cls, o: SupportsBool | Sized, /) -> bool: ...
@overload
def __new__(cls, o: object, /) -> Literal[True]: ...
# 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