Skip to content

Cannot extend all protocols #1343

@SimpleArt

Description

@SimpleArt

Related: #561

Previously subclassing typing.Iterable with typing.Protocol raised an error because typing.Iterable was not an actual protocol. This specific problem was fixed but there are other protocols which are not actual protocols, such as typing.AbstractSet.

My specific use-case is creating particularly spurred by the following line:

https://github.com/python/cpython/blob/144aaa74bbd77aee822ee92344744dbb05aa2f30/Lib/_collections_abc.py#L9

For example, if I wanted to extend collections.abc.Set to include the missing methods mentioned, I would approach it something like this:

# protocols.py
import typing


# Created for consistency and `isinstance` usage.
# Causes the mentioned error.
class AbstractSet(typing.AbstractSet, typing.Protocol):
    ...
# collections_abc.py
import protocols
import collections.abc

# Created to provide default implementations.
class AbstractSet(collections.abc.Set, protocols.AbstractSet):
    ...

It seems to me that the current fix for users is to simply not subclass the protocols and write out the entire protocol themselves.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: otherOther topics not covered

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions