Title: (🎁) Add a `StubOnlyBase` decorator for types that don't actually extend things, but do in the stubs (like `Generic`) · Issue #956 · python/typing · GitHub
Open Graph Title: (🎁) Add a `StubOnlyBase` decorator for types that don't actually extend things, but do in the stubs (like `Generic`) · Issue #956 · python/typing
X Title: (🎁) Add a `StubOnlyBase` decorator for types that don't actually extend things, but do in the stubs (like `Generic`) · Issue #956 · python/typing
Description: There are existing classes that extend things in an abstract way, like ABCs, and things that are Generic in theory but not in reality, for example all the collections (the collection classes are not Generics, they define __class_getitem_...
Open Graph Description: There are existing classes that extend things in an abstract way, like ABCs, and things that are Generic in theory but not in reality, for example all the collections (the collection classes are no...
X Description: There are existing classes that extend things in an abstract way, like ABCs, and things that are Generic in theory but not in reality, for example all the collections (the collection classes are no...
Opengraph URL: https://github.com/python/typing/issues/956
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"(🎁) Add a `StubOnlyBase` decorator for types that don't actually extend things, but do in the stubs (like `Generic`)","articleBody":"There are existing classes that extend things in an abstract way, like ABCs, and things that are `Generic` in theory but not in reality, for example all the `collections` (the collection classes are not `Generic`s, they define `__class_getitem__`) and some are still not subscribe-able but state they are in the stubs, for instance `_collections_abc.dict_keys` or any of the collections pre 3.9.\r\n\r\nIt could be used something like:\r\n```py\r\n# _collections_abc.pyi\r\n@StubOnlyBase(KeysView[_KT_co], Generic[_KT_co, _VT_co])\r\nclass dict_keys:\r\n ...\r\n```\r\n```py\r\n# typing.pyi\r\n\r\n@StubOnlyBase(Generic[_T])\r\nclass Container:\r\n if sys.version_info >= (3, 9):\r\n def __class_getitem__(???) -> ???:\r\n\r\n@StubOnlyBase(Collection[_KT], Generic[_KT, _VT_co])\r\nclass Mapping(Collection):\r\n ...\r\n```\r\n\r\n`Mapping` doesn't actually extend `Generic`, but `Container` defines `__class_getitem__`, so that would also need to be updated in the stubs.\r\n\r\nThis might help resolve https://github.com/python/mypy/issues/3186, but still indicate that it's not actually one of the bases.\r\n```py\r\n# builtins.pyi\r\n@StubOnlyBase(numbers.Integral)\r\nclass int:\r\n ...\r\n```\r\n\r\nI think a change like this would make the stubs much more understandable and closer to the actual implementation, also fixing issues about older python versions not working properly with the current stubs(https://github.com/python/mypy/issues/11529).\r\n\r\nI personally find it very confusing when referencing the stubs that the bases are often false.\r\n\r\nNo idea how this would be used in a real life annotation, but a workaround that works is:\r\n```py\r\nfrom typing import _alias # type: ignore[attr-defined]\r\nfrom typing import TYPE_CHECKING\r\nfrom _collections_abc import dict_keys\r\n\r\nif not TYPE_CHECKING:\r\n dict_keys = _alias(dict_keys, 2)\r\n\r\ndef foo(dk: dict_keys[str, int]) -> None: ...\r\n```\r\n\r\nhttps://github.com/python/typeshed/pull/6312#issuecomment-976036379\r\nhttps://github.com/python/typeshed/issues/6257\r\n\r\nObligatory shill to #953, which I think would render this moot.","author":{"url":"https://github.com/KotlinIsland","@type":"Person","name":"KotlinIsland"},"datePublished":"2021-11-23T05:15:37Z","url":"https://github.com/python/typing/issues/956"}
| 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:96950d5b-7d8d-733d-92a0-37e2b49f64cf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F84:1AD4B2:85AB:B1E7:6B0016E4 |
| html-safe-nonce | 997c442cbfb2c7d3d3c6907c1912687502fee031397cdd8f069ea2de46ff01ea |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Rjg0OjFBRDRCMjo4NUFCOkIxRTc6NkIwMDE2RTQiLCJ2aXNpdG9yX2lkIjoiODY3NjA4MTQ0MTUwMDU2NzI2OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 48968570a7e8b236aed1abe03182ebcb55d7ce42fc1ad04ae2b42328da1ec098 |
| hovercard-subject-tag | issue:1060848005 |
| 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/typing/956/issue_layout |
| twitter:image | https://opengraph.githubassets.com/04e47191b2e0da765f10f69486e20e6866f65815fc8563ee77c18fde8a03a47e/python/typing/issues/956 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/04e47191b2e0da765f10f69486e20e6866f65815fc8563ee77c18fde8a03a47e/python/typing/issues/956 |
| og:image:alt | There are existing classes that extend things in an abstract way, like ABCs, and things that are Generic in theory but not in reality, for example all the collections (the collection classes are no... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | KotlinIsland |
| hostname | github.com |
| expected-hostname | github.com |
| None | a2dd2291b1b13cc9c06f27ec92fa3dbaf34116fc24c9347b8539db61884c59f4 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typing git https://github.com/python/typing.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 24604008 |
| octolytics-dimension-repository_nwo | python/typing |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24604008 |
| octolytics-dimension-repository_network_root_nwo | python/typing |
| 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 | 70121d4e855ee0bb7b75cf15d061d91f8863c0f8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width