Title: No structural typing for mapping inside `KeysView`, `ValuesView` and `ItemsView` · Issue #14555 · python/typeshed · GitHub
Open Graph Title: No structural typing for mapping inside `KeysView`, `ValuesView` and `ItemsView` · Issue #14555 · python/typeshed
X Title: No structural typing for mapping inside `KeysView`, `ValuesView` and `ItemsView` · Issue #14555 · python/typeshed
Description: Maybe it's intended to be like that, but if some class implements all the necessary methods for KeysView, ValuesView or ItemsView, mypy will generate errors, although python code will work: from collections.abc import ItemsView, KeysView...
Open Graph Description: Maybe it's intended to be like that, but if some class implements all the necessary methods for KeysView, ValuesView or ItemsView, mypy will generate errors, although python code will work: from co...
X Description: Maybe it's intended to be like that, but if some class implements all the necessary methods for KeysView, ValuesView or ItemsView, mypy will generate errors, although python code will work: fro...
Opengraph URL: https://github.com/python/typeshed/issues/14555
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"No structural typing for mapping inside `KeysView`, `ValuesView` and `ItemsView`","articleBody":"Maybe it's intended to be like that, but if some class implements all the necessary methods for `KeysView`, `ValuesView` or `ItemsView`, `mypy` will generate errors, although `python` code will work:\n```python\nfrom collections.abc import ItemsView, KeysView, ValuesView\nfrom typing import Iterator\n\n\nclass Map[KT, VT]:\n def __init__(self, mapping: dict[KT, VT]) -> None:\n self._items: dict[KT, VT] = mapping\n\n def __getitem__(self, key: KT) -> VT:\n return self._items[key]\n\n def __len__(self) -> int:\n return len(self._items)\n\n def __iter__(self) -> Iterator[KT]:\n return iter(self._items)\n\n\na = Map[int, int]({3: 4})\nkeys = KeysView(a)\nvalues = ValuesView(a)\nitems = ItemsView(a)\n\nprint(list(keys))\nprint(len(keys))\n```\n```console\n$ mypy t.py\n\nt.py:20: error: Need type annotation for \"keys\" [var-annotated]\nt.py:20: error: Argument 1 to \"KeysView\" has incompatible type \"Map[int, int]\"; expected \"Mapping[Never, Any]\" [arg-type]\nt.py:21: error: Need type annotation for \"values\" [var-annotated]\nt.py:21: error: Argument 1 to \"ValuesView\" has incompatible type \"Map[int, int]\"; expected \"Mapping[Any, Never]\" [arg-type]\nt.py:22: error: Need type annotation for \"items\" [var-annotated]\nt.py:22: error: Argument 1 to \"ItemsView\" has incompatible type \"Map[int, int]\"; expected \"Mapping[Never, Never]\" [arg-type]\nFound 6 errors in 1 file (checked 1 source file)\n```\n\n```console\n$ python3 t.py\n\n[3]\n1\n```\nIn my case I couldn't add multiple inheritance with `Mapping` because of metaclass conflict","author":{"url":"https://github.com/Gatsik","@type":"Person","name":"Gatsik"},"datePublished":"2025-08-10T18:28:59Z","url":"https://github.com/python/typeshed/issues/14555"}
| 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:c4e17cdc-cf9b-f401-dc14-7e7562805a41 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CF4A:5537E:1B5E398:2433857:6B002937 |
| html-safe-nonce | 17a84b7549dc6a042bacf3654b9182bc8930046c9e15bccb07cd7dfb4f3c2b62 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRjRBOjU1MzdFOjFCNUUzOTg6MjQzMzg1Nzo2QjAwMjkzNyIsInZpc2l0b3JfaWQiOiIxNDQ4Nzc4NzcyNzg1NTAzMjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ef4d608e2f91899ec34efc40606b4ce8d01dd75909c9ce34f024558809a345fd |
| hovercard-subject-tag | issue:3307843433 |
| 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/14555/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cd402e8e86eea762f22302343abb2d63247f87e988448040ce360cf6f175b6a8/python/typeshed/issues/14555 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cd402e8e86eea762f22302343abb2d63247f87e988448040ce360cf6f175b6a8/python/typeshed/issues/14555 |
| og:image:alt | Maybe it's intended to be like that, but if some class implements all the necessary methods for KeysView, ValuesView or ItemsView, mypy will generate errors, although python code will work: from co... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Gatsik |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width