Skip to content

Type of generic class now required to use classmethod as a constructor? #7487

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

You must be logged in to vote

A class method is bound to a specific class type. If that class is generic, the type arguments for the type are dictated by the (specialized) class type to which it is bound.

Unlike class methods, constructors are special-cased in the type system (and in type checkers). They determine the type arguments for a generic class at construction time even if the class being constructed is not explicitly specialized. Class methods are not like this. If you're using a class method as an alternative constructor — especially one that returns Self, you need to manually specify the specialized type of the class at the call site.

Replies: 3 comments

Comment options

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

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants