Skip to content

Commit 6e97289

Browse files
committed
Fix redundant-expr for comprehensions
1 parent 3cda7db commit 6e97289

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

mypy/checkexpr.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5808,9 +5808,13 @@ def check_for_comp(self, e: GeneratorExpr | DictionaryComprehension) -> None:
58085808
self.chk.push_type_map(true_map)
58095809

58105810
if codes.REDUNDANT_EXPR in self.chk.options.enabled_error_codes:
5811-
if true_map is None:
5811+
if true_map is None or any(
5812+
isinstance(get_proper_type(t), UninhabitedType) for t in true_map.values()
5813+
):
58125814
self.msg.redundant_condition_in_comprehension(False, condition)
5813-
elif false_map is None:
5815+
elif false_map is None or any(
5816+
isinstance(get_proper_type(t), UninhabitedType) for t in false_map.values()
5817+
):
58145818
self.msg.redundant_condition_in_comprehension(True, condition)
58155819

58165820
def visit_conditional_expr(self, e: ConditionalExpr, allow_none_return: bool = False) -> Type:

test-data/unit/check-errorcodes.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ g = 3 if True else 4 # E: If condition is always true [redu
805805
h = 3 if False else 4 # E: If condition is always false [redundant-expr]
806806
i = [x for x in lst if True] # E: If condition in comprehension is always true [redundant-expr]
807807
j = [x for x in lst if False] # E: If condition in comprehension is always false [redundant-expr]
808-
k = [x for x in lst if isinstance(x, int) or foo()]
808+
k = [x for x in lst if isinstance(x, int) or foo()] # E: If condition in comprehension is always true [redundant-expr]
809809
[builtins fixtures/isinstancelist.pyi]
810810

811811
[case testRedundantExprTruthiness]

test-data/unit/check-isinstance.test

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,8 +2522,6 @@ def f2(x: T2) -> T2:
25222522
else:
25232523
reveal_type(x) # N: Revealed type is "__main__.C"
25242524
return x
2525-
2526-
25272525
[builtins fixtures/isinstance.pyi]
25282526

25292527
[case testIsInstanceAdHocIntersectionGenericsWithValuesDirectReturn]
@@ -2537,7 +2535,6 @@ class B:
25372535
class C:
25382536
attr: str
25392537

2540-
# TODO: these kinds of typevars play havoc with checking unreachable code
25412538
T1 = TypeVar('T1', A, B)
25422539
def f1(x: T1) -> T1:
25432540
if isinstance(x, A):

test-data/unit/check-literal.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,8 +2837,8 @@ w: Union[Truth, AlsoTruth]
28372837
if w:
28382838
reveal_type(w) # N: Revealed type is "Union[__main__.Truth, __main__.AlsoTruth]"
28392839
else:
2840-
_ = "unreachable" # E: Statement is unreachable
2841-
2840+
reveal_type(w) # E: Statement is unreachable \
2841+
# N: Revealed type is "Never"
28422842
[builtins fixtures/bool.pyi]
28432843

28442844
[case testLiteralAndInstanceSubtyping]

0 commit comments

Comments
 (0)