Title: Variable loses type inference after passing through dictionary of known type · Issue #9369 · python/mypy · GitHub
Open Graph Title: Variable loses type inference after passing through dictionary of known type · Issue #9369 · python/mypy
X Title: Variable loses type inference after passing through dictionary of known type · Issue #9369 · python/mypy
Description: 🐛 Bug Report (Possibly related to #6755?) When a variable is declared to be of a type including Optional, mypy correctly infers that it is not None when passing through an assert indicating as such. However, if mapped through a dictionar...
Open Graph Description: 🐛 Bug Report (Possibly related to #6755?) When a variable is declared to be of a type including Optional, mypy correctly infers that it is not None when passing through an assert indicating as such...
X Description: 🐛 Bug Report (Possibly related to #6755?) When a variable is declared to be of a type including Optional, mypy correctly infers that it is not None when passing through an assert indicating as such...
Opengraph URL: https://github.com/python/mypy/issues/9369
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Variable loses type inference after passing through dictionary of known type","articleBody":"\u003c!--\r\nNote: If the problem you are reporting is about a specific library function, then the typeshed tracker is better suited\r\nfor this report: https://github.com/python/typeshed/issues\r\n-->\r\n\r\n## 🐛 Bug Report\r\n\r\n(Possibly related to #6755?)\r\n\r\nWhen a variable is declared to be of a type including `Optional`, mypy correctly infers that it is _not_ `None` when passing through an assert indicating as such. However, if mapped through a dictionary with a known type (either by inference or declaration), mypy loses that inference and reverts to the original type definition.\r\n\r\n## To Reproduce\r\n\r\n```python\r\n\r\nfrom typing import (\r\n\tDict,\r\n\tOptional,\r\n)\r\n\r\ndef check1(x: Optional[str]) -> str:\r\n\tassert x # x: str\r\n\r\n\treturn x # x: str\r\n\r\ndef check2(x: Optional[str]) -> str:\r\n\tassert x # x: str\r\n\r\n\tx_map: Dict[str, str] = {\r\n\t\t\"x\": \"y\"\r\n\t}\r\n\tx = x_map.get(x, x) # x: Optional[str] ??\r\n\r\n\treturn x # x: Optional[str] ??\r\n\r\ndef check3(x: Optional[str]) -> str:\r\n\tassert x # x: str\r\n\r\n\tx_map: Dict[str, str] = {\r\n\t\t\"x\": \"y\"\r\n\t}\r\n\tx = x_map.get(x, x) # x: Optional[str] ??\r\n\r\n\tassert x # x: str\r\n\r\n\treturn x # x: str\r\n```\r\n\r\n## Expected Behavior\r\n\r\n\u003c!--\r\n How did you expect your project to behave?\r\n It’s fine if you’re not sure your understanding is correct.\r\n Write down what you thought would happen. If you just expected no errors, you can delete this section.\r\n-->\r\n\r\nThe type of `x` should remain inferred as `str` in `check2`, because both possible return values from `x_map.get(x, x)` are known to be `str`.\r\n\r\n## Actual Behavior\r\n\r\n\u003c!--\r\n Did something go wrong?\r\n Is something broken, or not behaving as you expected?\r\n-->\r\n\r\n> 20: error: Incompatible return value type (got \"Optional[str]\", expected \"str\")\r\n\r\n## Your Environment\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\r\n\r\n- Mypy version used: mypy 0.782\r\n- Mypy command-line flags: `mypy --strict`\r\n- Python version used: Python 3.6.11\r\n\r\n\u003c!--\r\nYou can freely edit this text, please remove all the lines\r\nyou believe are unnecessary.\r\n-->\r\n","author":{"url":"https://github.com/GPHemsley","@type":"Person","name":"GPHemsley"},"datePublished":"2020-08-28T06:55:29Z","url":"https://github.com/python/mypy/issues/9369"}
| 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:a8cbff22-dd70-03d6-44c6-b2775148929e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 93C2:18B602:7C1DD:AE79F:6B005171 |
| html-safe-nonce | f19620d63fd028ac0fb683e2b6715a60bc6d9ebb535efa334cd3101e7f7e7459 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5M0MyOjE4QjYwMjo3QzFERDpBRTc5Rjo2QjAwNTE3MSIsInZpc2l0b3JfaWQiOiI3MjcxMjA5MTQwOTAxNDAwOTQ1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f445cbb1fad6bf65af4bceaccbb45f73c770d83687c7c24171c8a71f36bc269b |
| hovercard-subject-tag | issue:687838617 |
| 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/9369/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8717875f88c6725b59747066556c0fc562819225ebe79b1874fa0b73a96048af/python/mypy/issues/9369 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8717875f88c6725b59747066556c0fc562819225ebe79b1874fa0b73a96048af/python/mypy/issues/9369 |
| og:image:alt | 🐛 Bug Report (Possibly related to #6755?) When a variable is declared to be of a type including Optional, mypy correctly infers that it is not None when passing through an assert indicating as such... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | GPHemsley |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| 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 | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width