Skip to content

pylance complains about return type using Self in a class method #6896

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

You must be logged in to vote

Pyright (the type checker that underlies pylance) is working correctly here and in conformance with the Python typing spec. If you'd like to read more about the Self type and how the typing spec defines it, refer to this link.

When you use Self in a type annotation, it represents an implicit type variable scoped to the class in which it is contained and with an upper bound of that class. It is meant to be used in cases where the class may be subclassed, and it's a stand-in for whatever subclass is used for a given instance. For example, if you created a subclass of Book called Novel, the Self type variable would concretely be of type Novel if you were to instantiate a Novel object.

Note t…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by hardhu
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