Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Sep 18, 2024 · 2 comments · May be fixed by #745
Labels
type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules

Comments

@DetachHead
Copy link
Owner

DetachHead commented Sep 18, 2024

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 DetachHead added the type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules label Sep 18, 2024
@UltimateLobster
Copy link

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
Copy link
Owner Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type checking / linting issues relating to existing diagnostic rules or proposals for new diagnostic rules
Projects
None yet
2 participants