Is this a bug in mypy?

It looks like mypy doesn’t know how to type narrow part of a tuple?

from typing import Union

items: list[tuple[int, Union[str, int]]] = []

# error: List comprehension has incompatible type List[Tuple[int, Union[str, int]]]; expected List[Tuple[int, int]]
int_items: list[tuple[int, int]] = [pair for pair in items if isinstance(pair[1], int)]

# OK
int_items_2: list[tuple[int, int]] = [(a, b) for a, b in items if isinstance(b, int)]

Just FYI, this would be better asked on a MyPy forum. To answer your question, no, I don’t believe so, though in addition to your workaround, you could use a [user-defined type guard(Type narrowing — Mypy 0.920+dev.01e6aba30c787c664ac88d849b45f7d303316951.dirty documentation). If you believe this should be implemented, you can open a mpypy issue, though there are already related open issues and unless you’re willing to contribute a PR, it seems unlikely to be a near-term priority. Cheers!

The closest I could find to a ‘mypy forum’ was their Gitter, but virtually all questions get lost in the noise.

That issue does indeed look like what I am facing and I’ll consider posting on it.

Anyway, thanks for the reply.

Ah, gotcha. Their issue tracker might be your best bet, then, if you’re sure it isn’t covered by the existing ones I linked. Best of luck!