DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
https://docs.basedpyright.com
Other
1.02k stars 19 forks source link

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

Open DetachHead opened 2 weeks ago

DetachHead commented 2 weeks ago

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

UltimateLobster commented 3 days ago

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]

DetachHead commented 3 days ago

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