Title: False positive `unreachable` when variable is set and reset inside loop · Issue #14987 · python/mypy · GitHub
Open Graph Title: False positive `unreachable` when variable is set and reset inside loop · Issue #14987 · python/mypy
X Title: False positive `unreachable` when variable is set and reset inside loop · Issue #14987 · python/mypy
Description: Bug Report mypy warns about the right side of a condition being unreachable when a variable is reset in an outer loop, then set again inside the inner loop. To Reproduce from typing import Optional test: Optional[float] = None reveal_typ...
Open Graph Description: Bug Report mypy warns about the right side of a condition being unreachable when a variable is reset in an outer loop, then set again inside the inner loop. To Reproduce from typing import Optional...
X Description: Bug Report mypy warns about the right side of a condition being unreachable when a variable is reset in an outer loop, then set again inside the inner loop. To Reproduce from typing import Optional...
Opengraph URL: https://github.com/python/mypy/issues/14987
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"False positive `unreachable` when variable is set and reset inside loop","articleBody":"**Bug Report**\r\n\r\nmypy warns about the right side of a condition being unreachable when a variable is reset in an outer loop, then set again inside the inner loop.\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nfrom typing import Optional\r\n\r\ntest: Optional[float] = None\r\nreveal_type(test) # Revealed type: Union[builtins.float, None]\r\n\r\nfor i in range(10):\r\n test = None\r\n reveal_type(test) # Revealed type: None (???)\r\n for k in range(10):\r\n if test is None or k \u003e test:\r\n test = k\r\n```\r\n\r\n**Expected Behavior**\r\n\r\nNo error.\r\n\r\n**Actual Behavior**\r\n\r\nmypy outputs `error: Right operand of \"or\" is never evaluated`. This is clearly because the type of `test` is re-evaluated at some point, even though it was declared with a type and no statement narrows that type.\r\n\r\nThis appears related to #7204, #8721, #8865, #13973, #14120, except it can't be solved by declaring the type of the variable before the loop in which it's updated (because that violates `no-redef`).\r\n\r\nNote: I'm not sure why anyone would want to actually do this in practice. But I thought it might provide a useful test case.\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 1.1.1\r\n- Mypy command-line flags: --disable-error-code name-defined, plus a few others that I don't believe could possibly have any relevance.\r\n- Mypy configuration options from `mypy.ini` (and other config files): None\r\n- Python version used: 3.10.10","author":{"url":"https://github.com/adebrecht661","@type":"Person","name":"adebrecht661"},"datePublished":"2023-03-31T13:56:30.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/14987/mypy/issues/14987"}
| 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:a96cddc8-cb8f-37b1-c50b-b9b520a23c78 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 920C:3F51C2:1513F45:1C0B695:6A5BA5E8 |
| html-safe-nonce | 697005d78c89976e5208609b3562f61a75100b3f9fc95ab90f7462c2786aa9bc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MjBDOjNGNTFDMjoxNTEzRjQ1OjFDMEI2OTU6NkE1QkE1RTgiLCJ2aXNpdG9yX2lkIjoiODA3MDg1ODQ3MDAyNjA5NDA1NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | dc49075dfcbd60c23b024fa4d36c6a8092a52e5506af9e71ce6fe38ad8a37a48 |
| hovercard-subject-tag | issue:1649402494 |
| 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/14987/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6b643a13018c036ea9da66d56f5946c5dc812e8a89ceb5e43fee588a45c4ad4d/python/mypy/issues/14987 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6b643a13018c036ea9da66d56f5946c5dc812e8a89ceb5e43fee588a45c4ad4d/python/mypy/issues/14987 |
| og:image:alt | Bug Report mypy warns about the right side of a condition being unreachable when a variable is reset in an outer loop, then set again inside the inner loop. To Reproduce from typing import Optional... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | adebrecht661 |
| 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