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
One can subclass collections.abc.Mapping as well as typing.Mapping[str, int]. However, it is currently prohibited to subclass typing.Callable and typing.Tuple:
I'm not entirely against this, but IIRC this is intentional, since these
are not plain generic classes. Doing the right thing in the type checker
might be difficult. I think Callable should basically be treated as a duck
type (call) and if you want to subclass Tuple you should subclass tuple
instead. Probably. Maybe @JukkaL has more thoughts?
Early prototypes of typing actually supported subclassing Tuple[...]. I think that it's as reasonable as subclassing List[...], which is already supported. Subclassing tuple is not really a good alternative because then you can't specify the item types or the number of items, and instead it falls back to Tuple[Any, ...].
I think that Callable should use structural subtyping -- if __call__ is defined, it's a callable. Explicitly subclassing it seems kind of reasonable as well, for consistency with the collections.abc.Callable ABC, but it would be less useful than subclassing Tuple[...].
Activity
gvanrossum commentedon Oct 16, 2016
I'm not entirely against this, but IIRC this is intentional, since these
are not plain generic classes. Doing the right thing in the type checker
might be difficult. I think Callable should basically be treated as a duck
type (call) and if you want to subclass Tuple you should subclass tuple
instead. Probably. Maybe @JukkaL has more thoughts?
JukkaL commentedon Oct 16, 2016
Early prototypes of
typingactually supported subclassingTuple[...]. I think that it's as reasonable as subclassingList[...], which is already supported. Subclassingtupleis not really a good alternative because then you can't specify the item types or the number of items, and instead it falls back toTuple[Any, ...].I think that
Callableshould use structural subtyping -- if__call__is defined, it's a callable. Explicitly subclassing it seems kind of reasonable as well, for consistency with thecollections.abc.CallableABC, but it would be less useful than subclassingTuple[...].Refactor Union, Tuple, and Callable (#308)