From f3ac415f93fd4abedef31c7e36ea5dd4f8ae387b Mon Sep 17 00:00:00 2001 From: Vincent Barbaresi Date: Sat, 10 Oct 2020 14:27:23 +0200 Subject: [PATCH] disable unreachable warnings in boolean ops on TypeVars with value restriction 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 https://github.com/python/mypy/issues/9456 Using `self.chk.should_report_unreachable_issues()` honors the suppression of the unreachable warning for TypeVar with value restrictions --- mypy/checkexpr.py | 2 +- test-data/unit/check-unreachable-code.test | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 9c0e2f4048b3..758d219cfd98 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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) diff --git a/test-data/unit/check-unreachable-code.test b/test-data/unit/check-unreachable-code.test index f5b49d87289a..e95faf503d99 100644 --- a/test-data/unit/check-unreachable-code.test +++ b/test-data/unit/check-unreachable-code.test @@ -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): @@ -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] pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy