Skip to content

Wrong type of variable in the class with __slots__ #8742

Answered by erictraut
belegnar asked this question in Q&A
Discussion options

You must be logged in to vote

The fact that you're using __slots__ here has nothing to do with the type violation you're seeing. You can completely remove __slots__ from your example, and you'll see the same behavior.

You have declared that the variable filters in the class Abstract must be of type Filter. In the class Impl, you are assigning a value to this variable that indeed conforms to the type Filter (since RegExFilter is a subclass of Filter), so that is not a type violation. However, you're later relying on the fact that Abstract.filters is a RegExFilter rather than a Filter. That's a type violation, and pyright is correct to highlight it. There is nothing that prevents Impl.filters from being overwritten by a…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by belegnar
Comment options

You must be logged in to vote
2 replies
@erictraut
Comment options

@belegnar
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants