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 collections ABCs with protocol #89187
Comments
Since the container ABCs are normal classes, and Protocol cannot subclass normal classes, there's no way to create a protocol that extends the ABCs without explicitly listing out all the methods needed for the collection. e.g., can't do this: from typing import Iterable, Protocol
class IterableWithMethod(Iterable, Protocol):
def method(self) -> None: pass Since the ABCs don't provide any default implementations (I think?), maybe they should just be defined as runtime checkable protocols instead of ABCs? |
Thanks for the bug report Anup. I'm mildly confused by what you meant, so please help me here: I interpreted your request as wanting any type that implements Are you perhaps saying that you want issubclass(Foo, IterableWithMethod) to work? |
Yeah, you got the gist of what I meant, though, I guess that was a confusing example since there are apparently protocols for Iterable. But not for many of the other collection ABCs such as Sequence, e.g., the following doesn't work: from typing import Sequence, Protocol
class SequenceWithMethod(Sequence, Protocol):
def method(self) -> None: pass In which case, anything that implements |
I suspect this may be intentional. See related issues https://bugs.python.org/issue35190 and https://bugs.python.org/issue23864. Copying verbatim from what Ivan said in the first issue: " |
The docs need to clarify which ABCs can satisfy isinstance() based solely on hasattr() logic and which ABCs require registration or direct subclassing. The docs should also explain why the more complex ABCs can't reliably be recognized based on the presence of desired method names. One of the original motivating reasons for the ABCs was that the presence of __getitem__ was insufficient for determining whether a class was intended to be a Mapping or a Sequence. Accordingly, those classes have to subclass from an ABC or register with the ABC in order to declare intent. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: