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
mypyseems to narrow the type ofvaluein aif value in containerbranch, which leads to false negatives.mypyrevealstuple[int, int, int]and correctly errors. https://mypy-play.net/?mypy=1.19.1&python=3.12&gist=12a961d6aab6b40ee13d895ab9ce037bmypyrevealsSizeand produces a false negative. https://mypy-play.net/?mypy=1.20.0&python=3.12&gist=76665c3096babff2c683164fdae02dbc