Title: False positive unreachable with redefined variable in loop · Issue #8865 · python/mypy · GitHub
Open Graph Title: False positive unreachable with redefined variable in loop · Issue #8865 · python/mypy
X Title: False positive unreachable with redefined variable in loop · Issue #8865 · python/mypy
Description: When checking the following code: from typing import AbstractSet, Iterable, Optional, Set, TypeVar T = TypeVar('T') def intersection(seq: Iterable[AbstractSet[T]]) -> Optional[Set[T]]: ret = None for elem in seq: if ret is None: ret = se...
Open Graph Description: When checking the following code: from typing import AbstractSet, Iterable, Optional, Set, TypeVar T = TypeVar('T') def intersection(seq: Iterable[AbstractSet[T]]) -> Optional[Set[T]]: ret = None f...
X Description: When checking the following code: from typing import AbstractSet, Iterable, Optional, Set, TypeVar T = TypeVar('T') def intersection(seq: Iterable[AbstractSet[T]]) -> Optional[Set[T]]: r...
Opengraph URL: https://github.com/python/mypy/issues/8865
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"False positive unreachable with redefined variable in loop","articleBody":"When checking the following code:\r\n\r\n```py\r\nfrom typing import AbstractSet, Iterable, Optional, Set, TypeVar\r\n\r\nT = TypeVar('T')\r\n\r\ndef intersection(seq: Iterable[AbstractSet[T]]) -\u003e Optional[Set[T]]:\r\n ret = None\r\n for elem in seq:\r\n if ret is None:\r\n ret = set(elem)\r\n else:\r\n ret \u0026= elem\r\n return ret\r\n```\r\n\r\nmypy reports:\r\n\r\n```\r\n$ mypy --warn-unreachable intersection.py\r\nintersection.py:11: error: Statement is unreachable\r\n```\r\n\r\nThis statement is the `ret \u0026= elem` inside the `else` block.\r\n\r\nIt seems that mypy is using the initial type of `ret` to conclude that `ret is None` is always true, ignoring the fact that inside the loop `ret` is redefined to a different type.\r\n\r\nIf I place a `reveal_type` just before the `return`, mypy does report ``Union[builtins.set[T`-1], None]`` as the type, so it figures out the redefinition eventually.\r\n\r\nI'm using mypy 0.770 on Pyton 3.8.2, with the `--warn-unreachable` flag.\r\n","author":{"url":"https://github.com/mthuurne","@type":"Person","name":"mthuurne"},"datePublished":"2020-05-20T21:51:15.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/8865/mypy/issues/8865"}
| 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:ca8c7faa-7c0f-0c95-87c9-434ed4f978d2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ABA2:26C182:1C01672:2496B0B:6A5C732C |
| html-safe-nonce | 0049a5cc6eb806e99ab70d33e1751119dbffedeeb1333ab43f513a1f541f8266 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQkEyOjI2QzE4MjoxQzAxNjcyOjI0OTZCMEI6NkE1QzczMkMiLCJ2aXNpdG9yX2lkIjoiODY4MDEwMTU5NTMwMzQwODQyOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 83d9db57768f5b01767af97c6e6880ad65efcb813d8c5a29a0091c610f75bd04 |
| hovercard-subject-tag | issue:622106624 |
| 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/8865/issue_layout |
| twitter:image | https://opengraph.githubassets.com/23f8d8b17ca2d82175407136e0bf3816286a02b3f1917a8a324cf50a0cb63ed2/python/mypy/issues/8865 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/23f8d8b17ca2d82175407136e0bf3816286a02b3f1917a8a324cf50a0cb63ed2/python/mypy/issues/8865 |
| og:image:alt | When checking the following code: from typing import AbstractSet, Iterable, Optional, Set, TypeVar T = TypeVar('T') def intersection(seq: Iterable[AbstractSet[T]]) -> Optional[Set[T]]: ret = None f... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mthuurne |
| 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