Title: Type error in mypy? · Issue #2222 · python/mypy · GitHub
Open Graph Title: Type error in mypy? · Issue #2222 · python/mypy
X Title: Type error in mypy? · Issue #2222 · python/mypy
Description: During #2221 (which I have little hope to be actually accepted) I ran into this function in binder.py: def get(self, expr: Union[Expression, Var]) -> Type: return self._get(expr.literal_hash) The Var case comes from analyze_var_ref() in ...
Open Graph Description: During #2221 (which I have little hope to be actually accepted) I ran into this function in binder.py: def get(self, expr: Union[Expression, Var]) -> Type: return self._get(expr.literal_hash) The V...
X Description: During #2221 (which I have little hope to be actually accepted) I ran into this function in binder.py: def get(self, expr: Union[Expression, Var]) -> Type: return self._get(expr.literal_hash) Th...
Opengraph URL: https://github.com/python/mypy/issues/2222
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type error in mypy? ","articleBody":"During #2221 (which I have little hope to be actually accepted) I ran into [this](../../mypy/blob/master/mypy/binder.py#L108) function in binder.py:\n\n``` Python\n def get(self, expr: Union[Expression, Var]) -> Type:\n return self._get(expr.literal_hash)\n```\n\nThe Var case comes from [`analyze_var_ref()`](../../mypy/blob/master/mypy/checkexpr.py#L160) in checkexpr.py.\n\nWell, I couldn't find where Var.literal_hash is initialized to anything other than None, and returning None in the Var case passes all the tests. So I tried to replace it with something like the following:\n\n``` Python\n def get(self, expr: Union[Expression, Var]) -> Type:\n if isinstance(expr, Var):\n new_expr = NameExpr(expr.name())\n else:\n new_expr = expr\n return self._get(new_expr.literal_hash)\n```\n\nI then ran mypy on itself, but it raised many errors. So I ran the test suite, and I got many failures; however, it seems that these failures are due to a _more precise_ typechecking. For example, take testModifyLoopFor4 (simplified):\n\n``` Python\nx = None # type: Union[int, str]\nx = 1\n\nif 1:\n x = 'a'\n```\n\nThe test does not say anything bad in this part, but mypy now complains that x is an int and therefore 'a' cannot be assigned into it. It might be too restrictive, but it is my understanding that this is the desired behavior, since \"x=1\" rebinds the variable into int (it's valid since int is part of the Union) and then it tries to rebind it to str, but now it is not possible.\n\nI am not 100% sure, since I am not 100% sure how the code works and what is the expected behavior, but it looks like a bug - and if so, there are ~40 \"type errors\" to fix in the source code (although I suspect none of them is an actual error).\n\nSo what is the expected behavior? When and how should types rebind?\n","author":{"url":"https://github.com/elazarg","@type":"Person","name":"elazarg"},"datePublished":"2016-10-06T05:32:42Z","url":"https://github.com/python/mypy/issues/2222"}
| 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:14d7630a-fb55-8b6c-fd1e-728fa139e168 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F5E:3F9D33:2A4D2FC:3999FF3:6B005055 |
| html-safe-nonce | f88a27c73048f3daf290a8e7ec6a4dba0b270b01b8a7f2d32b87301dd80018d4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjVFOjNGOUQzMzoyQTREMkZDOjM5OTlGRjM6NkIwMDUwNTUiLCJ2aXNpdG9yX2lkIjoiMjYzNTYwMDk0MDYxNDMwNzkyNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bc09820c9a4ea940f6f9ba81e708f8fa84dcd542d335ef5b4b4366c7021d66ee |
| hovercard-subject-tag | issue:181328094 |
| 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/2222/issue_layout |
| twitter:image | https://opengraph.githubassets.com/da634b5c0b0fb118c7766ea0170bb839112522653c906b109f501245feeb72a1/python/mypy/issues/2222 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/da634b5c0b0fb118c7766ea0170bb839112522653c906b109f501245feeb72a1/python/mypy/issues/2222 |
| og:image:alt | During #2221 (which I have little hope to be actually accepted) I ran into this function in binder.py: def get(self, expr: Union[Expression, Var]) -> Type: return self._get(expr.literal_hash) The V... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | elazarg |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| 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 | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width