Title: Incomplete or inconsistent Literal and Union behavior on conditional checks · Issue #9220 · python/mypy · GitHub
Open Graph Title: Incomplete or inconsistent Literal and Union behavior on conditional checks · Issue #9220 · python/mypy
X Title: Incomplete or inconsistent Literal and Union behavior on conditional checks · Issue #9220 · python/mypy
Description: Used mypy version: 0.782 I stumbled upon some incomplete behavior related to Literal and Union. In most cases Union is not properly expanded upon conditional check when dealing with literals. Below example should describe clearly the sit...
Open Graph Description: Used mypy version: 0.782 I stumbled upon some incomplete behavior related to Literal and Union. In most cases Union is not properly expanded upon conditional check when dealing with literals. Below...
X Description: Used mypy version: 0.782 I stumbled upon some incomplete behavior related to Literal and Union. In most cases Union is not properly expanded upon conditional check when dealing with literals. Below...
Opengraph URL: https://github.com/python/mypy/issues/9220
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Incomplete or inconsistent Literal and Union behavior on conditional checks","articleBody":"Used mypy version: `0.782`\r\n\r\nI stumbled upon some incomplete behavior related to `Literal` and `Union`. In most cases `Union` is not properly expanded upon conditional check when dealing with literals. Below example should describe clearly the situation.\r\n\r\n```python\r\nfrom typing import Union\r\nfrom typing_extensions import Literal\r\n\r\nclass A:\r\n def __bool__(self) -> Literal[True]:\r\n return True\r\n\r\n @property\r\n def is_ok(self) -> Literal[True]:\r\n return True\r\n\r\n def ok(self) -> Literal[True]:\r\n return True\r\n\r\nclass B:\r\n def __bool__(self) -> Literal[False]:\r\n return False\r\n\r\n @property\r\n def is_ok(self) -> Literal[False]:\r\n return False\r\n\r\n def ok(self) -> Literal[False]:\r\n return False\r\n\r\n\r\ndef get_a_or_b() -> Union[A, B]: ...\r\n\r\nthing = get_a_or_b()\r\n\r\nif thing:\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\nelse:\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\n\r\nif bool(thing):\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\nelse:\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\n\r\nif thing.is_ok:\r\n reveal_type(thing) # Revealed type is 'main.A'\r\nelse:\r\n reveal_type(thing) # Revealed type is 'main.B'\r\n\r\nif thing.ok():\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\nelse:\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\n```\r\n\r\nI would expect that all examples will give the same result as the one with `is_ok` property. Maybe the `bool()` example would require some additional thought but it would seem wise to make it also work. Also, walrus operator makes it so that even the `is_ok` property example does not work. Other examples behave the same as before:\r\n\r\n```python\r\nif thing := get_a_or_b():\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\nelse:\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\n\r\nif bool(thing := get_a_or_b()):\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\nelse:\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\n\r\nif (thing := get_a_or_b()).is_ok:\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\nelse:\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\n\r\nif (thing := get_a_or_b()).ok():\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\nelse:\r\n reveal_type(thing) # Revealed type is 'Union[main.A, main.B]'\r\n```","author":{"url":"https://github.com/kprzybyla","@type":"Person","name":"kprzybyla"},"datePublished":"2020-07-27T21:55:42Z","url":"https://github.com/python/mypy/issues/9220"}
| 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:c71620c0-01f6-256f-ded2-b65cf4aeea78 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 831E:1756E4:1A00AEC:224F9AA:6B00301C |
| html-safe-nonce | 970dd6f44ad0463ccb8efab883ebddca2afc1856937353978c59f363db3b8dba |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MzFFOjE3NTZFNDoxQTAwQUVDOjIyNEY5QUE6NkIwMDMwMUMiLCJ2aXNpdG9yX2lkIjoiNDQyNjM2NDg0Mjg5NDQzODQyOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4e1d312bc42379a5da55cf04344a1c1a4cb479fae0094b4f2e4fd9f3e7e593da |
| hovercard-subject-tag | issue:666609092 |
| 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/9220/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1fc885e3f08d139053f315f93447bfb5b8af05db35b560bd837caa4553a20718/python/mypy/issues/9220 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1fc885e3f08d139053f315f93447bfb5b8af05db35b560bd837caa4553a20718/python/mypy/issues/9220 |
| og:image:alt | Used mypy version: 0.782 I stumbled upon some incomplete behavior related to Literal and Union. In most cases Union is not properly expanded upon conditional check when dealing with literals. Below... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kprzybyla |
| 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