-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Analyse x.__bool__() <=> bool(x) for reachabilty #5515
Comments
This seems like something that should be handled in the typeshed stubs, not in special-case logic within a type checker. The def __new__(cls, __o: object = ...) -> Self: ... It could be defined as an overload that returns |
I think pyright has some issues with defining the new method of bool like that # builtins.pyi
class _Truthy(Protocol):
def __bool__(self) -> Literal[True]: ...
class _Falsy(Protocol):
def __bool__(self) -> Literal[False]: ...
@final
class bool(int):
@overload
def __new__(cls, __o: _Truthy) -> Literal[True]: ...
@overload
def __new__(cls, __o: _Falsy) -> Literal[False]: ...
@overload
def __new__(cls, __o: object = ...) -> bool: ... # test.py
class MissingSentinel(Any if TYPE_CHECKING else object):
__slots__ = ()
def __eq__(self, other: object) -> Literal[False]:
return False
def __bool__(self) -> Literal[False]:
return False
def __hash__(self) -> Literal[0]:
return 0
def __repr__(self) -> Literal["..."]:
return "..."
MISSING: Final = MissingSentinel()
reveal_type(MISSING.__bool__) # type is () -> Literal[False]
reveal_type(bool(MISSING.__bool__())) # revealed type is bool?
reveal_type(bool(MISSING)) # revealed type is Literal[True]??? |
This appears to be caused by the weird bases but I'll run this change by the typeshed folk cause it seems to mostly work apart from this |
Also adding to this the types of |
When you define a class that derives from |
This class is for a private sentinel that shouldn't be exposed to users and can be assigned to any type where things like None don't make sense as a default. |
Is your feature request related to a problem? Please describe.
Currently objects which are marked as returning Literal[False/True] do not actually have their return types used for reachabilty analysis, there is also no way to get the return type using
bool(x)
which should act an alias forx.__bool__()
(or maybetype(x).__bool__(x)
if you'd prefer 😉)Describe the solution you'd like
I'd like to have the return of
__bool__
be used for type checking and also havebool()
call the methods bool to get the return type and not just have it bebool
when it could be more specific.Additional context
MyPy is implementing a similar feature and I've found it very successful at finding bugs python/mypy#15645
The text was updated successfully, but these errors were encountered: