Skip to content

Commit 5c620d8

Browse files
committed
Fix crash with chained containment
Dumb bug of mine, fixes #20686
1 parent e858d5d commit 5c620d8

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

mypy/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6557,7 +6557,7 @@ def comparison_type_narrowing_helper(self, node: ComparisonExpr) -> tuple[TypeMa
65576557
"==",
65586558
operands=[operands[left_index], operands[right_index]],
65596559
operand_types=[item_type, collection_item_type],
6560-
expr_indices=[left_index, right_index],
6560+
expr_indices=[0, 1],
65616561
narrowable_indices={0},
65626562
)
65636563

test-data/unit/check-narrowing.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3020,6 +3020,20 @@ def f2(x: Any) -> None:
30203020
reveal_type(x) # N: Revealed type is "Any"
30213021
[builtins fixtures/tuple.pyi]
30223022

3023+
[case testNarrowChainedContainment]
3024+
# flags: --strict-equality --warn-unreachable
3025+
3026+
def bad_compare_has_key(has_key: bool, key: str, s: tuple[str, ...]) -> None:
3027+
if has_key == key in s: # E: Non-overlapping equality check (left operand type: "bool", right operand type: "str")
3028+
reveal_type(has_key) # N: Revealed type is "builtins.bool"
3029+
reveal_type(key) # N: Revealed type is "builtins.str"
3030+
3031+
def bad_but_should_pass(has_key: bool, key: bool, s: tuple[bool, ...]) -> None:
3032+
if has_key == key in s:
3033+
reveal_type(has_key) # N: Revealed type is "builtins.bool"
3034+
reveal_type(key) # N: Revealed type is "builtins.bool"
3035+
[builtins fixtures/primitives.pyi]
3036+
30233037
[case testNarrowTypeObject]
30243038
# flags: --strict-equality --warn-unreachable
30253039
from typing import Any

0 commit comments

Comments
 (0)