Title: Assert at end of if statement does not help mypy value inference · Issue #3431 · python/mypy · GitHub
Open Graph Title: Assert at end of if statement does not help mypy value inference · Issue #3431 · python/mypy
X Title: Assert at end of if statement does not help mypy value inference · Issue #3431 · python/mypy
Description: While working on making zulip pass with mypy --strict-optional, I ran into the following piece of code. Mypy was essentially saying that message.get_realm() was not guaranteed to work ("Not all parts of the union have get_realm() method"...
Open Graph Description: While working on making zulip pass with mypy --strict-optional, I ran into the following piece of code. Mypy was essentially saying that message.get_realm() was not guaranteed to work ("Not all par...
X Description: While working on making zulip pass with mypy --strict-optional, I ran into the following piece of code. Mypy was essentially saying that message.get_realm() was not guaranteed to work ("Not al...
Opengraph URL: https://github.com/python/mypy/issues/3431
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Assert at end of if statement does not help mypy value inference","articleBody":"While working on making zulip pass with `mypy --strict-optional`, I ran into the following piece of code. Mypy was essentially saying that message.get_realm() was not guaranteed to work (\"Not all parts of the union have get_realm() method\", or somesuch.)\r\n\r\n```\r\nif message is None:\r\n message = Message.objects.select_related().get(id=message_id)\r\n\r\nrendered_content = render_markdown(message, content, realm=message.get_realm())\r\n```\r\nLet's assume, at least for here, that the get() call will never return None. I tried the following change, but mypy was still giving the exact same error:\r\n\r\n```\r\nif message is None:\r\n message = Message.objects.select_related().get(id=message_id)\r\n assert message is not None # ADDED\r\n\r\nrendered_content = render_markdown(message, content, realm=message.get_realm())\r\n```\r\n\r\nHowever, with this change, mypy was now happy with the code snippet:\r\n\r\n```\r\nif message is None:\r\n message = Message.objects.select_related().get(id=message_id)\r\n\r\nassert message is not None # ADDED, but outside the if, now.\r\nrendered_content = render_markdown(message, content, realm=message.get_realm())\r\n```\r\n\r\nThe two asserts clearly have the same code behavior of making it impossible for `message` to be `None` when we reach the `rendered_content = ...` line, but mypy only understands that currently (version 5.111) for the last code snippet only. Opening this bug report for discussion of that.\r\n\r\n(Feel free to change the title of this bug report to a more meaningful / better one.)","author":{"url":"https://github.com/chrish42","@type":"Person","name":"chrish42"},"datePublished":"2017-05-24T00:05:37Z","url":"https://github.com/python/mypy/issues/3431"}
| 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:1c404abc-24dd-8f42-0290-31344e077351 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CE16:1F75:7F49:5F219:6B003875 |
| html-safe-nonce | 6af9e43b141d7501e65d12842a1a5dd2612759a313af96c35a0f3058284bea9f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRTE2OjFGNzU6N0Y0OTo1RjIxOTo2QjAwMzg3NSIsInZpc2l0b3JfaWQiOiIyOTg3MTk3MzM1ODEwODgxNjUzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3291d83921ef47a0b5957a9b7fdde01ee5bf45fe5a50718f571dd104c9ade4ec |
| hovercard-subject-tag | issue:230883462 |
| 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/3431/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3d322ae545e91904ab150e54b4c002cd0f7a3ea481883c06c67bb2f9823b4f4d/python/mypy/issues/3431 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3d322ae545e91904ab150e54b4c002cd0f7a3ea481883c06c67bb2f9823b4f4d/python/mypy/issues/3431 |
| og:image:alt | While working on making zulip pass with mypy --strict-optional, I ran into the following piece of code. Mypy was essentially saying that message.get_realm() was not guaranteed to work ("Not all par... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | chrish42 |
| 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