Skip to content

Commit 968d94d

Browse files
committed
Use infer_condition_value to detect TYPE_CHECKING.
1 parent c2fec83 commit 968d94d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mypy/checker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
from mypy.patterns import AsPattern, StarredPattern
153153
from mypy.plugin import Plugin
154154
from mypy.plugins import dataclasses as dataclasses_plugin
155+
from mypy.reachability import infer_condition_value, MYPY_TRUE, MYPY_FALSE
155156
from mypy.scope import Scope
156157
from mypy.semanal import is_trivial_body, refers_to_fullname, set_callable_name
157158
from mypy.semanal_enum import ENUM_BASES, ENUM_SPECIAL_PROPS
@@ -5045,7 +5046,7 @@ def _visit_if_stmt_redundant_expr_helper(
50455046

50465047
if codes.REDUNDANT_EXPR not in self.options.enabled_error_codes:
50475048
return
5048-
if refers_to_fullname(expr, ("typing.TYPE_CHECKING", "typing_extensions.TYPE_CHECKING")):
5049+
if infer_condition_value(expr, self.options) in (MYPY_FALSE, MYPY_TRUE):
50495050
return
50505051

50515052
def _filter(body: Block | None) -> bool:

test-data/unit/check-isinstance.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,6 +3080,16 @@ while False: # E: While condition is always false
30803080

30813081
[builtins fixtures/bool.pyi]
30823082

3083+
[case testNoRedundantExpressionWarningsForUsagesOfTYPECHECKING]
3084+
# flags: --enable-error-code=redundant-expr
3085+
from typing import TYPE_CHECKING
3086+
3087+
if TYPE_CHECKING:
3088+
...
3089+
3090+
if not TYPE_CHECKING:
3091+
...
3092+
30833093
[case testNoRedundantExpressionWarningsForExhaustivenessChecks]
30843094
# flags: --enable-error-code=redundant-expr
30853095
from typing import Literal, Never, NoReturn

0 commit comments

Comments
 (0)