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 use multiple inheritance with collections.abc.Buffer and typing.Protocol #104797

Closed
AlexWaygood opened this issue May 23, 2023 · 0 comments · Fixed by #104827
Closed

Cannot use multiple inheritance with collections.abc.Buffer and typing.Protocol #104797

AlexWaygood opened this issue May 23, 2023 · 0 comments · Fixed by #104827
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented May 23, 2023

Various stdlib classes are treated as protocols by type checkers, but are actually ABCs at runtime (for performance reasons). Examples include contextlib.AbstractContextManager and collections.abc.Iterable. These classes are special-cased in typing.py to allow for multiple inheritance with typing.Protocol, so that the interface can be extended:

>>> from contextlib import AbstractContextManager
>>> from typing import Protocol
>>> class Foo(AbstractContextManager, Protocol):
...     def extra_method(self) -> None: ...
...
>>>

collections.abc.Buffer is a new-in-3.12 class that, like AbstractContextManager and Iterable, is an ABC at runtime but will be treated by type checkers as if it were a Protocol. However, multiple inheritance with collections.abc.Buffer and typing.Protocol currently fails:

>>> class Bar(Buffer, Protocol):
...     def extra_method(self) -> None: ...
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\alexw\coding\cpython\Lib\abc.py", line 106, in __new__
    cls = super().__new__(mcls, name, bases, namespace, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\alexw\coding\cpython\Lib\typing.py", line 1916, in __init_subclass__
    raise TypeError('Protocols can only inherit from other'
TypeError: Protocols can only inherit from other protocols, got <class 'collections.abc.Buffer'>

I think Buffer should be special-cased in the same way as Buffer and Iterable. It needs to be added to this mapping, I think:

cpython/Lib/typing.py

Lines 1740 to 1746 in ddb1485

_PROTO_ALLOWLIST = {
'collections.abc': [
'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
'Hashable', 'Sized', 'Container', 'Collection', 'Reversible',
],
'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
}

Cc. @JelleZijlstra for PEP-688

Linked PRs

@AlexWaygood AlexWaygood added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir topic-typing 3.12 bugs and security fixes 3.13 bugs and security fixes labels May 23, 2023
@JelleZijlstra JelleZijlstra self-assigned this May 23, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 24, 2023
…er (pythonGH-104827)

(cherry picked from commit c0ab7d4)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
AlexWaygood pushed a commit that referenced this issue May 24, 2023
…fer (GH-104827) (#104841)

gh-104797: Allow Protocols to inherit from collections.abc.Buffer (GH-104827)
(cherry picked from commit c0ab7d4)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants