Title: Cannot use multiple inheritance with `collections.abc.Buffer` and `typing.Protocol` · Issue #104797 · python/cpython · GitHub
Open Graph Title: Cannot use multiple inheritance with `collections.abc.Buffer` and `typing.Protocol` · Issue #104797 · python/cpython
X Title: Cannot use multiple inheritance with `collections.abc.Buffer` and `typing.Protocol` · Issue #104797 · python/cpython
Description: 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-case...
Open Graph Description: 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....
X Description: 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....
Opengraph URL: https://github.com/python/cpython/issues/104797
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Cannot use multiple inheritance with `collections.abc.Buffer` and `typing.Protocol`","articleBody":"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:\r\n\r\n```pycon\r\n\u003e\u003e\u003e from contextlib import AbstractContextManager\r\n\u003e\u003e\u003e from typing import Protocol\r\n\u003e\u003e\u003e class Foo(AbstractContextManager, Protocol):\r\n... def extra_method(self) -\u003e None: ...\r\n...\r\n\u003e\u003e\u003e\r\n```\r\n\r\n`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:\r\n\r\n```pycon\r\n\u003e\u003e\u003e class Bar(Buffer, Protocol):\r\n... def extra_method(self) -\u003e None: ...\r\n...\r\nTraceback (most recent call last):\r\n File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\r\n File \"C:\\Users\\alexw\\coding\\cpython\\Lib\\abc.py\", line 106, in __new__\r\n cls = super().__new__(mcls, name, bases, namespace, **kwargs)\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"C:\\Users\\alexw\\coding\\cpython\\Lib\\typing.py\", line 1916, in __init_subclass__\r\n raise TypeError('Protocols can only inherit from other'\r\nTypeError: Protocols can only inherit from other protocols, got \u003cclass 'collections.abc.Buffer'\u003e\r\n```\r\n\r\nI think `Buffer` should be special-cased in the same way as `Buffer` and `Iterable`. It needs to be added to this mapping, I think:\r\n\r\nhttps://github.com/python/cpython/blob/ddb14859535ab8091381b9d0baf32dbe245b5e65/Lib/typing.py#L1740-L1746\r\n\r\nCc. @JelleZijlstra for PEP-688\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-104827\n* gh-104841\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/AlexWaygood","@type":"Person","name":"AlexWaygood"},"datePublished":"2023-05-23T14:18:02.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/104797/cpython/issues/104797"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3d1fd52f-82f5-6563-a900-314f6c6b83f9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ADFC:2D5987:17C25B8:2025026:6969A9B7 |
| html-safe-nonce | 934186ee369e9514ffa9aba88fb527c8a4728a69d6d94bfdb84feb9f19a5ace8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBREZDOjJENTk4NzoxN0MyNUI4OjIwMjUwMjY6Njk2OUE5QjciLCJ2aXNpdG9yX2lkIjoiODI0NDA3NDQ3MDk5MzE0NDI0NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 44483756c2d56b126f7d2cabf17fda203e118bdcc0e3c86f562f94803e743129 |
| hovercard-subject-tag | issue:1722182988 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/104797/issue_layout |
| twitter:image | https://opengraph.githubassets.com/68886d358c3fa5d3e343b9e3acf0709a059cbc16106a6c9ba883bbbcdfa8af9a/python/cpython/issues/104797 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/68886d358c3fa5d3e343b9e3acf0709a059cbc16106a6c9ba883bbbcdfa8af9a/python/cpython/issues/104797 |
| og:image:alt | 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.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AlexWaygood |
| hostname | github.com |
| expected-hostname | github.com |
| None | 24c4c97a2d520cb286b35e1a4c22d7a4df3c26a2fa28dd7cdf0e65db327b4de7 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 124667f43168afb6c9c03b7c02eb5b1d2e1be3d9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width