You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Generic Self classmethod fails for generic types #3631
@ilevkivskyi I'm fine with that. AFAICT #2354 is effectively fixed, the code example below works great:
fromtypingimportGeneric, TypeVar, Type_T=TypeVar('_T')
SelfType=TypeVar('SelfType', bound='Friend')
classFriend(Generic[_T]):
other=None# type: Frienddefmake(self: SelfType) ->SelfType:
returnselfclassSuperFriend(Generic[_T], Friend[_T]):
passreveal_type(SuperFriend[int]().make()) # E: Revealed type is 'test_generic_self.SuperFriend[builtins.int]'
I'd be fine if classmethods had the same behavior. In fact, @elazarg 's change #3633mostly fixes this issue. The types (as opposed to <nothing>) are correct but declaring the function make still throws an error:
fromtypingimportGeneric, TypeVar, Type_T=TypeVar('_T')
SelfType=TypeVar('SelfType', bound='Friend')
classFriend(Generic[_T]):
other=None# type: Friend@classmethoddefmake(cls: Type[SelfType]) ->SelfType: # E: Unsupported type Type["SelfType"]returncls()
classSuperFriend(Generic[_T], Friend[_T]):
passreveal_type(SuperFriend[int]().make()) # E: Revealed type is 'test_generic_self.SuperFriend*[builtins.int*]'
I think the Unsupported type Type["SelfType"] error is a separate issue. It happens even for staticmethod, for which there's no specific check for selftype.
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.
Activity
ilevkivskyi commentedon Jun 29, 2017
I think this is a duplicate of #3363 and #2354
I think we could leave only one of these three issues open and raise its priority.
rowillia commentedon Jun 29, 2017
@ilevkivskyi I'm fine with that. AFAICT #2354 is effectively fixed, the code example below works great:
I'd be fine if
classmethodshad the same behavior. In fact, @elazarg 's change #3633 mostly fixes this issue. The types (as opposed to<nothing>) are correct but declaring the functionmakestill throws an error:elazarg commentedon Jun 30, 2017
I think the
Unsupported type Type["SelfType"]error is a separate issue. It happens even forstaticmethod, for which there's no specific check for selftype.rowillia commentedon Jun 30, 2017
@elazarg got it. If you can land #2354 I can debug the
Type["SelfType"]issue.gvanrossum commentedon Jun 30, 2017
bharel commentedon Jun 29, 2018
I've been sent here by stackoverflow. Issue is still relevant.
Allow calling Type[T] where T has generic bound (#5309)