Title: "Type of TypedDict is ambiguous" despite tag fields being distinct Literal types · Issue #8533 · python/mypy · GitHub
Open Graph Title: "Type of TypedDict is ambiguous" despite tag fields being distinct Literal types · Issue #8533 · python/mypy
X Title: "Type of TypedDict is ambiguous" despite tag fields being distinct Literal types · Issue #8533 · python/mypy
Description: Hi there, Thanks for all the awesome work on mypy! It's brought incredible improvements to my workflow. :-) I'm reporting what could be a bug with tagged unions, which were released in 0.770. As of today this is also reproducible against...
Open Graph Description: Hi there, Thanks for all the awesome work on mypy! It's brought incredible improvements to my workflow. :-) I'm reporting what could be a bug with tagged unions, which were released in 0.770. As of...
X Description: Hi there, Thanks for all the awesome work on mypy! It's brought incredible improvements to my workflow. :-) I'm reporting what could be a bug with tagged unions, which were released in 0.77...
Opengraph URL: https://github.com/python/mypy/issues/8533
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"\"Type of TypedDict is ambiguous\" despite tag fields being distinct Literal types","articleBody":"Hi there,\r\n\r\nThanks for all the awesome work on mypy! It's brought incredible improvements to my workflow. :-)\r\n\r\nI'm reporting what could be a bug with [tagged unions](https://mypy.readthedocs.io/en/stable/literal_types.html#tagged-unions), which were released in 0.770. As of today this is also reproducible against `master`.\r\n\r\nUnfortunately this bug makes usage of tagged unions impractical. Last time I beta-tested it (a few days before the 0.770 release — perhaps around March 1st), it worked though.\r\n\r\nLet me know if there's anything I can do to help resolve!\r\n\r\n## Reproduction case\r\n\r\n```python\r\n# example.py\r\nfrom typing import TypedDict, Literal, Union\r\n\r\n# TypedDict interfaces with distinct tag fields.\r\nA = TypedDict('A', {'@type': Literal['type-a'], 'value': str})\r\nB = TypedDict('B', {'@type': Literal['type-b'], 'value': str})\r\n\r\n# Tagged union of A and B.\r\nC = Union[A, B]\r\n\r\nc: C = {\r\n '@type': 'type-a',\r\n 'value': 'test',\r\n}\r\n```\r\n\r\n## Expected behavior\r\n\r\n```console\r\n$ mypy example.py\r\n# OK - no errors\r\n```\r\n\r\n## Actual behavior\r\n\r\n```console\r\n$ mypy example.py\r\nexample.py:7:8: error: Type of TypedDict is ambiguous, could be any of (\"A\", \"B\")\r\nexample.py:7:8: error: Incompatible types in assignment (expression has type \"Dict[str, str]\", variable has type \"Union[A, B]\")\r\nFound 2 errors in 1 file (checked 1 source file)\r\n```\r\n\r\n## Version info\r\n\r\n- Python 3.8.1\r\n- Mypy 0.770\r\n- Mypy flags: none (and no `mypy.ini` in the current workdir).\r\n\r\n## Additional context\r\n\r\nI notice an ambiguous case is tested here:\r\n\r\nhttps://github.com/python/mypy/blob/1180a9dfbdf6aef9a9df3a6f7639b3cd42f0be47/test-data/unit/check-typeddict.test#L837-L846\r\n\r\nBut the snippet above is _unambiguous_: `Literal['type-a']` and `Literal['type-b']` don't overlap at all. Is the test above passing in `master`?","author":{"url":"https://github.com/florimondmanca","@type":"Person","name":"florimondmanca"},"datePublished":"2020-03-13T16:19:39Z","url":"https://github.com/python/mypy/issues/8533"}
| 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:b0c2ab81-581c-66fa-9725-dc8677e082fb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A82A:2573D7:282BA57:36E4479:6B00481D |
| html-safe-nonce | 68d5e67c12f94b6b599a96ea7f424b153924c749522f564f3ad22420811819ef |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODJBOjI1NzNENzoyODJCQTU3OjM2RTQ0Nzk6NkIwMDQ4MUQiLCJ2aXNpdG9yX2lkIjoiMTY2ODU4MTA1MjM5NTgzMzM3MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | aae42e8b6a0af4d5cfdc8291b30c686358b2104154b0dcca0ccbe0c89e932f4c |
| hovercard-subject-tag | issue:580706973 |
| 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/8533/issue_layout |
| twitter:image | https://opengraph.githubassets.com/24c2d54394b6e9a2d67eda79d6fa3338d04ec9ba253165d1f277cb11de6bfad8/python/mypy/issues/8533 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/24c2d54394b6e9a2d67eda79d6fa3338d04ec9ba253165d1f277cb11de6bfad8/python/mypy/issues/8533 |
| og:image:alt | Hi there, Thanks for all the awesome work on mypy! It's brought incredible improvements to my workflow. :-) I'm reporting what could be a bug with tagged unions, which were released in 0.770. As of... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | florimondmanca |
| 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