Title: Type inference for narrowing of unions · Issue #1436 · python/mypy · GitHub
Open Graph Title: Type inference for narrowing of unions · Issue #1436 · python/mypy
X Title: Type inference for narrowing of unions · Issue #1436 · python/mypy
Description: From #1141 (comment) This pattern is the most common cause of cast() calls in my code, and it would also be awesome if mypy could infer this narrowing on its own so I wouldn't need the explicit cast (although it may be too much to ask). ...
Open Graph Description: From #1141 (comment) This pattern is the most common cause of cast() calls in my code, and it would also be awesome if mypy could infer this narrowing on its own so I wouldn't need the explicit cas...
X Description: From #1141 (comment) This pattern is the most common cause of cast() calls in my code, and it would also be awesome if mypy could infer this narrowing on its own so I wouldn't need the explicit...
Opengraph URL: https://github.com/python/mypy/issues/1436
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type inference for narrowing of unions","articleBody":"From https://github.com/python/mypy/issues/1141#issuecomment-213981705\n\nThis pattern is the most common cause of `cast()` calls in my code, and it would also be awesome if mypy could infer this narrowing on its own so I wouldn't need the explicit cast (although it may be too much to ask). \n\n```\ndef f(x: Union[str, bytes]):\n if isinstance(x, bytes):\n x = x.decode('utf8')\n x = cast(str, x)\n # From here on x is a str instead of a union.\n```\n\nThe logic would be that in the (implied) `else` branch of the `if`, we know that `x` is not `bytes`, so it must be of type `Union[str, bytes] - bytes = Union[str] = str`. After executing the assignment in the `if` branch, we also know that it is of type `str` there. The type checker could construct a new union from the types that are possible in each branch of the `if`, which in this case would simplify to `str`.\n\nOn the other hand, this is currently legal and would become illegal if the union were automatically narrowed in this way:\n\n```\ndef f(x: Union[str, bytes]):\n if isinstance(x, bytes):\n x = decode('utf8')\n if something():\n x = b\"some bytes\"\n```\n","author":{"url":"https://github.com/bdarnell","@type":"Person","name":"bdarnell"},"datePublished":"2016-04-24T16:06:24Z","url":"https://github.com/python/mypy/issues/1436"}
| 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:635fd373-6c63-dcc8-2db4-8c3b12900847 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DD98:1A6297:11070B1:166D725:6B000C9F |
| html-safe-nonce | 1ab2d2e3a1f6341da4be93a42c5ea965ff74787242dd84db0424fac242bb81a8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERDk4OjFBNjI5NzoxMTA3MEIxOjE2NkQ3MjU6NkIwMDBDOUYiLCJ2aXNpdG9yX2lkIjoiNTM0NDM1NzUyMDUxNTI3MTgzOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 26f06981cef005dcc92bab72fa0844017d3661df3b54f7a91352ab43652fe8e6 |
| hovercard-subject-tag | issue:150676102 |
| 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/1436/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7627858e87bf3d383c698636ed8c455322a31e662b69f35ef13daf6987a4e11e/python/mypy/issues/1436 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7627858e87bf3d383c698636ed8c455322a31e662b69f35ef13daf6987a4e11e/python/mypy/issues/1436 |
| og:image:alt | From #1141 (comment) This pattern is the most common cause of cast() calls in my code, and it would also be awesome if mypy could infer this narrowing on its own so I wouldn't need the explicit cas... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bdarnell |
| 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