ITADN

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

#21512Openrandolf-scholz 创建于 2026-05-19
bug
R
randolf-scholzcommented
`mypy` seems to narrow the type of `value` in a `if value in container` branch, which leads to false negatives. ```python 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 ``` - with 1.19.1, `mypy` reveals `tuple[int, int, int]` and correctly errors. https://mypy-play.net/?mypy=1.19.1&python=3.12&gist=12a961d6aab6b40ee13d895ab9ce037b - with 1.20.0 onwards, `mypy` reveals `Size` and produces a false negative. https://mypy-play.net/?mypy=1.20.0&python=3.12&gist=76665c3096babff2c683164fdae02dbc
0 条评论