Title: Add `typing.get_protocol_members` and `typing.is_protocol` · Issue #104873 · python/cpython · GitHub
Open Graph Title: Add `typing.get_protocol_members` and `typing.is_protocol` · Issue #104873 · python/cpython
X Title: Add `typing.get_protocol_members` and `typing.is_protocol` · Issue #104873 · python/cpython
Description: #103160 added an attribute __protocol_attrs__ that holds the names of all protocol attributes: >>> from typing import Protocol >>> class X(Protocol): ... a: int ... def b(self) -> str: pass ... >>> X.__protocol_attrs__ {'b', 'a'} This is...
Open Graph Description: #103160 added an attribute __protocol_attrs__ that holds the names of all protocol attributes: >>> from typing import Protocol >>> class X(Protocol): ... a: int ... def b(self) -> str: pass ... >>>...
X Description: #103160 added an attribute __protocol_attrs__ that holds the names of all protocol attributes: >>> from typing import Protocol >>> class X(Protocol): ... a: int ... def b(self) -&...
Opengraph URL: https://github.com/python/cpython/issues/104873
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add `typing.get_protocol_members` and `typing.is_protocol`","articleBody":"#103160 added an attribute `__protocol_attrs__` that holds the names of all protocol attributes:\r\n\r\n```\r\n\u003e\u003e\u003e from typing import Protocol\r\n\u003e\u003e\u003e class X(Protocol):\r\n... a: int\r\n... def b(self) -\u003e str: pass\r\n... \r\n\u003e\u003e\u003e X.__protocol_attrs__\r\n{'b', 'a'}\r\n```\r\n\r\nThis is useful for code that needs to extract the names included in a Protocol at runtime. Previously, this was quite difficult, as you had to look at the class's `__dict__` and `__annotations__` directly and exclude a long list of internal attributes (e.g. https://github.com/quora/pyanalyze/blob/bd7f520adc2d8b098be657dfa514d1433bea3b0c/pyanalyze/checker.py#L428).\r\n\r\nHowever, currently `__protocol_attrs__` is an undocumented private attribute. I think we should either document it or add an introspection helper like `typing.get_protocol_attrs()` that exposes it.\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-104878\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/JelleZijlstra","@type":"Person","name":"JelleZijlstra"},"datePublished":"2023-05-24T16:21:18.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":12},"url":"https://github.com/104873/cpython/issues/104873"}
| 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:cac26687-bddc-e2c2-a7ae-823b79388008 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D056:2AEA36:1842406:2036D31:696AEA7F |
| html-safe-nonce | 6a2adb233d6703d2bb74a99b818668f06da5115b0704bcae2904a6f2b3588f26 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMDU2OjJBRUEzNjoxODQyNDA2OjIwMzZEMzE6Njk2QUVBN0YiLCJ2aXNpdG9yX2lkIjoiNTM5ODIyODI4NTk2NTEzNDQ2MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | de0f66602de158abe830d2f3b68d9685fe5c4fcab807de82e8ae7aecb05c45e0 |
| hovercard-subject-tag | issue:1724357892 |
| 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/104873/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c9bcac3ba9e0cce750b01972550112fb185219578f737c7b7e4a4ad04704b4da/python/cpython/issues/104873 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c9bcac3ba9e0cce750b01972550112fb185219578f737c7b7e4a4ad04704b4da/python/cpython/issues/104873 |
| og:image:alt | #103160 added an attribute __protocol_attrs__ that holds the names of all protocol attributes: >>> from typing import Protocol >>> class X(Protocol): ... a: int ... def b(self) -> str: pass ... >>>... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JelleZijlstra |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| 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 | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width