Skip to content

Cannot extend built-in Protocols #561

@FuegoFro

Description

@FuegoFro

In typing.py, a number of built-in protocols extend from Generic, not Protocol (eg Iterable, Container, Sized, etc). However, at runtime the Procotol class enforces that all bases for a class also inherit from Protocol. Thus the following code

from typing import Iterable, TypeVar
from typing_extensions import Protocol

T = TypeVar('T')

class Foo(Iterable[T], Protocol[T]):
    pass

causes the error

TypeError: Protocols can only inherit from other protocols, got typing.Iterable

This is further confused by the fact that, in recent versions of typeshed, typing.pyi has these types like Iterable extending Protocol and not Generic (opposite of runtime, but also seemingly the more "correct" version).

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