Title: Surprising error involving bytes duck-type compatibility · Issue #12643 · python/mypy · GitHub
Open Graph Title: Surprising error involving bytes duck-type compatibility · Issue #12643 · python/mypy
X Title: Surprising error involving bytes duck-type compatibility · Issue #12643 · python/mypy
Description: Mypy seems to fail to narrow down union with not conditions. I couldn't find an issue already open on the topic. To Reproduce Running mypy on the following sample, an error is reported: from typing import Union Buffer = Union[bytes, byte...
Open Graph Description: Mypy seems to fail to narrow down union with not conditions. I couldn't find an issue already open on the topic. To Reproduce Running mypy on the following sample, an error is reported: from typing...
X Description: Mypy seems to fail to narrow down union with not conditions. I couldn't find an issue already open on the topic. To Reproduce Running mypy on the following sample, an error is reported: from ty...
Opengraph URL: https://github.com/python/mypy/issues/12643
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Surprising error involving bytes duck-type compatibility","articleBody":"Mypy seems to fail to narrow down union with `not` conditions. I couldn't find an issue already open on the topic.\r\n\r\n**To Reproduce**\r\n\r\nRunning mypy on the following sample, an error is reported:\r\n\r\n```python\r\nfrom typing import Union\r\n\r\nBuffer = Union[bytes, bytearray, memoryview]\r\n\r\ndef f(data: Buffer) -> str:\r\n if not isinstance(data, bytes):\r\n data = bytes(data)\r\n\r\n return data.decode()\r\n```\r\n\r\n```\r\n$ mypy testunion.py \r\ntestunion.py:9: error: Item \"memoryview\" of \"Union[bytes, bytearray, memoryview]\" has no attribute \"decode\" [union-attr]\r\n```\r\n\r\nThis is unexpected because, after the `if`, the type of `data` is definitely `bytes`.\r\n\r\nIf a test `if isinstance(data, (bytearray, memoryview))` is used instead there is no error. However this has likely a lower performance and requires more maintenance should other types be added to the union.\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 0.941\r\n- Python version used: 3.8.10\r\n- In `pyproject.toml`:\r\n```yaml\r\n[tool.mypy]\r\nwarn_unused_ignores = true\r\nshow_error_codes = true\r\nstrict = true\r\n```\r\n","author":{"url":"https://github.com/dvarrazzo","@type":"Person","name":"dvarrazzo"},"datePublished":"2022-04-21T14:40:20Z","url":"https://github.com/python/mypy/issues/12643"}
| 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:737a5240-7b50-ed4e-2408-eeb6c99d3db2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8DE4:BACA4:2E694BF:3D74DE6:6B002944 |
| html-safe-nonce | d8033222fa5ec9cd6ac244101cce809eaa9e7d77c3a01f5c6737367e6b30cf89 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4REU0OkJBQ0E0OjJFNjk0QkY6M0Q3NERFNjo2QjAwMjk0NCIsInZpc2l0b3JfaWQiOiIyNjkwMzQ2MTkxNDk2OTQ4MDM2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 015a96f3cb3333d251118f331962ed735a6f3787e3135ad0b98d78011901617d |
| hovercard-subject-tag | issue:1211142103 |
| 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/12643/issue_layout |
| twitter:image | https://opengraph.githubassets.com/63f20d17d242c2ff115d9b42fc9171e476bdddee20a37907c50962272f204e0f/python/mypy/issues/12643 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/63f20d17d242c2ff115d9b42fc9171e476bdddee20a37907c50962272f204e0f/python/mypy/issues/12643 |
| og:image:alt | Mypy seems to fail to narrow down union with not conditions. I couldn't find an issue already open on the topic. To Reproduce Running mypy on the following sample, an error is reported: from typing... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dvarrazzo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width