Title: Reachable code in loop marked unreachable · Issue #8721 · python/mypy · GitHub
Open Graph Title: Reachable code in loop marked unreachable · Issue #8721 · python/mypy
X Title: Reachable code in loop marked unreachable · Issue #8721 · python/mypy
Description: Are you reporting a bug, or opening a feature request?: bug What is the actual behavior/output? Given the following (cut down) loop code, mypy identifies incorrectly that the print statement cannot be reached. last = None reveal_type(las...
Open Graph Description: Are you reporting a bug, or opening a feature request?: bug What is the actual behavior/output? Given the following (cut down) loop code, mypy identifies incorrectly that the print statement cannot...
X Description: Are you reporting a bug, or opening a feature request?: bug What is the actual behavior/output? Given the following (cut down) loop code, mypy identifies incorrectly that the print statement cannot...
Opengraph URL: https://github.com/python/mypy/issues/8721
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Reachable code in loop marked unreachable","articleBody":"* Are you reporting a bug, or opening a feature request?: bug\r\n* What is the actual behavior/output?\r\n\r\nGiven the following (cut down) loop code, mypy identifies incorrectly that the print statement cannot be reached.\r\n``` python\r\nlast = None\r\nreveal_type(last) # Revealed type is 'None'\r\n\r\nfor val in range(10):\r\n reveal_type(last) # Revealed type is 'None'\r\n\r\n if last is not None and last \u003c 2:\r\n print(\"apparently unreachable\")\r\n reveal_type(last) # no output\r\n\r\n reveal_type(last) # Revealed type is 'None'\r\n last = val\r\n reveal_type(last) # Revealed type is 'builtins.int*'\r\n```\r\n\r\nMy original code also used the value of `last` within the conditional; that was also warned about being not evaluated.\r\n\r\nNotably, running `mypy --no-warn-unreachable` still doesn't give any output for the `reveal_type` in the `if` branch.\r\n\r\nHappily this issue can be worked around by adding an explicit `Optional[int]` annotation to `last`.\r\n\r\n* What is the behavior/output you expect?\r\n\r\nI think `mypy` should consider `last` to be `Optional[int]` in the early part of the loop, rather than just being `None`. That would fix the unreachable warning as well as potentially being more correct.\r\n\r\nWithin the `if last is not None` branch, `last` should be of type `int`.\r\n\r\n* What are the versions of mypy and Python you are using? mypy 0.770, Python 3.5.2\r\n Do you see the same issue after installing mypy from Git master? yes (tested at 125728a547f80ede4ecedc0ff7f890407d309caf)\r\n* What are the mypy flags you are using? `--warn-unreachable`\r\n","author":{"url":"https://github.com/PeterJCLaw","@type":"Person","name":"PeterJCLaw"},"datePublished":"2020-04-25T14:27:18.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/8721/mypy/issues/8721"}
| 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:f8ff1019-46e1-ff90-7d64-f566af2f67f0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AEEC:2150F3:658BFD:87855B:6A5B76CC |
| html-safe-nonce | 87efa528d4b54d08ce55c0ce0106131f20e4fb34f3262122a363c4820106bd55 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRUVDOjIxNTBGMzo2NThCRkQ6ODc4NTVCOjZBNUI3NkNDIiwidmlzaXRvcl9pZCI6Ijc0MzExNTIxNTUzMzA1NzQwMjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8cec0c42d0c7be9792397596d832f7e2d03eb3023db578795d53a45c8938cc4c |
| hovercard-subject-tag | issue:606780713 |
| 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/8721/issue_layout |
| twitter:image | https://opengraph.githubassets.com/23f8d8b17ca2d82175407136e0bf3816286a02b3f1917a8a324cf50a0cb63ed2/python/mypy/issues/8721 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/23f8d8b17ca2d82175407136e0bf3816286a02b3f1917a8a324cf50a0cb63ed2/python/mypy/issues/8721 |
| og:image:alt | Are you reporting a bug, or opening a feature request?: bug What is the actual behavior/output? Given the following (cut down) loop code, mypy identifies incorrectly that the print statement cannot... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | PeterJCLaw |
| 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