Title: Typing: Document performance pitfalls of protocols decorated with `@runtime_checkable` · Issue #102936 · python/cpython · GitHub
Open Graph Title: Typing: Document performance pitfalls of protocols decorated with `@runtime_checkable` · Issue #102936 · python/cpython
X Title: Typing: Document performance pitfalls of protocols decorated with `@runtime_checkable` · Issue #102936 · python/cpython
Description: Documentation The following two calls look similar, but the second is dramatically slow compared to the first: from typing import SupportsInt isinstance(3, int) isinstance(3, SupportsInt) The reason for the slowness is because of _Protoc...
Open Graph Description: Documentation The following two calls look similar, but the second is dramatically slow compared to the first: from typing import SupportsInt isinstance(3, int) isinstance(3, SupportsInt) The reaso...
X Description: Documentation The following two calls look similar, but the second is dramatically slow compared to the first: from typing import SupportsInt isinstance(3, int) isinstance(3, SupportsInt) The reaso...
Opengraph URL: https://github.com/python/cpython/issues/102936
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Typing: Document performance pitfalls of protocols decorated with `@runtime_checkable`","articleBody":"# Documentation\r\n\r\nThe following two calls look similar, but the second is dramatically slow compared to the first:\r\n\r\n```python\r\nfrom typing import SupportsInt\r\n\r\nisinstance(3, int)\r\nisinstance(3, SupportsInt)\r\n```\r\n\r\nThe reason for the slowness is because of [`_ProtocolMeta.__instancecheck__`](https://github.com/python/cpython/blob/d3ca042c99b2e86a2bb927a877fdfcbacdc22f89/Lib/typing.py#L1977-L1999), which makes calling `isinstance()` against any runtime-checkable protocol pretty slow. It's possible we can find ways of optimising `_ProtocolMeta.__instancecheck__`, but whatever we do, it is likely to remain substantially slower to call `isinstance()` against runtime-checkable protocols than it is to call `isinstance()` against \"normal\" classes. This is often pretty surprising for users, so one of the consensus points in https://github.com/python/typing/issues/1363 was that this should be better documented.\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-102937\n* gh-102963\n* gh-102964\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/AlexWaygood","@type":"Person","name":"AlexWaygood"},"datePublished":"2023-03-22T22:48:23.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/102936/cpython/issues/102936"}
| 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:1ee1597c-2b22-d2e3-eb88-8657a9a4f2a0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CFCE:23FC6E:2B96E0:3A9BBE:6969AA1C |
| html-safe-nonce | 42a327c4fb16380a026c054d65f292b9434ffd142c33afc9972fd946160d95ac |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRkNFOjIzRkM2RToyQjk2RTA6M0E5QkJFOjY5NjlBQTFDIiwidmlzaXRvcl9pZCI6IjUyNTg2MTc1MTc4NTUzMjA2MDQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | dddf1ba346264de3120e2427eea1e8fac60f7574d1cfda210ce5d04344f8ad8d |
| hovercard-subject-tag | issue:1636602204 |
| 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/102936/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c1036acd0c249e9d2c22aa0bab635c2aeff360966788e89f5cab582741745fd7/python/cpython/issues/102936 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c1036acd0c249e9d2c22aa0bab635c2aeff360966788e89f5cab582741745fd7/python/cpython/issues/102936 |
| og:image:alt | Documentation The following two calls look similar, but the second is dramatically slow compared to the first: from typing import SupportsInt isinstance(3, int) isinstance(3, SupportsInt) The reaso... |
| 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