Skip to content

Unexpected behavior regarding variable that can contain instance of base class or subclass #8144

Closed Answered by jrheard-seesaw
jrheard-seesaw asked this question in Q&A
Discussion options

You must be logged in to vote

I think I figured it out - foo is truthy, and pyright knows that foo or bar will always evaluate to foo, which is an instance of SubClass. Whoops!

I think that the issue in my specific situation is that (in the version of the code I'm actually working on) foo can actually be SubClass | None in practice, and is mis-annotated as being SubClass because we don't have very many pyright checks set up yet. Updating its annotation to SubClass | None resolves the reportUnnecessaryIsInstance error :)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jrheard-seesaw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant