Skip to content

Why do I need a second type var for a method on a class? #9555

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

You must be logged in to vote

The expression MyThing.build_one binds MyThing to the build_one method. The MyThing class takes one type argument, so you would normally need to do something like MyThing[str].build_one. Since you haven't supplied a type argument, a type checker will assume Any (or Unknown, which is what pyright calls an "implicit" Any). That means the class variable T is replaced with Unknown, so the type of MyThing.build_one is (some_property: Unknown) -> MyThing[Unknown].

Now let's see what happens if you use a separate method-scoped type variable U. The expression MyThing.build_two once again binds MyThing[Unknown] to the method build_two and the class-scoped type variable T is replaced by Unknown. Th…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ericroberts
Comment options

Answer selected by ericroberts
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