Should `type(x)` perform type erasure for generics?

At runtime type(x) is just list, not list[int], so I think that’s ideally what type checkers should infer too.

The type checkers you flag should therefore probably change their behavior.

For both mypy and pyright it is possible to construct an example where the type checker behaves incorrectly because of this behavior:

from typing import reveal_type

def g(x: list[int] | str):
    if type(x) is list[int]:
        reveal_type(x)  # list[int] (should be Never)
    else:
        reveal_type(x)  # list[int] | str
1 Like