Skip to content

Callable and Tuple should be subclassable. #299

Closed

Description

@ilevkivskyi

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:

class C(collections.abc.Callable): ...  # works
class C(typing.Callable): ...           # fails

class C(typing.List[T]): ...            # works
class C(typing.Tuple[T, ...]): ...      # fails

I think that both Callable and Tuple should be subclassable (at least in the simple form shown above).

@gvanrossum If you agree, then you can assign this issue to me.

Activity

  1. gvanrossum commented on Oct 16, 2016

    @gvanrossum
    Member

    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?

  2. JukkaL commented on Oct 16, 2016

    @JukkaL
    Contributor

    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[...].

  3. added a commit that references this issue on Oct 29, 2016
    50d2b4d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions