Title: mypy should support optional tagged unions · Issue #8925 · python/mypy · GitHub
Open Graph Title: mypy should support optional tagged unions · Issue #8925 · python/mypy
X Title: mypy should support optional tagged unions · Issue #8925 · python/mypy
Description: Report Type: Bug Example: https://mypy-play.net/?mypy=latest&python=3.8&gist=529e99849ebe65749d8568b743f21050 from typing import Literal, TypedDict, Union class NewJob(TypedDict): event_type: Literal["new-job"] job_description: str class...
Open Graph Description: Report Type: Bug Example: https://mypy-play.net/?mypy=latest&python=3.8&gist=529e99849ebe65749d8568b743f21050 from typing import Literal, TypedDict, Union class NewJob(TypedDict): event_type: Liter...
X Description: Report Type: Bug Example: https://mypy-play.net/?mypy=latest&python=3.8&gist=529e99849ebe65749d8568b743f21050 from typing import Literal, TypedDict, Union class NewJob(TypedDict): event_typ...
Opengraph URL: https://github.com/python/mypy/issues/8925
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mypy should support optional tagged unions","articleBody":"__Report Type:__ Bug\r\n\r\n__Example:__ https://mypy-play.net/?mypy=latest&python=3.8&gist=529e99849ebe65749d8568b743f21050\r\n\r\n```python\r\nfrom typing import Literal, TypedDict, Union\r\n\r\nclass NewJob(TypedDict):\r\n event_type: Literal[\"new-job\"]\r\n job_description: str\r\n\r\nclass CancelJob(TypedDict):\r\n event_type: Literal[\"cancel-job\"]\r\n job_id: int\r\n\r\nrequest: Union[NewJob, CancelJob, None]\r\n\r\n# works\r\nif request is not None:\r\n if request[\"event_type\"] == \"new-job\":\r\n print(\"Starting new job\", request[\"job_description\"])\r\n\r\n# fails with `main.py:20: error: TypedDict \"CancelJob\" has no key 'job_description'`\r\nif request is not None and request[\"event_type\"] == \"new-job\":\r\n print(\"Starting new job\", request[\"job_description\"])\r\n```\r\n\r\n* What is the actual behavior/output?\r\nMypy does not discriminate `TypedDicts` when `None` is part of the `Union`.\r\n* What is the behavior/output you expect?\r\nMypy narrows a `Union` of `TypedDict` and `None` like it does for unions of `TypedDict`s.\r\n* What are the versions of mypy and Python you are using?\r\nmypy 0.770\r\nPython 3.7.6\r\n* Do you see the same issue after installing mypy from Git master?\r\nI have not tried master.\r\n\r\n\r\nThis is a small issue because there is an easy workaround of checking for `None` before checking the tag.","author":{"url":"https://github.com/chdsbd","@type":"Person","name":"chdsbd"},"datePublished":"2020-05-30T14:31:11Z","url":"https://github.com/python/mypy/issues/8925"}
| 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:dbb52240-da38-29c9-40fa-441b7d917f7e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B258:27FBAD:2AA242:3A302A:6B0069A9 |
| html-safe-nonce | b56626d3268754178972032cf1b83d940184ba7d37445fdb720cbd92ca8ccba0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjU4OjI3RkJBRDoyQUEyNDI6M0EzMDJBOjZCMDA2OUE5IiwidmlzaXRvcl9pZCI6IjIwNjkyNDU3MzUyNTcyNzA2OTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 55e45a9dccfbe3c57ac97168b1a89f92faddc2827b3bfb3a3fba08943a04409a |
| hovercard-subject-tag | issue:627743355 |
| 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/8925/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cab14f9d93ed9146a754663b278c54822304941102c90329fdfe7687b8cf5d7c/python/mypy/issues/8925 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cab14f9d93ed9146a754663b278c54822304941102c90329fdfe7687b8cf5d7c/python/mypy/issues/8925 |
| og:image:alt | Report Type: Bug Example: https://mypy-play.net/?mypy=latest&python=3.8&gist=529e99849ebe65749d8568b743f21050 from typing import Literal, TypedDict, Union class NewJob(TypedDict): event_type: Liter... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chdsbd |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6527e91c60c0b29d16606ff32ac5df8ecdc37a2a62205ea057a14d7762c870fa |
| 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 | 46f1cd2ae3cd8ae1a81019b5d619742e76f13bbd |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width