Skip to content
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

Support __bool__ with Literal in --warn-unreachable #15645

Merged
merged 1 commit into from
Aug 12, 2023
Merged
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
12 changes: 4 additions & 8 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,8 @@ def true_only(t: Type) -> ProperType:
else:
ret_type = _get_type_special_method_bool_ret_type(t)

if ret_type and ret_type.can_be_false and not ret_type.can_be_true:
new_t = copy_type(t)
new_t.can_be_true = False
return new_t
if ret_type and not ret_type.can_be_true:
return UninhabitedType(line=t.line, column=t.column)

new_t = copy_type(t)
new_t.can_be_false = False
Expand Down Expand Up @@ -635,10 +633,8 @@ def false_only(t: Type) -> ProperType:
else:
ret_type = _get_type_special_method_bool_ret_type(t)

if ret_type and ret_type.can_be_true and not ret_type.can_be_false:
new_t = copy_type(t)
new_t.can_be_false = False
return new_t
if ret_type and not ret_type.can_be_false:
return UninhabitedType(line=t.line)

new_t = copy_type(t)
new_t.can_be_true = False
Expand Down
32 changes: 32 additions & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,38 @@ def f() -> None:
x = 1 # E: Statement is unreachable
[builtins fixtures/dict.pyi]

[case testUnreachableLiteralFrom__bool__]
# flags: --warn-unreachable
from typing_extensions import Literal

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

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

class Maybe:
def __bool__(self) -> Literal[True | False]: ...

t = Truth()
if t:
x = 1
else:
x = 2 # E: Statement is unreachable

if Lie():
x = 3 # E: Statement is unreachable

if Maybe():
x = 4


def foo() -> bool: ...

y = Truth() or foo() # E: Right operand of "or" is never evaluated
z = Lie() and foo() # E: Right operand of "and" is never evaluated
[builtins fixtures/dict.pyi]

[case testUnreachableModuleBody1]
# flags: --warn-unreachable
from typing import NoReturn
Expand Down
Loading