Skip to content

[Regression 1.19.1 -> 1.20.0] value in container based narrowing causes false negatives. #21512

@randolf-scholz

Description

@randolf-scholz

mypy seems to narrow the type of value in a if value in container branch, which leads to false negatives.

class Size(tuple[int, ...]):
    def numel(self) -> int:
        return 0  # math.prod(self)

sizes: list[Size] = [Size([1, 2, 3])]
print(sizes[0].numel())  # OK
value = (1, 2, 3)
if value in sizes:
    reveal_type(value)  # mypy: Size; pyright, ty, zuban, pyrefly: tuple
    print(value.numel())  # ERROR at runtime

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions