Title: Constrained TypeVar fails to narrow on left hand side of conditional expression · Issue #4134 · python/mypy · GitHub
Open Graph Title: Constrained TypeVar fails to narrow on left hand side of conditional expression · Issue #4134 · python/mypy
X Title: Constrained TypeVar fails to narrow on left hand side of conditional expression · Issue #4134 · python/mypy
Description: When I run mypy --python-version 3.6 on the following code: from typing import AnyStr def f(x: AnyStr) -> bytes: return x.encode("ascii") if isinstance(x, str) else x I get: test.py:3: error: "bytes" has no attribute "encode"; maybe "dec...
Open Graph Description: When I run mypy --python-version 3.6 on the following code: from typing import AnyStr def f(x: AnyStr) -> bytes: return x.encode("ascii") if isinstance(x, str) else x I get: test.py:3: error: "byte...
X Description: When I run mypy --python-version 3.6 on the following code: from typing import AnyStr def f(x: AnyStr) -> bytes: return x.encode("ascii") if isinstance(x, str) else x I get: test.py:3:...
Opengraph URL: https://github.com/python/mypy/issues/4134
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Constrained TypeVar fails to narrow on left hand side of conditional expression","articleBody":"When I run `mypy --python-version 3.6` on the following code:\r\n\r\n```python\r\nfrom typing import AnyStr\r\ndef f(x: AnyStr) -> bytes:\r\n return x.encode(\"ascii\") if isinstance(x, str) else x\r\n```\r\n\r\nI get:\r\n\r\n```\r\ntest.py:3: error: \"bytes\" has no attribute \"encode\"; maybe \"decode\"?\r\n```\r\n\r\nBut as far as I can tell, this is backwards! `x.encode(\"ascii\")` is only called when `isinstance(x, str)`, not in the `else` case (where x is a `bytes` object).\r\n\r\nThis is with the latest `mypy==0.530`","author":{"url":"https://github.com/alanhdu","@type":"Person","name":"alanhdu"},"datePublished":"2017-10-19T16:35:12Z","url":"https://github.com/python/mypy/issues/4134"}
| 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:0eac7aa8-6085-cae7-4fb5-4e14b10a87bf |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8EA0:1F7A:D90E41:127DB79:6B0007E7 |
| html-safe-nonce | 9bfbcd161d4c40adbef89deb3efbb905da3712a115ff4d4cbcf10e61f4444553 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RUEwOjFGN0E6RDkwRTQxOjEyN0RCNzk6NkIwMDA3RTciLCJ2aXNpdG9yX2lkIjoiNjg0ODEzNjEwMTgyODIzMzE5MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e6654a8e78f86767c77000d3d0b6fb079ea93940bdf439e48a034a59b8df52bc |
| hovercard-subject-tag | issue:266908901 |
| 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/4134/issue_layout |
| twitter:image | https://opengraph.githubassets.com/66732231e730ef635c8bad295727d6f4ae121d9f33a67e8e010fb52ab39421b3/python/mypy/issues/4134 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/66732231e730ef635c8bad295727d6f4ae121d9f33a67e8e010fb52ab39421b3/python/mypy/issues/4134 |
| og:image:alt | When I run mypy --python-version 3.6 on the following code: from typing import AnyStr def f(x: AnyStr) -> bytes: return x.encode("ascii") if isinstance(x, str) else x I get: test.py:3: error: "byte... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | alanhdu |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width