Skip to content

TypeIs[x] should be treated as a subtype of bool #8521

@erictraut

Description

@erictraut

Pyright does not currently perform proper subtyping checks for TypeIs and TypeGuard. In particular, it treats TypeIs[x] as equivalent to bool when performing subtyping checks. TypeIs[x] should be considered a subtype of bool, and TypeIs[x] should be considered a subtype of TypeIs[y] only if x is a subtype of y.

This can be demonstrated in this code, where the Any input to is_dataclass should cause the overload logic to resolve both overloads rather than the first.

import dataclasses
from typing import reveal_type
def func(x: Any) -> None:
    if dataclasses.is_dataclass(x) and isinstance(x, object):
        reveal_type(x)
        print(dataclasses.asdict(x))

This is related to this discussion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions