Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot extend all protocols #1343

Closed
SimpleArt opened this issue Feb 4, 2023 · 2 comments
Closed

Cannot extend all protocols #1343

SimpleArt opened this issue Feb 4, 2023 · 2 comments
Labels
topic: other Other topics not covered

Comments

@SimpleArt
Copy link

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.

@SimpleArt SimpleArt added the topic: other Other topics not covered label Feb 4, 2023
@SimpleArt
Copy link
Author

I just realized that:

  • typing.AbstractSet and similar are deprecated.
  • They were intended to be generic versions of their counterparts, not protocols.

So if you want protocols, you'll need to fully implement them from scratch.

@JelleZijlstra
Copy link
Member

Yes, in general only the very simple (1-2 methods) abstract types were made into protocols. The more complex ones (Sequence, Mapping, etc.) aren't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: other Other topics not covered
Projects
None yet
Development

No branches or pull requests

2 participants