GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Cannot use multiple inheritance with `collections.abc.Buffer` and `typing.Protocol` · Issue #104797 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
#104827
Closed
Cannot use multiple inheritance with collections.abc.Buffer and typing.Protocol#104797
#104827
Copy link
Assignees
JelleZijlstra
Labels
3.12only security fixesonly security fixes3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@AlexWaygood
AlexWaygood
opened on May 23, 2023
Issue body actions

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

  • gh-104797: Allow Protocols to inherit from collections.abc.Buffer #104827
  • [3.12] gh-104797: Allow Protocols to inherit from collections.abc.Buffer (GH-104827) #104841

Activity

  1. AlexWaygood
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    topic-typing
    3.12only security fixesonly security fixes
    3.13only security fixesonly security fixes
    on May 23, 2023
  2. JelleZijlstra
    self-assigned this
    on May 23, 2023
  3. AlexWaygood
    closed this as completedin #104827on May 24, 2023
  4. AlexWaygood
    added a commit that references this issue on May 24, 2023

    gh-104797: Allow Protocols to inherit from collections.abc.Buffer (#1…

    c0ab7d4
  5. miss-islington
    added a commit that references this issue on May 24, 2023

    pythongh-104797: Allow Protocols to inherit from collections.abc.Buff…

    42a05db
  6. AlexWaygood
    added a commit that references this issue on May 24, 2023

    [3.12] gh-104797: Allow Protocols to inherit from collections.abc.Buf…

    d10d1e3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @JelleZijlstra
    JelleZijlstra

Labels

3.12only security fixesonly security fixes3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.