-
Notifications
You must be signed in to change notification settings - Fork 226
Closed
astral-sh/ruff
#22729Labels
narrowingrelated to flow-sensitive type narrowingrelated to flow-sensitive type narrowing
Description
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 NotImplementedErrorhttps://play.ty.dev/12096b28-8338-4038-a5c5-5db362115bd6
My workaround:
if type(b) is t and isinstance(b, t):
return bVersion
55a174ed9 2026-01-19
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
narrowingrelated to flow-sensitive type narrowingrelated to flow-sensitive type narrowing