Title: Support inferring dict literals to be a union of TypedDicts · Issue #13274 · python/mypy · GitHub
Open Graph Title: Support inferring dict literals to be a union of TypedDicts · Issue #13274 · python/mypy
X Title: Support inferring dict literals to be a union of TypedDicts · Issue #13274 · python/mypy
Description: Feature from __future__ import annotations from typing import TypedDict class A(TypedDict, total=False): name: str class B(TypedDict, total=False): name: str def foo(data: A | B) -> None: ... # this should not report an error foo({"name"...
Open Graph Description: Feature from __future__ import annotations from typing import TypedDict class A(TypedDict, total=False): name: str class B(TypedDict, total=False): name: str def foo(data: A | B) -> None: ... # thi...
X Description: Feature from __future__ import annotations from typing import TypedDict class A(TypedDict, total=False): name: str class B(TypedDict, total=False): name: str def foo(data: A | B) -> None: ... # ...
Opengraph URL: https://github.com/python/mypy/issues/13274
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support inferring dict literals to be a union of TypedDicts","articleBody":"**Feature**\r\n\r\n```py\r\nfrom __future__ import annotations\r\nfrom typing import TypedDict\r\n\r\nclass A(TypedDict, total=False):\r\n name: str\r\n\r\nclass B(TypedDict, total=False):\r\n name: str\r\n\r\ndef foo(data: A | B) -> None:\r\n ...\r\n\r\n# this should not report an error\r\nfoo({\"name\": \"Robert\"}) # error: Type of TypedDict is ambiguous, could be any of (\"A\", \"B\")\r\n```\r\n\r\n**Pitch**\r\n\r\nWhile the code example provided is nonsensical, I ran into this error using real world types. In the more abstract sense, inferring the `{\"name\": \"foo\"}` expression to be `A | B` should be supported as it is type safe. On top of that, Pyright also supports this pattern and infers the expression correctly.\r\n\r\nThere is also a \"bug\" with the check in that it doesn't respect nested types, consider this case:\r\n\r\n```py\r\nfrom __future__ import annotations\r\nfrom typing import TypedDict\r\n\r\nclass A(TypedDict, total=False):\r\n foo: C\r\n\r\nclass B(TypedDict, total=False):\r\n foo: D\r\n\r\nclass C(TypedDict, total=False):\r\n c: str\r\n\r\nclass D(TypedDict, total=False):\r\n d: str\r\n\r\ndef foo(data: A | B) -> None:\r\n ...\r\n\r\nfoo({\"foo\": {\"c\": \"foo\"}}) # error: Type of TypedDict is ambiguous, could be any of (\"A\", \"B\")\r\n```\r\n\r\nIn this case, there is no ambiguity, the expression can be safely inferred to be of type `A`.","author":{"url":"https://github.com/RobertCraigie","@type":"Person","name":"RobertCraigie"},"datePublished":"2022-07-28T18:15:16Z","url":"https://github.com/python/mypy/issues/13274"}
| 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:bf6c9e84-6355-0ee0-78f1-806855a62054 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CBEC:3AC034:42AB27:5A3690:6B002C93 |
| html-safe-nonce | 42755edf1863534f96ed22c22ce44f0595610139551a6c773a0a38beb61454d8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQkVDOjNBQzAzNDo0MkFCMjc6NUEzNjkwOjZCMDAyQzkzIiwidmlzaXRvcl9pZCI6IjQxNDc3NjQ3ODg0NDI3MDUwNDMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8dd5ccbb7597b46ce59b06b37f031422c17614a1fc171243700f5dbbadee2887 |
| hovercard-subject-tag | issue:1321317099 |
| 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/13274/issue_layout |
| twitter:image | https://opengraph.githubassets.com/24c2d54394b6e9a2d67eda79d6fa3338d04ec9ba253165d1f277cb11de6bfad8/python/mypy/issues/13274 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/24c2d54394b6e9a2d67eda79d6fa3338d04ec9ba253165d1f277cb11de6bfad8/python/mypy/issues/13274 |
| og:image:alt | Feature from __future__ import annotations from typing import TypedDict class A(TypedDict, total=False): name: str class B(TypedDict, total=False): name: str def foo(data: A | B) -> None: ... # thi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | RobertCraigie |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width