basedpyright icon indicating copy to clipboard operation
basedpyright copied to clipboard

`isinstance` checks on classes with generics should narrow to the generic's bound if it's covariant, and `Never` if it's contravariant

Open DetachHead opened this issue 1 year ago • 2 comments

Code sample in basedpyright playground

class Foo[T: int | str]:
    def foo(self, other: object):
        if isinstance(other, Foo):
            reveal_type(other) # Foo[Unknown], should be Foo[int | str]

if the generic is covariant it should narrow to Foo[int | str] (or whatever the generic's bound is), and if it's contravariant it should narrow to Foo[Never] i think

DetachHead avatar Sep 18 '24 00:09 DetachHead

It doesn't seem like there's enough information here to come to this conclusion because the other argument has no generic connection to self (The T is never used to annotate anything about other).

So the only conclusion I can see here is that other should be of type Foo[BoundOfT]

UltimateLobster avatar Sep 30 '24 04:09 UltimateLobster

yeah that's what i meant sorry, updated the example to be more clear

DetachHead avatar Sep 30 '24 06:09 DetachHead