Title: Mypy does not correctly narrow indexing operations when using Literal or Final keys · Issue #7905 · python/mypy · GitHub
Open Graph Title: Mypy does not correctly narrow indexing operations when using Literal or Final keys · Issue #7905 · python/mypy
X Title: Mypy does not correctly narrow indexing operations when using Literal or Final keys · Issue #7905 · python/mypy
Description: Consider the following program: from typing_extensions import Literal, TypedDict from enum import Enum class Key(Enum): X = 1 Y = 2 Z = 3 class MyDict(TypedDict): key: Literal[Key.X, Key.Y] blah: int KEY: Literal["key"] = "key" d: MyDict...
Open Graph Description: Consider the following program: from typing_extensions import Literal, TypedDict from enum import Enum class Key(Enum): X = 1 Y = 2 Z = 3 class MyDict(TypedDict): key: Literal[Key.X, Key.Y] blah: i...
X Description: Consider the following program: from typing_extensions import Literal, TypedDict from enum import Enum class Key(Enum): X = 1 Y = 2 Z = 3 class MyDict(TypedDict): key: Literal[Key.X, Key.Y] blah: i...
Opengraph URL: https://github.com/python/mypy/issues/7905
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Mypy does not correctly narrow indexing operations when using Literal or Final keys","articleBody":"Consider the following program:\r\n\r\n```\r\nfrom typing_extensions import Literal, TypedDict\r\nfrom enum import Enum\r\n\r\nclass Key(Enum):\r\n X = 1\r\n Y = 2\r\n Z = 3\r\n\r\nclass MyDict(TypedDict):\r\n key: Literal[Key.X, Key.Y]\r\n blah: int\r\n\r\nKEY: Literal[\"key\"] = \"key\"\r\n\r\nd: MyDict\r\n\r\nif d[\"key\"] is Key.X:\r\n reveal_type(d[\"key\"]) # note: Revealed type is 'Literal[Key.X]'\r\n\r\nif d[KEY] is Key.X:\r\n reveal_type(d[KEY]) # note: Revealed type is 'Literal[Key.X, Key.Y]'\r\n\r\n```\r\n\r\nMypy is currently capable of narrowing expressions like `d[\"key\"]`, which we can see in the first expression.\r\n\r\nSo, it's natural to assume that mypy would be able to do the same for the second since the two programs are theoretically identical -- but we can't.\r\n\r\nThe root cause has to do with the \"literal\" subsystem (which is not to be confused with the Literal types subsystem) here: https://github.com/python/mypy/blob/master/mypy/literals.py#L65\r\n\r\nThe index in the second example is a NameExpr, which causes the if statement to evaluate to false and return a `LITERAL_NO`. This then makes the narrowing logic rule out `d[KEY]` as a candidate for narrowing in https://github.com/python/mypy/blob/master/mypy/checker.py#L3775.\r\n\r\nI'm not really sure what the best way of fixing this would be. The natural solution would be to also pass along the expression type into the `literal(...)` function, but that seems very annoying to do. I'm also not entirely sure whether this is even a sound narrowing: I'm not very familiar with the \"literals\" subsystem, or how it's meant to interact with Literal types.","author":{"url":"https://github.com/Michael0x2a","@type":"Person","name":"Michael0x2a"},"datePublished":"2019-11-08T03:20:55Z","url":"https://github.com/python/mypy/issues/7905"}
| 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:e9c60c19-4721-8def-c772-0b5b2260c9a6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9E26:22BD05:22C04AB:2F22796:6B0029C6 |
| html-safe-nonce | 0b4250ff751f85815f7de4c1f4509135f118259ae51551655f788594dd74f9ba |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RTI2OjIyQkQwNToyMkMwNEFCOjJGMjI3OTY6NkIwMDI5QzYiLCJ2aXNpdG9yX2lkIjoiNDExNTIwNzA3NjYxODE4NTE1OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2796bc990f71d7d8c7da9c3bde0888fab6f5607de4a2cb61c565e928f1bba6b1 |
| hovercard-subject-tag | issue:519635729 |
| 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/7905/issue_layout |
| twitter:image | https://opengraph.githubassets.com/11e74727f2c047e0b89407bf7c02762864033fe484e53e131777f781d9644799/python/mypy/issues/7905 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/11e74727f2c047e0b89407bf7c02762864033fe484e53e131777f781d9644799/python/mypy/issues/7905 |
| og:image:alt | Consider the following program: from typing_extensions import Literal, TypedDict from enum import Enum class Key(Enum): X = 1 Y = 2 Z = 3 class MyDict(TypedDict): key: Literal[Key.X, Key.Y] blah: i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Michael0x2a |
| 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