Title: Treat bool as equivalent to Literal[True, False] to allow better type inference · Issue #6113 · python/mypy · GitHub
Open Graph Title: Treat bool as equivalent to Literal[True, False] to allow better type inference · Issue #6113 · python/mypy
X Title: Treat bool as equivalent to Literal[True, False] to allow better type inference · Issue #6113 · python/mypy
Description: Similar to this #6027, but more basic use case. from typing import Collection, Union, List def foo(bar: Union[bool, Collection[int]]) -> Union[int, List[int]]: if bar is False: # or if not bar: return 10 if bar is True: return 20 # other...
Open Graph Description: Similar to this #6027, but more basic use case. from typing import Collection, Union, List def foo(bar: Union[bool, Collection[int]]) -> Union[int, List[int]]: if bar is False: # or if not bar: ret...
X Description: Similar to this #6027, but more basic use case. from typing import Collection, Union, List def foo(bar: Union[bool, Collection[int]]) -> Union[int, List[int]]: if bar is False: # or if not bar: ...
Opengraph URL: https://github.com/python/mypy/issues/6113
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Treat bool as equivalent to Literal[True, False] to allow better type inference","articleBody":"Similar to this https://github.com/python/mypy/issues/6027, but more basic use case.\r\n\r\n```python\r\nfrom typing import Collection, Union, List\r\n\r\n\r\ndef foo(bar: Union[bool, Collection[int]]) -> Union[int, List[int]]:\r\n if bar is False: # or if not bar:\r\n return 10\r\n\r\n if bar is True:\r\n return 20\r\n\r\n # otherwise, bar must be a collection (list, tuple, ...)\r\n return [i*2 for i in bar]\r\n\r\n\r\nprint(foo(True))\r\nprint(foo(False))\r\nprint(foo([1, 2, 3]))\r\nprint(foo((1, 2, 3,)))\r\n```\r\n\r\nMypy is giving the error\r\n\r\n```error: Item \"bool\" of \"Union[bool, Collection[int]]\" has no attribute \"__iter__\" (not iterable)```\r\n\r\nBut it should be able to infer that it can't be a `bool`?\r\nusing if / elif /else also gives the error.\r\n","author":{"url":"https://github.com/ahmedsoe","@type":"Person","name":"ahmedsoe"},"datePublished":"2018-12-28T14:44:50Z","url":"https://github.com/python/mypy/issues/6113"}
| 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:2c1edc23-e1fb-59b8-da60-415d991a8380 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8460:1DA187:338FCA0:44510E0:6B003854 |
| html-safe-nonce | d6803ff49ba0baac801906d54edefd966b31dbf2a2e00e937da73eb1aae02c8e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NDYwOjFEQTE4NzozMzhGQ0EwOjQ0NTEwRTA6NkIwMDM4NTQiLCJ2aXNpdG9yX2lkIjoiNDc5OTk4NzMxNjQ0MzU5Mjc4OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a52f97697f33de68907c15a3b2e57cbbf2873f78e3fbd93b35404e7c34c3878a |
| hovercard-subject-tag | issue:394651867 |
| 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/6113/issue_layout |
| twitter:image | https://opengraph.githubassets.com/01e10c8cdf85975d709ce3bc96b97586c340997dde541b614458004b4f6009d1/python/mypy/issues/6113 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/01e10c8cdf85975d709ce3bc96b97586c340997dde541b614458004b4f6009d1/python/mypy/issues/6113 |
| og:image:alt | Similar to this #6027, but more basic use case. from typing import Collection, Union, List def foo(bar: Union[bool, Collection[int]]) -> Union[int, List[int]]: if bar is False: # or if not bar: ret... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ahmedsoe |
| 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 | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width