Title: Narrowing Union[TypedDict] with `in` keyword · Issue #11080 · python/mypy · GitHub
Open Graph Title: Narrowing Union[TypedDict] with `in` keyword · Issue #11080 · python/mypy
X Title: Narrowing Union[TypedDict] with `in` keyword · Issue #11080 · python/mypy
Description: Feature mypy should narrow Union[TypedDict] when using in keyword. Pitch from typing import Literal, TypedDict, Union Key = Literal['a', 'b'] class A(TypedDict): a: int class B(TypedDict): b: int def try_print(x: Union[A, B], key: Key): ...
Open Graph Description: Feature mypy should narrow Union[TypedDict] when using in keyword. Pitch from typing import Literal, TypedDict, Union Key = Literal['a', 'b'] class A(TypedDict): a: int class B(TypedDict): b: int d...
X Description: Feature mypy should narrow Union[TypedDict] when using in keyword. Pitch from typing import Literal, TypedDict, Union Key = Literal['a', 'b'] class A(TypedDict): a: int class B(Type...
Opengraph URL: https://github.com/python/mypy/issues/11080
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Narrowing Union[TypedDict] with `in` keyword","articleBody":"**Feature**\r\n\r\nmypy should narrow Union[TypedDict] when using `in` keyword.\r\n\r\n**Pitch**\r\n\r\n```python\r\nfrom typing import Literal, TypedDict, Union\r\n\r\nKey = Literal['a', 'b']\r\n\r\nclass A(TypedDict):\r\n a: int\r\n \r\nclass B(TypedDict):\r\n b: int\r\n\r\ndef try_print(x: Union[A, B], key: Key):\r\n if key in x:\r\n print(x[key])\r\n```\r\n\r\nThe example above triggers errors in mypy: \r\n> main.py:13: error: TypedDict \"A\" has no key \"b\"\r\n> main.py:13: error: TypedDict \"B\" has no key \"a\"\r\n\r\nIn theory, `if key in x` condition should provide enough information for mypy to narrow the type of parameter `x`. It should know that `x` is aligned with `key`, so the print statement is type safe.\r\n\r\nCurrent workaround is to manually cast the type as below, \r\n```\r\n if key == 'a':\r\n print(cast(A, x)[key])\r\n else:\r\n print(cast(B, x)[key])\r\n```\r\nbut when there's much more keys and TypedDicts involved, this approach would become verbose and messy.\r\n\r\nIf mypy can add this narrowing feature, it will allow more flexibility for TypedDict Union use cases","author":{"url":"https://github.com/devin-git","@type":"Person","name":"devin-git"},"datePublished":"2021-09-09T07:25:47Z","url":"https://github.com/python/mypy/issues/11080"}
| 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:e4aa23d4-a2e6-83f6-0c34-f75f3221d260 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E648:4D1C6:21E9AB7:2E23703:6B002864 |
| html-safe-nonce | fe164e4dd68aeac39e85b9dfb59c96c8284bc6b853e2609bf2ccba5103b67d1b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNjQ4OjREMUM2OjIxRTlBQjc6MkUyMzcwMzo2QjAwMjg2NCIsInZpc2l0b3JfaWQiOiI2NjUxNTQyNzAxMjI3Mzg2OTgwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 242af343c61e715f15d28ee697470aa33460e40eace86107b8fc939c47d02919 |
| hovercard-subject-tag | issue:991893760 |
| 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/11080/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0281085943840478af75bd342797e30047a4ad4ffe3ea98311c31ef8d252db48/python/mypy/issues/11080 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0281085943840478af75bd342797e30047a4ad4ffe3ea98311c31ef8d252db48/python/mypy/issues/11080 |
| og:image:alt | Feature mypy should narrow Union[TypedDict] when using in keyword. Pitch from typing import Literal, TypedDict, Union Key = Literal['a', 'b'] class A(TypedDict): a: int class B(TypedDict): b: int d... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | devin-git |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width