Skip to content

Commit

Permalink
disable unreachable warnings in boolean ops on TypeVars with value re…
Browse files Browse the repository at this point in the history
…striction (#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
  • Loading branch information
vbarbaresi committed Oct 18, 2020
1 parent 6077dc8 commit e21214f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2762,7 +2762,7 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
# the analysis from the semanal phase below. We assume that nodes
# marked as unreachable during semantic analysis were done so intentionally.
# So, we shouldn't report an error.
if self.chk.options.warn_unreachable:
if self.chk.should_report_unreachable_issues():
if right_map is None:
self.msg.unreachable_right_operand(e.op, e.right)

Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ from typing import TypeVar, Generic

T1 = TypeVar('T1', bound=int)
T2 = TypeVar('T2', int, str)
T3 = TypeVar('T3', None, str)

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


class Test4(Generic[T3]):
def __init__(self, x: T3):
# https://github.com/python/mypy/issues/9456
# On TypeVars with value restrictions, we currently have no way
# of checking a statement for all the type expansions.
# Thus unreachable warnings are disabled
if x and False:
pass
# This test should fail after this limitation is removed.
if False and x:
pass

[builtins fixtures/isinstancelist.pyi]

[case testUnreachableFlagContextManagersNoSuppress]
Expand Down

0 comments on commit e21214f

Please sign in to comment.