-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Generic Self classmethod fails for generic types #3631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@ilevkivskyi I'm fine with that. AFAICT #2354 is effectively fixed, the code example below works great: from typing import Generic, TypeVar, Type
_T = TypeVar('_T')
SelfType = TypeVar('SelfType', bound='Friend')
class Friend(Generic[_T]):
other = None # type: Friend
def make(self: SelfType) -> SelfType:
return self
class SuperFriend(Generic[_T], Friend[_T]):
pass
reveal_type(SuperFriend[int]().make()) # E: Revealed type is 'test_generic_self.SuperFriend[builtins.int]' I'd be fine if from typing import Generic, TypeVar, Type
_T = TypeVar('_T')
SelfType = TypeVar('SelfType', bound='Friend')
class Friend(Generic[_T]):
other = None # type: Friend
@classmethod
def make(cls: Type[SelfType]) -> SelfType: # E: Unsupported type Type["SelfType"]
return cls()
class SuperFriend(Generic[_T], Friend[_T]):
pass
reveal_type(SuperFriend[int]().make()) # E: Revealed type is 'test_generic_self.SuperFriend*[builtins.int*]' |
I think the |
Yeah, it gives the same error when `Type[SelfType]` is used outside the
`Friend` class. It's purely because of the use of Type[] with a type
variable that is bound by a generic class.
|
I've been sent here by stackoverflow. Issue is still relevant. |
Uh oh!
There was an error while loading. Please reload this page.
Repro:
I suspect the issue is that the type should be:
But there is no way to express that that I can tell.
Using an instance method behaves as expected:
The text was updated successfully, but these errors were encountered: