File tree 2 files changed +36
-8
lines changed 2 files changed +36
-8
lines changed Original file line number Diff line number Diff line change @@ -602,10 +602,8 @@ def true_only(t: Type) -> ProperType:
602
602
else :
603
603
ret_type = _get_type_special_method_bool_ret_type (t )
604
604
605
- if ret_type and ret_type .can_be_false and not ret_type .can_be_true :
606
- new_t = copy_type (t )
607
- new_t .can_be_true = False
608
- return new_t
605
+ if ret_type and not ret_type .can_be_true :
606
+ return UninhabitedType (line = t .line , column = t .column )
609
607
610
608
new_t = copy_type (t )
611
609
new_t .can_be_false = False
@@ -637,10 +635,8 @@ def false_only(t: Type) -> ProperType:
637
635
else :
638
636
ret_type = _get_type_special_method_bool_ret_type (t )
639
637
640
- if ret_type and ret_type .can_be_true and not ret_type .can_be_false :
641
- new_t = copy_type (t )
642
- new_t .can_be_false = False
643
- return new_t
638
+ if ret_type and not ret_type .can_be_false :
639
+ return UninhabitedType (line = t .line )
644
640
645
641
new_t = copy_type (t )
646
642
new_t .can_be_true = False
Original file line number Diff line number Diff line change @@ -1379,6 +1379,38 @@ def f() -> None:
1379
1379
x = 1 # E: Statement is unreachable
1380
1380
[builtins fixtures/dict.pyi]
1381
1381
1382
+ [case testUnreachableLiteralFrom__bool__]
1383
+ # flags: --warn-unreachable
1384
+ from typing_extensions import Literal
1385
+
1386
+ class Truth:
1387
+ def __bool__(self) -> Literal[True]: ...
1388
+
1389
+ class Lie:
1390
+ def __bool__(self) -> Literal[False]: ...
1391
+
1392
+ class Maybe:
1393
+ def __bool__(self) -> Literal[True | False]: ...
1394
+
1395
+ t = Truth()
1396
+ if t:
1397
+ x = 1
1398
+ else:
1399
+ x = 2 # E: Statement is unreachable
1400
+
1401
+ if Lie():
1402
+ x = 3 # E: Statement is unreachable
1403
+
1404
+ if Maybe():
1405
+ x = 4
1406
+
1407
+
1408
+ def foo() -> bool: ...
1409
+
1410
+ y = Truth() or foo() # E: Right operand of "or" is never evaluated
1411
+ z = Lie() and foo() # E: Right operand of "and" is never evaluated
1412
+ [builtins fixtures/dict.pyi]
1413
+
1382
1414
[case testUnreachableModuleBody1]
1383
1415
# flags: --warn-unreachable
1384
1416
from typing import NoReturn
You can’t perform that action at this time.
0 commit comments