Title: Add support for TypedDict extra_items= and closed= (PEP 728) · Issue #18176 · python/mypy · GitHub
Open Graph Title: Add support for TypedDict extra_items= and closed= (PEP 728) · Issue #18176 · python/mypy
X Title: Add support for TypedDict extra_items= and closed= (PEP 728) · Issue #18176 · python/mypy
Description: Feature PEP 728 proposes changes to TypedDict, including the ability to specify the types of unspecified keys. This has been implemented in typing_extensions with the __extra_items__ keyword since version 4.10. I would like to see suppor...
Open Graph Description: Feature PEP 728 proposes changes to TypedDict, including the ability to specify the types of unspecified keys. This has been implemented in typing_extensions with the __extra_items__ keyword since ...
X Description: Feature PEP 728 proposes changes to TypedDict, including the ability to specify the types of unspecified keys. This has been implemented in typing_extensions with the __extra_items__ keyword since ...
Opengraph URL: https://github.com/python/mypy/issues/18176
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add support for TypedDict extra_items= and closed= (PEP 728)","articleBody":"**Feature**\r\n\r\n[PEP 728](https://peps.python.org/pep-0728/) proposes changes to `TypedDict`, including the ability to specify the types of unspecified keys.\r\n\r\nThis has been [implemented in `typing_extensions` with the `__extra_items__` keyword](https://typing-extensions.readthedocs.io/en/latest/index.html#typing_extensions.TypedDict.__extra_items__) since version 4.10.\r\n\r\nI would like to see support for this added to `mypy`.\r\n\r\n**Pitch**\r\n\r\nI am working on using mypy to validate Python code associated with rest API services. It is not uncommon for the OpenAPI specs we use to include objects whose `additionalProperties` values map to schemas. This is how OpenAPI handles the case of objects whose keywords are not known in advance, but whose values must be of a particular type.\r\n\r\nI want to be able to type annotate these schemas in Python, but currently there is no good mechanism for doing this. That is one of the problems that PEP 728 attempts to solve.\r\n\r\nCurrently with mypy I have found a few ways to handle this (in the absence of adding the requested support):\r\n\r\n1. As noted in the PEP, one can always suppress the `typeddict-unknown-key` errors. But of course that means no type checking is happening for the additional keys being used.\r\n2. I haven't coded it up yet, but I suspect with overloading I could probably come up with something that works for this. Basically, I could overload the getitem/setitem methods in a Protocol, specifying the correct return types for the various pre-defined keys (as Literals), and then add \"generic\" fallback get/set item methods for the \"extra key type\".\r\n3. It's possible I could come up with some Frankensteining of TypedDicts and defaultdicts that accomplishes this. Although that has the downside that the dict would always return a value regardless of the key, and that's not desirable.\r\n\r\nFor both 2 and 3, it would definitely be less clean and much lengthier than using the solution in `typing_extensions`.\r\n\r\nFrom my research when trying to figure out how to handle this problem, I get the impression I am not the only one trying to handle this case. That is also indicated by the fact that the current JSON schema and OpenAPI spec includes this ability. And of course, that is part of the motivation for the PEP being opened in the first place.","author":{"url":"https://github.com/mharding-hpe","@type":"Person","name":"mharding-hpe"},"datePublished":"2024-11-21T20:48:00.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/18176/mypy/issues/18176"}
| 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:3b0a99d6-d736-25d7-d9f5-3a18d21a1719 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B172:5B83E:59779D:81420F:6A4D13EF |
| html-safe-nonce | 305e76568c1f1da6f3a6769bf0efbd6892292796be475f5f28c21b90dbcd2af8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMTcyOjVCODNFOjU5Nzc5RDo4MTQyMEY6NkE0RDEzRUYiLCJ2aXNpdG9yX2lkIjoiNjgwOTU2NzMxNjk3MzI2MTgwNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | af1bc126927e89a439d06c108c43c7e8a73ab2cbf70631bda286e58c0c9dc8f4 |
| hovercard-subject-tag | issue:2680908211 |
| 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/mypy/18176/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a58713ddc2408135050345223ccb9049034d527b6aa83f17654c073ca6404a61/python/mypy/issues/18176 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a58713ddc2408135050345223ccb9049034d527b6aa83f17654c073ca6404a61/python/mypy/issues/18176 |
| og:image:alt | Feature PEP 728 proposes changes to TypedDict, including the ability to specify the types of unspecified keys. This has been implemented in typing_extensions with the __extra_items__ keyword since ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mharding-hpe |
| hostname | github.com |
| expected-hostname | github.com |
| None | f02420044fa3730a0607f4a52546bdd73f5ae60187ef57d284ecb6d7f3f1d880 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| 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 | f0b6a62c1f0be1086cb0b5b077b82cebe520eb42 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width