Skip to content

type(x) is t doesn't work for t: type[T] #2565

@arjenzorgdoc

Description

@arjenzorgdoc

Summary

type(x) is y doesn't work if y is of type type[T]. It does work with isinstance

I think this should all type check, but ty thinks bad is not ok:

def good1(b: object) -> int:
    if type(b) is int:
        return b
    raise NotImplementedError


def good2[T: int](b: object, t: type[T]) -> int:
    if isinstance(b, t):
        return b
    raise NotImplementedError


def bad[T: int](b: object, t: type[T]) -> int:
    if type(b) is t:
        return b   # < ---- type error here
    raise NotImplementedError

https://play.ty.dev/12096b28-8338-4038-a5c5-5db362115bd6

My workaround:

    if type(b) is t and isinstance(b, t):
        return b

Version

55a174ed9 2026-01-19

Metadata

Metadata

Assignees

Labels

narrowingrelated to flow-sensitive type narrowing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions