Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6778,6 +6778,17 @@ def comparison_type_narrowing_helper(self, node: ComparisonExpr) -> tuple[TypeMa
expr_indices=[0, 1],
narrowable_indices={0},
)
if else_map and not (
isinstance(p_typ := get_proper_type(iterable_type), TupleType)
and all(
is_singleton_equality_type(get_proper_type(item))
for item in p_typ.items
)
):
# In general, we can't do negative narrowing, since e.g. the container
# could just be empty. However, we can do negative narrowing for some
# tuples e.g. `x not in (None,)`
else_map = {}

if right_index in narrowable_operand_index_to_hash:
if_type, else_type = self.conditional_types_for_iterable(
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -3268,6 +3268,21 @@ def f1(x: Union[str, float], t1: list[Literal['a', 'b']], t2: list[str]):
reveal_type(x) # N: Revealed type is "builtins.str | builtins.float"
[builtins fixtures/primitives.pyi]

[case testNegativeNarrowingInContainer]
# flags: --warn-unreachable
from enum import Enum

class Color(Enum):
RED = 1

def count(colors: list[Color]) -> None:
counts: dict[Color, int] = {}
for color in colors:
if color not in counts:
counts[color] = 0
counts[color] += 1
[builtins fixtures/dict.pyi]

[case testNarrowAnyWithEqualityOrContainment]
# flags: --strict-equality --warn-unreachable
# https://github.com/python/mypy/issues/17841
Expand Down
Loading