Title: Narrowing unions with Literal and TypedDict · Issue #7944 · python/mypy · GitHub
Open Graph Title: Narrowing unions with Literal and TypedDict · Issue #7944 · python/mypy
X Title: Narrowing unions with Literal and TypedDict · Issue #7944 · python/mypy
Description: I expected the following code to narrow the type of the argument, but it is unable to. from mypy_extensions import TypedDict from typing_extensions import Literal TaggedStr = TypedDict('TaggedStr', {'tag': Literal['str'], 's': str}) Tagg...
Open Graph Description: I expected the following code to narrow the type of the argument, but it is unable to. from mypy_extensions import TypedDict from typing_extensions import Literal TaggedStr = TypedDict('TaggedStr',...
X Description: I expected the following code to narrow the type of the argument, but it is unable to. from mypy_extensions import TypedDict from typing_extensions import Literal TaggedStr = TypedDict('TaggedS...
Opengraph URL: https://github.com/python/mypy/issues/7944
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Narrowing unions with Literal and TypedDict","articleBody":"I expected the following code to narrow the type of the argument, but it is unable to.\r\n```python\r\nfrom mypy_extensions import TypedDict\r\nfrom typing_extensions import Literal\r\n\r\nTaggedStr = TypedDict('TaggedStr', {'tag': Literal['str'], 's': str})\r\nTaggedInt = TypedDict('TaggedInt', {'tag': Literal['int'], 'i': int})\r\n\r\ndef do_tagged(x: Union[TaggedStr, TaggedInt]):\r\n if x['tag'] == 'str':\r\n reveal_type(x)\r\n else:\r\n reveal_type(x)\r\n```\r\n\r\n* What is the actual behavior/output?\r\nNo narrowing occurs:\r\n```\r\nexample.py:10: note: Revealed type is 'Union[TypedDict('example.TaggedStr', {'tag': Literal['str'], 's': builtins.str}), TypedDict('example.TaggedInt', {'tag': Literal['int'], 'i': builtins.int})]'\r\nexample.py:12: note: Revealed type is 'Union[TypedDict('example.TaggedStr', {'tag': Literal['str'], 's': builtins.str}), TypedDict('example.TaggedInt', {'tag': Literal['int'], 'i': builtins.int})]'\r\n```\r\n\r\n* What is the behavior/output you expect?\r\nI'd expect the comparisons against literal keys to allow narrowing the types.\r\n```\r\nexample.py:10: note: Revealed type is 'TypedDict('example.TaggedStr', {'tag': Literal['str'], 's': builtins.str})'\r\nexample.py:12: note: Revealed type is 'TypedDict('example.TaggedInt', {'tag': Literal['int'], 'i': builtins.int})'\r\n```\r\n\r\n* What are the versions of mypy and Python you are using?\r\nmypy 0.740, Python 3.7.4","author":{"url":"https://github.com/porglezomp","@type":"Person","name":"porglezomp"},"datePublished":"2019-11-13T02:24:41Z","url":"https://github.com/python/mypy/issues/7944"}
| 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:f08c8858-6650-c32a-d05c-5f8dba8f1eb1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A706:2DB972:2FBB555:3F41908:6B002C3D |
| html-safe-nonce | b32107066f7a8f5559caec7fa99a3024362e68984d4f85ab4be5c91d277a39e6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzA2OjJEQjk3MjoyRkJCNTU1OjNGNDE5MDg6NkIwMDJDM0QiLCJ2aXNpdG9yX2lkIjoiNjA4MTc5NDgyMzg5MzgyMjUyNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | aec8fa44ce8d401a779a7525a34267df736f61c08aac4be6e31f27b6a57087e6 |
| hovercard-subject-tag | issue:521916960 |
| 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/7944/issue_layout |
| twitter:image | https://opengraph.githubassets.com/540c908742f27fa1590e838f9e14c2b6ff0d5042990d1e201cec9f4e41ea6023/python/mypy/issues/7944 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/540c908742f27fa1590e838f9e14c2b6ff0d5042990d1e201cec9f4e41ea6023/python/mypy/issues/7944 |
| og:image:alt | I expected the following code to narrow the type of the argument, but it is unable to. from mypy_extensions import TypedDict from typing_extensions import Literal TaggedStr = TypedDict('TaggedStr',... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | porglezomp |
| 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