Title: Infer more specific type after equality or membership check · Issue #6027 · python/mypy · GitHub
Open Graph Title: Infer more specific type after equality or membership check · Issue #6027 · python/mypy
X Title: Infer more specific type after equality or membership check · Issue #6027 · python/mypy
Description: from typing import Union def foo(bar: Union[str, int]): if bar in ["a", "b", "c"]: # to a human, bar is clearly a str. mypy still thinks it's a Union[str, int] return bar.upper() else: return bar $ mypy test.py test.py:5: error: Item "in...
Open Graph Description: from typing import Union def foo(bar: Union[str, int]): if bar in ["a", "b", "c"]: # to a human, bar is clearly a str. mypy still thinks it's a Union[str, int] return bar.upper() else: return bar $...
X Description: from typing import Union def foo(bar: Union[str, int]): if bar in ["a", "b", "c"]: # to a human, bar is clearly a str. mypy still thinks it's a Union[str, int] ret...
Opengraph URL: https://github.com/python/mypy/issues/6027
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Infer more specific type after equality or membership check","articleBody":"```python\r\nfrom typing import Union\r\n\r\ndef foo(bar: Union[str, int]):\r\n\tif bar in [\"a\", \"b\", \"c\"]:\r\n # to a human, bar is clearly a str. mypy still thinks it's a Union[str, int]\r\n\t\treturn bar.upper()\r\n\telse:\r\n\t\treturn bar\r\n```\r\n```\r\n$ mypy test.py \r\ntest.py:5: error: Item \"int\" of \"Union[str, int]\" has no attribute \"upper\"\r\n```\r\n\r\nThe same thing happens if the condition is, for example, `bar == \"a\"`.\r\n* Are you reporting a bug, or opening a feature request?\r\n\r\nFeature request\r\n\r\n* What is the behavior/output you expect?\r\n\r\nMypy to understand the effect of conditions like `bar == \"a\"` or `bar in [\"a\"]`, like it already does with `isinstance` checks\r\n","author":{"url":"https://github.com/jackriccomini","@type":"Person","name":"jackriccomini"},"datePublished":"2018-12-07T15:56:09Z","url":"https://github.com/python/mypy/issues/6027"}
| 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:007fd351-3cd9-f115-1c01-d64b336e2fe7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8BC6:3E7030:9CCD04:D222BB:6B00472C |
| html-safe-nonce | 037e425c7657b709f97e70f9776af05712248ec4affcf74d445c227d13af8e9e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QkM2OjNFNzAzMDo5Q0NEMDQ6RDIyMkJCOjZCMDA0NzJDIiwidmlzaXRvcl9pZCI6IjkwMjkwNzI0ODU1OTI2MTQ3MDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2957264cd455e3c5f58016298e343b2cacf8355dde82417868a5e8d69c332c05 |
| hovercard-subject-tag | issue:388711480 |
| 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/6027/issue_layout |
| twitter:image | https://opengraph.githubassets.com/35832d17969b2c9ca3a03c4741e956c367e8729d13ea3e3107a6452a5a33b1ac/python/mypy/issues/6027 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/35832d17969b2c9ca3a03c4741e956c367e8729d13ea3e3107a6452a5a33b1ac/python/mypy/issues/6027 |
| og:image:alt | from typing import Union def foo(bar: Union[str, int]): if bar in ["a", "b", "c"]: # to a human, bar is clearly a str. mypy still thinks it's a Union[str, int] return bar.upper() else: return bar $... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jackriccomini |
| 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