Title: Type refinements are not applied to index expressions · Issue #7339 · python/mypy · GitHub
Open Graph Title: Type refinements are not applied to index expressions · Issue #7339 · python/mypy
X Title: Type refinements are not applied to index expressions · Issue #7339 · python/mypy
Description: Ran into this issue today, I believe mypy is incorrectly flagging this as an error: from typing import Any, Dict, Optional def rename_keys( original_dict: Dict[str, Any], key_mapping: Dict[str, Optional[str]] ) -> Dict[str, Any]: """Rena...
Open Graph Description: Ran into this issue today, I believe mypy is incorrectly flagging this as an error: from typing import Any, Dict, Optional def rename_keys( original_dict: Dict[str, Any], key_mapping: Dict[str, Opt...
X Description: Ran into this issue today, I believe mypy is incorrectly flagging this as an error: from typing import Any, Dict, Optional def rename_keys( original_dict: Dict[str, Any], key_mapping: Dict[str, Opt...
Opengraph URL: https://github.com/python/mypy/issues/7339
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type refinements are not applied to index expressions","articleBody":"Ran into this issue today, I believe mypy is incorrectly flagging this as an error:\r\n```\r\nfrom typing import Any, Dict, Optional\r\n\r\n\r\ndef rename_keys(\r\n original_dict: Dict[str, Any], key_mapping: Dict[str, Optional[str]]\r\n) -> Dict[str, Any]:\r\n \"\"\"Renames keys in the dictionary using the key_mapping. If the value in key_mapping is None then it will remove the key entirely\"\"\"\r\n return {\r\n key_mapping.get(k, k): v\r\n for k, v in original_dict.items()\r\n if k not in key_mapping or key_mapping[k] is not None\r\n }\r\n```\r\n\r\noutput from mypy:\r\n```\r\n/tmp/mypytest.py:8: error: Key expression in dictionary comprehension has incompatible type \"Optional[str]\"; expected type \"str\"\r\n```\r\n\r\nif `key_mapping.get(k)` is `None` then it would fail the if-statement at the end of the dictionary comprehension and therefore get filtered out\r\n\r\nif `k` is not in `key_mapping` then `k` is the result of the expression `key_mapping.get(k,k)`. `k` can't be `None` because `k` comes from the keys of `original_dict` which has the type `Dict[str, Any]`\r\n\r\nVersions: mypy 0.701, Python 3.7.4\r\n\r\nno flags","author":{"url":"https://github.com/tomalexander","@type":"Person","name":"tomalexander"},"datePublished":"2019-08-14T14:37:50Z","url":"https://github.com/python/mypy/issues/7339"}
| 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:e824fd18-ca39-91ea-a033-b3e1be19afc7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D036:1AC37C:A2AF54:D91717:6B004717 |
| html-safe-nonce | 1b605810c6ec0ff407524e3594de20a5d8f91b56dbdcc556bea540b05f309103 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMDM2OjFBQzM3QzpBMkFGNTQ6RDkxNzE3OjZCMDA0NzE3IiwidmlzaXRvcl9pZCI6IjgwMDQ0MzAyMTY2MTkzNzg0NTUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3d675ae5e5acbfc96a906b60de47615866f265af34e10fc2ba981e2916cbc5d4 |
| hovercard-subject-tag | issue:480717468 |
| 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/7339/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1c32b3a1419ea7bbfe85a75f7d62009345b23925189961fa7f40374de0d9ec57/python/mypy/issues/7339 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1c32b3a1419ea7bbfe85a75f7d62009345b23925189961fa7f40374de0d9ec57/python/mypy/issues/7339 |
| og:image:alt | Ran into this issue today, I believe mypy is incorrectly flagging this as an error: from typing import Any, Dict, Optional def rename_keys( original_dict: Dict[str, Any], key_mapping: Dict[str, Opt... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tomalexander |
| 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