Title: Type fails to get "refined" on branch when `is None`-check happens in variable assignment · Issue #9229 · python/mypy · GitHub
Open Graph Title: Type fails to get "refined" on branch when `is None`-check happens in variable assignment · Issue #9229 · python/mypy
X Title: Type fails to get "refined" on branch when `is None`-check happens in variable assignment · Issue #9229 · python/mypy
Description: Are you reporting a bug, or opening a feature request? Bug Please insert below the code you are checking with mypy: from typing import Optional def bar(var: str): pass def foo(var: Optional[str]=None): var_not_None = not (var is None) if...
Open Graph Description: Are you reporting a bug, or opening a feature request? Bug Please insert below the code you are checking with mypy: from typing import Optional def bar(var: str): pass def foo(var: Optional[str]=No...
X Description: Are you reporting a bug, or opening a feature request? Bug Please insert below the code you are checking with mypy: from typing import Optional def bar(var: str): pass def foo(var: Optional[str]=No...
Opengraph URL: https://github.com/python/mypy/issues/9229
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type fails to get \"refined\" on branch when `is None`-check happens in variable assignment","articleBody":"* Are you reporting a bug, or opening a feature request? **Bug**\r\n* Please insert below the code you are checking with mypy:\r\n\r\n```python\r\nfrom typing import Optional\r\n\r\ndef bar(var: str):\r\n pass\r\n\r\ndef foo(var: Optional[str]=None):\r\n var_not_None = not (var is None)\r\n if var_not_None:\r\n # var can only be a `str` at this point,\r\n # but `mypy` complains that\r\n # Argument 1 to \"bar\" has incompatible type \"Optional[str]\"; expected \"str\"\r\n bar(var)\r\n\r\ndef foo2(var: Optional[str]=None):\r\n if not(var is None):\r\n # no complaints from mypy\r\n bar(var)\r\n```\r\n\r\n* What is the actual behavior/output?\r\n\r\nRunning `mypy` on this reproducer returns:\r\n\r\n`error: Argument 1 to \"bar\" has incompatible type \"Optional[str]\"; expected \"str\"\r\nFound 1 error in 1 file (checked 1 source file)`\r\n\r\n* What is the behavior/output you expect?\r\n\r\nNo errors, both `foo2` and `foo` are equivalent.\r\n\r\n* What are the versions of mypy and Python you are using?\r\n\r\n`mypy 0.782` and `Python 3.6.8`\r\n\r\n* Do you see the same issue after installing mypy from Git master?\r\n\r\nYup, unless I did something wrong 😅 \r\n\r\n**Summary:**\r\n\r\nWhen you have an `Optional[str]`-typed variable `var`, one would expect that an `not (var is None)` check in an `if-statement` should tell `mypy` that all code within that branch has that the variable has type `str`. Indeed, `mypy` shows the expected behaviour for this, as shown in function `foo2` above.\r\n\r\nHowever, there are _many_ instances where you want to check if a variable is `None` outside of the if-statement. For instance, if your if-statement were to check many boolean conditions at once. In this situation, one may save the result of the check into a variable. This is the case in the `foo` function above, where `mypy` complains.\r\n\r\nI suspect this problem may not be solvable in the general case, but perhaps we can improve the status-quo nonetheless to catch this error.\r\n\r\nIf this is a beginner-friendly bug, I would love to help fix it! I've been meaning to get involved with the `mypy` project; just some guidance would do. Thanks!\r\n","author":{"url":"https://github.com/davidmrdavid","@type":"Person","name":"davidmrdavid"},"datePublished":"2020-07-29T02:55:58Z","url":"https://github.com/python/mypy/issues/9229"}
| 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:f17b3420-149f-a26d-66d3-aeddf111094d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9028:251AC0:7474E2:9A5DCB:6B00382B |
| html-safe-nonce | 59297ed302fc6d0a095f4d2fb08e9c59d9c4070c76177fac4025af1722482637 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDI4OjI1MUFDMDo3NDc0RTI6OUE1RENCOjZCMDAzODJCIiwidmlzaXRvcl9pZCI6Ijg1NzUyNzExNjIxMDY0OTEzMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6b70652911c8478f619e26e2510cd2fe82d4b897a07c0404c3c4362ad76b8f76 |
| hovercard-subject-tag | issue:667508754 |
| 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/9229/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f1777b9d6c4f97b82789a24b2beef4c3ab6f4495449545bf87c3ff27d1ee95e3/python/mypy/issues/9229 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f1777b9d6c4f97b82789a24b2beef4c3ab6f4495449545bf87c3ff27d1ee95e3/python/mypy/issues/9229 |
| og:image:alt | Are you reporting a bug, or opening a feature request? Bug Please insert below the code you are checking with mypy: from typing import Optional def bar(var: str): pass def foo(var: Optional[str]=No... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | davidmrdavid |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width