Skip to content

Commit e21214f

Browse files
authored
disable unreachable warnings in boolean ops on TypeVars with value restriction (#9572)
This paragraph explains the limitation with TypeVars: https://github.com/python/mypy/blob/eb50379defc13cea9a8cbbdc0254a578ef6c415e/mypy/checker.py#L967-#L974 We currently have no way of checking for all the type expansions, and it's causing the issue #9456 Using `self.chk.should_report_unreachable_issues()` honors the suppression of the unreachable warning for TypeVar with value restrictions
1 parent 6077dc8 commit e21214f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2762,7 +2762,7 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
27622762
# the analysis from the semanal phase below. We assume that nodes
27632763
# marked as unreachable during semantic analysis were done so intentionally.
27642764
# So, we shouldn't report an error.
2765-
if self.chk.options.warn_unreachable:
2765+
if self.chk.should_report_unreachable_issues():
27662766
if right_map is None:
27672767
self.msg.unreachable_right_operand(e.op, e.right)
27682768

test-data/unit/check-unreachable-code.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ from typing import TypeVar, Generic
925925

926926
T1 = TypeVar('T1', bound=int)
927927
T2 = TypeVar('T2', int, str)
928+
T3 = TypeVar('T3', None, str)
928929

929930
def test1(x: T1) -> T1:
930931
if isinstance(x, int):
@@ -961,6 +962,19 @@ class Test3(Generic[T2]):
961962
# Same issue as above
962963
reveal_type(self.x)
963964

965+
966+
class Test4(Generic[T3]):
967+
def __init__(self, x: T3):
968+
# https://github.com/python/mypy/issues/9456
969+
# On TypeVars with value restrictions, we currently have no way
970+
# of checking a statement for all the type expansions.
971+
# Thus unreachable warnings are disabled
972+
if x and False:
973+
pass
974+
# This test should fail after this limitation is removed.
975+
if False and x:
976+
pass
977+
964978
[builtins fixtures/isinstancelist.pyi]
965979

966980
[case testUnreachableFlagContextManagersNoSuppress]

0 commit comments

Comments
 (0)