Title: --warn-unreachable false positive with minimal example without types explicitly set · Issue #7204 · python/mypy · GitHub
Open Graph Title: --warn-unreachable false positive with minimal example without types explicitly set · Issue #7204 · python/mypy
X Title: --warn-unreachable false positive with minimal example without types explicitly set · Issue #7204 · python/mypy
Description: Hello, I just downloaded the new mypy v0.720 and am playing with the --warn-unreachable-flag in my projects. I noticed one occasion where there is something that seems like a false positive, so I am making an issue for it with a minimal ...
Open Graph Description: Hello, I just downloaded the new mypy v0.720 and am playing with the --warn-unreachable-flag in my projects. I noticed one occasion where there is something that seems like a false positive, so I a...
X Description: Hello, I just downloaded the new mypy v0.720 and am playing with the --warn-unreachable-flag in my projects. I noticed one occasion where there is something that seems like a false positive, so I a...
Opengraph URL: https://github.com/python/mypy/issues/7204
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"--warn-unreachable false positive with minimal example without types explicitly set","articleBody":"Hello,\r\n\r\nI just downloaded the new mypy v0.720 and am playing with the `--warn-unreachable-flag` in my projects.\r\n\r\nI noticed one occasion where there is something that seems like a false positive, so I am making an issue for it with a minimal reproducible example.\r\n\r\n\r\n#### Version\r\n```\r\nmypy==0.720\r\nmypy-extensions==0.4.1\r\n```\r\n\r\n#### source code example\r\n\r\n```python\r\nimport sys\r\nfrom typing import Optional\r\n\r\nfound_id = None\r\n\r\nsymbols_list = [{'symbol': 'A', 'id': 1}, {'symbol': 'B', 'id': 2}, {'symbol': 'B', 'id': 3}]\r\n\r\nfor entry in symbols_list:\r\n if entry['symbol'] == 'B':\r\n if found_id:\r\n print('error, symbol found multiple times!')\r\n sys.exit(1)\r\n\r\n found_id = entry['id']\r\n```\r\n\r\n\r\nThis when tested with `mypy test.py --warn-unreachable` returns:\r\n\u003e test.py:10: error: Statement is unreachable\r\n\r\nSo essentially telling us that `if found_id` is never entered due to the `found_id` type being `None`. But in reality the type should be `Optional[int]` since you can see that it's set to `entry['id']` later.\r\n\r\nIf you explicitly set the types mypy no longer complains:\r\n```python\r\nimport sys\r\nfrom typing import Optional\r\n\r\nfound_id: Optional[int] = None\r\n\r\nsymbols_list = [{'symbol': 'A', 'id': 1}, {'symbol': 'B', 'id': 2}, {'symbol': 'B', 'id': 3}]\r\n\r\nfor entry in symbols_list:\r\n if entry['symbol'] == 'B':\r\n if found_id:\r\n print('error, symbol found multiple times!')\r\n sys.exit(1)\r\n\r\n assert isinstance(entry['id'], int)\r\n found_id = entry['id']\r\n```\r\n\r\n#### Bug or not\r\n\r\nEssentially I am not sure if this is intended behaviour or not. After writing this issue down I realize it all boils down to the matter of mypy not correctly inferring type of `found_id`.","author":{"url":"https://github.com/LefterisJP","@type":"Person","name":"LefterisJP"},"datePublished":"2019-07-13T14:25:43.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/7204/mypy/issues/7204"}
| 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:fd7e9a62-d5d7-6f9c-d405-885868d4fb97 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C4C8:3EA7B0:531A5D:6F3B11:6A5B6F9B |
| html-safe-nonce | aedba0497df11d216d5a28b606aaa4c7da56861208b6521cca9a28039a579b79 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNEM4OjNFQTdCMDo1MzFBNUQ6NkYzQjExOjZBNUI2RjlCIiwidmlzaXRvcl9pZCI6IjkwODIyMjQwMTkwMDkzMzAxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 42c965dd78062cb7cfe9138a1c06a784f1ab72861d0d4444482cbac959064140 |
| hovercard-subject-tag | issue:467726338 |
| 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/7204/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0da147ab61de4d4ec7bb8f5e604909e7e9054a2c1aa58e03b691f0bf97230365/python/mypy/issues/7204 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0da147ab61de4d4ec7bb8f5e604909e7e9054a2c1aa58e03b691f0bf97230365/python/mypy/issues/7204 |
| og:image:alt | Hello, I just downloaded the new mypy v0.720 and am playing with the --warn-unreachable-flag in my projects. I noticed one occasion where there is something that seems like a false positive, so I a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | LefterisJP |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| 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 | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width