Skip to content

Type guard doesn't intersect types like instance check #1351

@NeilGirdhar

Description

@NeilGirdhar

The type guard doesn't seem to work like an instance check:

from typing import TypeGuard, Any
class X: pass
class Y: pass
x: X = X()
assert isinstance(x, Y)
def f(z: Any) -> TypeGuard[Y]:
    return isinstance(z, Y)
reveal_type(x)  # Revealed type is "a.<subclass of "X" and "Y">", which is great!
x2: X
assert f(x2)
reveal_type(x2)  # Revealed type is "a.Y", which is unfortunate.

This is causing problems with the definition of dataclasses.is_dataclass python/typeshed#9723.

The above code was tested with MyPy and Pyright.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: otherOther topics not covered

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions