Title: Type detection doesn't work with TypedDict · Issue #7344 · python/mypy · GitHub
Open Graph Title: Type detection doesn't work with TypedDict · Issue #7344 · python/mypy
X Title: Type detection doesn't work with TypedDict · Issue #7344 · python/mypy
Description: Are you reporting a bug, or opening a feature request? Bug Code that reproduces the issue: from typing import Union, List, Iterable from typing_extensions import Literal, TypedDict # Goal here is to add mypy type so that # if the dict's ...
Open Graph Description: Are you reporting a bug, or opening a feature request? Bug Code that reproduces the issue: from typing import Union, List, Iterable from typing_extensions import Literal, TypedDict # Goal here is t...
X Description: Are you reporting a bug, or opening a feature request? Bug Code that reproduces the issue: from typing import Union, List, Iterable from typing_extensions import Literal, TypedDict # Goal here is t...
Opengraph URL: https://github.com/python/mypy/issues/7344
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type detection doesn't work with TypedDict","articleBody":"* Are you reporting a bug, or opening a feature request? **Bug**\r\n* Code that reproduces the issue:\r\n```python3\r\nfrom typing import Union, List, Iterable\r\nfrom typing_extensions import Literal, TypedDict\r\n\r\n# Goal here is to add mypy type so that\r\n# if the dict's key is 'a' or 'b' mypy knows\r\n# that the value should be int or str.\r\nABDictT = TypedDict('ABDictT', {\r\n 'key': Literal['a', 'b'],\r\n 'value': Union[str, int]\r\n})\r\n\r\n# If key is 'c', or 'd' then mypy should know\r\n# value should be str, or int list.\r\nCDDictT = TypedDict('CDDictT', {\r\n 'key': Literal['c', 'd'],\r\n 'value': Union[str, Iterable[int]],\r\n})\r\n\r\nValueDictElementT = Union[ABDictT, CDDictT]\r\nValuesDictT = Iterable[ValueDictElementT]\r\n\r\ndef some_func(values: ValuesDictT) -> None:\r\n for element in values:\r\n if element['key'] == 'a':\r\n # The reveal_type here show the type to be\r\n # Union[str, int, Iterable[int]] since we\r\n # did a check that element['key'] is 'a' mypy\r\n # should be able to tell that the type is\r\n # ABDictT here?\r\n reveal_type(element['value'])\r\n```\r\n\r\n* What is the actual behavior/output?\r\n```\r\n$ mypy --strict repro.py\r\nrepro.py:30: note: Revealed type is 'Union[builtins.str, builtins.int, typing.Iterable[builtins.int]]'\r\n```\r\n\r\n* What is the behavior/output you expect?\r\n`mypy` should be able to tell that the type is `ABDictT` after checking that the key is `a` and therefore the output on the `reveal_type` should be `Union[str, int]`.\r\n```\r\n$ mypy --strict repro.py\r\nrepro.py:30: note: Revealed type is 'Union[builtins.str, builtins.int]'\r\n```\r\n\r\n* What are the versions of mypy and Python you are using? **Python: 3.6.8, mypy: 0.720**\r\n Do you see the same issue after installing mypy from Git master? **Yes**\r\n* What are the mypy flags you are using? (For example --strict-optional) **I did use `--strict` but it can also be reproduced with no flags.**\r\n","author":{"url":"https://github.com/priyank-p","@type":"Person","name":"priyank-p"},"datePublished":"2019-08-14T18:50:20Z","url":"https://github.com/python/mypy/issues/7344"}
| 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:5a0b9fb7-bdef-5fcc-6fa5-d5cf1080d8ea |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 943E:2E9DBB:20CBA06:2CB88CB:6B0023F9 |
| html-safe-nonce | 44eac32da2854f72e57f366ed802b11803f43056920c667ee4807df8f36a275d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NDNFOjJFOURCQjoyMENCQTA2OjJDQjg4Q0I6NkIwMDIzRjkiLCJ2aXNpdG9yX2lkIjoiNTU2MDc3Mjc1MzUzMjkyMDgyNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 52ee7e47200c6776ef0fead7986118e572f909437becf8374bdc01e9a69fcc52 |
| hovercard-subject-tag | issue:480833897 |
| 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/7344/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9a021dfc7c862e0aa7759d76470fc1edad24e24bbc07e00b676fa688cee879e0/python/mypy/issues/7344 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9a021dfc7c862e0aa7759d76470fc1edad24e24bbc07e00b676fa688cee879e0/python/mypy/issues/7344 |
| og:image:alt | Are you reporting a bug, or opening a feature request? Bug Code that reproduces the issue: from typing import Union, List, Iterable from typing_extensions import Literal, TypedDict # Goal here is t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | priyank-p |
| 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