Title: Use of sys.version_info in ternary operator with straddling code is not handled correctly · Issue #5550 · python/mypy · GitHub
Open Graph Title: Use of sys.version_info in ternary operator with straddling code is not handled correctly · Issue #5550 · python/mypy
X Title: Use of sys.version_info in ternary operator with straddling code is not handled correctly · Issue #5550 · python/mypy
Description: When if statements are used, mypy is able to recognize the use of the sys.version_info and handle the types correctly for straddling code that needs to work with both Python 3 and 2. Unfortunately, mypy is not able to handle the same cas...
Open Graph Description: When if statements are used, mypy is able to recognize the use of the sys.version_info and handle the types correctly for straddling code that needs to work with both Python 3 and 2. Unfortunately,...
X Description: When if statements are used, mypy is able to recognize the use of the sys.version_info and handle the types correctly for straddling code that needs to work with both Python 3 and 2. Unfortunately,...
Opengraph URL: https://github.com/python/mypy/issues/5550
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use of sys.version_info in ternary operator with straddling code is not handled correctly","articleBody":"When `if` statements are used, *mypy* is able to recognize the use of the `sys.version_info` and handle the types correctly for straddling code that needs to work with both Python 3 and 2. Unfortunately, *mypy* is not able to handle the same case with a ternary operator.\r\n\r\nCan this bug be fixed?\r\n\r\nHere's the code that demonstrates the issue:\r\n```\r\ndef ensure_native_str(str_or_bytes): # type: (Union[Text, bytes]) -> str\r\n return (\r\n str_or_bytes if isinstance(str_or_bytes, str) else\r\n str_or_bytes.decode('utf-8') if six.PY3 else\r\n str_or_bytes.encode('utf-8')\r\n )\r\n```\r\nWhen processed by *mypy* it returns:\r\n```\r\n# using mypy --python-version 3.7 /path/to/file\r\n...: error: \"bytes\" has no attribute \"encode\"; maybe \"decode\"?\r\n# using mypy --python-version 2.7 /path/to/file\r\n...: error: Incompatible return value type (got \"unicode\", expected \"str\")\r\n```\r\n\r\nWhen the same function is changed into this:\r\n```\r\ndef ensure_native_str(str_or_bytes): # type: (Union[Text, bytes]) -> str\r\n if isinstance(str_or_bytes, str):\r\n return str_or_bytes\r\n if six.PY3:\r\n return str_or_bytes.decode('utf-8')\r\n return str_or_bytes.encode('utf-8')\r\n```\r\nit passes mypy checks with both `--python-version 3.7` and `--python-version 2.7`.\r\nThis means that the branches in straddling code were correctly handled and the types inferred appropriately. With the ternary operator above, unfortunately, that did not happen.\r\n\r\nThis was tested with both mypy-0.620 and mypy-0.560.\r\nPython version that mypy was run with was 3.6.\r\nWhen `six.PY3` is replaced with the explicit `sys.version_info[0] == 3` the behavior is the same.\r\nThe `if` statement version passes the checks, the ternary operator version doesn't.","author":{"url":"https://github.com/zvezdan","@type":"Person","name":"zvezdan"},"datePublished":"2018-09-01T01:24:59Z","url":"https://github.com/python/mypy/issues/5550"}
| 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:c0a3bd90-bba1-ed8f-704d-bb436ebc8f7f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8FC4:C5FC2:1A7CA95:22FB587:6B002F16 |
| html-safe-nonce | 1a1d8eadf45b066b5fa1e9520b1377c2ec0533c968f58f8c1a514eb083aad3fe |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RkM0OkM1RkMyOjFBN0NBOTU6MjJGQjU4Nzo2QjAwMkYxNiIsInZpc2l0b3JfaWQiOiIyNDk0ODg3MzcwOTMxODQyODM4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9d7e466fdba0a5b757efb71603cde8b67cf81efd66d73389703e7d3bbd83fd6a |
| hovercard-subject-tag | issue:356160105 |
| 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/5550/issue_layout |
| twitter:image | https://opengraph.githubassets.com/35715ac0f2ada23f3adaafcd09dee32f7969a1a78a73ae79bee987b33ed8cf9a/python/mypy/issues/5550 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/35715ac0f2ada23f3adaafcd09dee32f7969a1a78a73ae79bee987b33ed8cf9a/python/mypy/issues/5550 |
| og:image:alt | When if statements are used, mypy is able to recognize the use of the sys.version_info and handle the types correctly for straddling code that needs to work with both Python 3 and 2. Unfortunately,... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | zvezdan |
| 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