Title: Runtime-checkable protocols are broken on py312 (the sequel) · Issue #104935 · python/cpython · GitHub
Open Graph Title: Runtime-checkable protocols are broken on py312 (the sequel) · Issue #104935 · python/cpython
X Title: Runtime-checkable protocols are broken on py312 (the sequel) · Issue #104935 · python/cpython
Description: Bug report On 3.8-3.11, all subclasses of typing.Generic were guaranteed to have an _is_protocol class attribute, which is used as an internal marker: cpython/Lib/typing.py Line 1790 in 76873ca _is_protocol = False Two places in typing.p...
Open Graph Description: Bug report On 3.8-3.11, all subclasses of typing.Generic were guaranteed to have an _is_protocol class attribute, which is used as an internal marker: cpython/Lib/typing.py Line 1790 in 76873ca _is...
X Description: Bug report On 3.8-3.11, all subclasses of typing.Generic were guaranteed to have an _is_protocol class attribute, which is used as an internal marker: cpython/Lib/typing.py Line 1790 in 76873ca _is...
Opengraph URL: https://github.com/python/cpython/issues/104935
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Runtime-checkable protocols are broken on py312 (the sequel)","articleBody":"# Bug report\r\n\r\nOn 3.8-3.11, all subclasses of `typing.Generic` were guaranteed to have an `_is_protocol` class attribute, which is used as an internal marker:\r\n\r\nhttps://github.com/python/cpython/blob/76873ca6b1ad1a1eb9518f0ff7fc594ec96d0a65/Lib/typing.py#L1790\r\n\r\nTwo places in `typing.py` rely on all subclasses of `Generic` having this marker:\r\n\r\nhttps://github.com/python/cpython/blob/dbc8216f4c00ea40b0c2d3ca487e5afeb4b0e0b1/Lib/typing.py#L1895-L1897\r\n\r\nhttps://github.com/python/cpython/blob/dbc8216f4c00ea40b0c2d3ca487e5afeb4b0e0b1/Lib/typing.py#L2062-L2064\r\n\r\nHowever, on Python 3.12 (due to the implementation of PEP-695), subclasses of `Generic` no longer have this marker:\r\n\r\n```pycon\r\n\u003e\u003e\u003e class Foo[T]: ...\r\n...\r\n\u003e\u003e\u003e Foo._is_protocol\r\nTraceback (most recent call last):\r\n File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\r\nAttributeError: type object 'Foo' has no attribute '_is_protocol'\r\n```\r\n\r\nThis leads to `AttributeError` being raised in two situations where it shouldn't be:\r\n\r\n```pycon\r\nPython 3.13.0a0 (heads/main:1080c4386d, May 25 2023, 13:11:38) [MSC v.1932 64 bit (AMD64)] on win32\r\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\r\n\u003e\u003e\u003e from typing import Protocol, runtime_checkable\r\n\u003e\u003e\u003e @runtime_checkable\r\n... class Foo[T]: ...\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\\typing.py\", line 2062, in runtime_checkable\r\n if not issubclass(cls, Generic) or not cls._is_protocol:\r\n ^^^^^^^^^^^^^^^^\r\nAttributeError: type object 'Foo' has no attribute '_is_protocol'\r\n\u003e\u003e\u003e @runtime_checkable\r\n... class HasX(Protocol):\r\n... x: int\r\n...\r\n\u003e\u003e\u003e class Bar[T]:\r\n... x: T\r\n... def __init__(self, x):\r\n... self.x = x\r\n...\r\n\u003e\u003e\u003e isinstance(Bar(42), HasX)\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\\typing.py\", line 1810, in __instancecheck__\r\n if super().__instancecheck__(instance):\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"C:\\Users\\alexw\\coding\\cpython\\Lib\\abc.py\", line 119, in __instancecheck__\r\n return _abc_instancecheck(cls, instance)\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"C:\\Users\\alexw\\coding\\cpython\\Lib\\typing.py\", line 1794, in __subclasscheck__\r\n return super().__subclasscheck__(other)\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"C:\\Users\\alexw\\coding\\cpython\\Lib\\abc.py\", line 123, in __subclasscheck__\r\n return _abc_subclasscheck(cls, subclass)\r\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\r\n File \"C:\\Users\\alexw\\coding\\cpython\\Lib\\typing.py\", line 1897, in _proto_hook\r\n issubclass(other, Generic) and other._is_protocol):\r\n ^^^^^^^^^^^^^^^^^^\r\nAttributeError: type object 'Bar' has no attribute '_is_protocol'\r\n```\r\n\r\nCc. @JelleZijlstra for PEP-695\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-104939\n* gh-104941\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/AlexWaygood","@type":"Person","name":"AlexWaygood"},"datePublished":"2023-05-25T15:20:50.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/104935/cpython/issues/104935"}
| 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:b080835e-0708-6e5b-b29c-8e0002854844 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8A10:E27E8:7E6C8E:AAE388:6969DC07 |
| html-safe-nonce | 833062a36cf637cbe6e9324e248abfb95d5875c355295e3b34ffe3606852f3b8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QTEwOkUyN0U4OjdFNkM4RTpBQUUzODg6Njk2OURDMDciLCJ2aXNpdG9yX2lkIjoiMTY5NjU4ODc3NDYyNDU4MjY2MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 72ad404b3ff907b0124562ba5862bd6983a94fcf3901769734f4241cbe8c75f8 |
| hovercard-subject-tag | issue:1726033008 |
| 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/104935/issue_layout |
| twitter:image | https://opengraph.githubassets.com/20552d7bcb768e867520ed9797c6adc5b1f1e1fac52f2a04f619205cdfeff631/python/cpython/issues/104935 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/20552d7bcb768e867520ed9797c6adc5b1f1e1fac52f2a04f619205cdfeff631/python/cpython/issues/104935 |
| og:image:alt | Bug report On 3.8-3.11, all subclasses of typing.Generic were guaranteed to have an _is_protocol class attribute, which is used as an internal marker: cpython/Lib/typing.py Line 1790 in 76873ca _is... |
| 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 | 7b32f1c7c4549428ee399213e8345494fc55b5637195d3fc5f493657579235e8 |
| 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 | bdde15ad1b403e23b08bbd89b53fbe6bdf688cad |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width