Skip to content

Support isinstance narrowing of specialized generic classes #456

@MatthewMckee4

Description

@MatthewMckee4

We currently narrow like this:

class A[T]: ...

def _(x: A[int] | int):
    if isinstance(x, A):
        reveal_type(x)  # revealed: (A[int] & A[Unknown]) | (int & A[Unknown])
    else:
        reveal_type(x)  # revealed: (A[int] & ~A[Unknown]) | (int & ~A[Unknown])

Ideally we would reveal:

class A[T]: ...

def _(x: A[int] | int):
    if isinstance(x, A):
        reveal_type(x)  # revealed: A[int]
    else:
        reveal_type(x)  # revealed: int

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggenericsBugs or features relating to ty's generics implementationnarrowingrelated to flow-sensitive type narrowing

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions