Title: Deriving builtins from ABCs implies incorrect and problematic metaclass structure · Issue #1595 · python/typeshed · GitHub
Open Graph Title: Deriving builtins from ABCs implies incorrect and problematic metaclass structure · Issue #1595 · python/typeshed
X Title: Deriving builtins from ABCs implies incorrect and problematic metaclass structure · Issue #1595 · python/typeshed
Description: typeshed claims that str derives from Sequence and therefore indirectly from collections_abc.Sized whose metaclass is declared to be ABCMeta. This is done as a type-checkable alternative to Sequence.register(str) which handles isinstance...
Open Graph Description: typeshed claims that str derives from Sequence and therefore indirectly from collections_abc.Sized whose metaclass is declared to be ABCMeta. This is done as a type-checkable alternative to Sequenc...
X Description: typeshed claims that str derives from Sequence and therefore indirectly from collections_abc.Sized whose metaclass is declared to be ABCMeta. This is done as a type-checkable alternative to Sequenc...
Opengraph URL: https://github.com/python/typeshed/issues/1595
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Deriving builtins from ABCs implies incorrect and problematic metaclass structure","articleBody":"typeshed [claims](https://github.com/python/typeshed/blob/master/stdlib/3/builtins.pyi#L233) that `str` derives from [`Sequence`](https://github.com/python/typing/blob/master/src/typing.py#L1846) and therefore indirectly from [`collections_abc.Sized`](https://github.com/python/cpython/blob/3.6/Lib/_collections_abc.py#L359) whose metaclass is declared to be ABCMeta. This is done as a type-checkable alternative to `Sequence.register(str)` which handles isinstance and issubclass calls.\r\n\r\nHowever derivation and register are not indistinguishable, since the metaclass structure is different. `issubclass(type(str), ABCMeta)` should be False, otherwise the definition `class A(str, Enum): pass` would fail at runtime due to inconsistent metaclass structure:\r\n```\r\n>>> from collections import Sized\r\n>>> from enum import Enum\r\n>>> class A(Sized, Enum): pass\r\n...\r\nTraceback (most recent call last):\r\n File \"\u003cstdin>\", line 1, in \u003cmodule>\r\nTypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases\r\n```\r\nThe error is that `type(Enum) is EnumMeta` and `type(Sized) is ABCMeta` and they are unrelated.\r\n\r\nThis bug has at least the following implications:\r\n* It obviously implies that `a: ABCMeta = str` typechecks.\r\n* It makes it harder to warn about inconsistent metaclass structure without false positives. This is why mypy only catches specific cases.\r\n* As exemplified above, `class A(str, Enum)` appears to have inconsistent metaclass structure, even though this is the recommended way of making an enum of strings. It might also be the case with some other builtins.","author":{"url":"https://github.com/elazarg","@type":"Person","name":"elazarg"},"datePublished":"2017-09-08T15:23:49Z","url":"https://github.com/python/typeshed/issues/1595"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:de5f1316-9efd-df1b-5e08-df7271119122 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E74A:1BEF18:1C18C6F:25C5618:6B0005E5 |
| html-safe-nonce | d82af59b16b17682a572b72493aa7aa835edf97f42e184f686bf435708b5c1ab |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNzRBOjFCRUYxODoxQzE4QzZGOjI1QzU2MTg6NkIwMDA1RTUiLCJ2aXNpdG9yX2lkIjoiMjU1OTI5MTc0NDU0MTc1MjA1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 2e462832c6a9397965256d4b260f43b8d0dd110f15187b8729ba3a2951cb3a4e |
| hovercard-subject-tag | issue:256288832 |
| 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/typeshed/1595/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e8269239b59f8f4c458f6f351a263adaa0ad4ac1db08b135027307c0d6b3b583/python/typeshed/issues/1595 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e8269239b59f8f4c458f6f351a263adaa0ad4ac1db08b135027307c0d6b3b583/python/typeshed/issues/1595 |
| og:image:alt | typeshed claims that str derives from Sequence and therefore indirectly from collections_abc.Sized whose metaclass is declared to be ABCMeta. This is done as a type-checkable alternative to Sequenc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | elazarg |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width