Title: disambiguate booleans on conditions ("and" & "or" operators) · Issue #1698 · python/mypy · GitHub
Open Graph Title: disambiguate booleans on conditions ("and" & "or" operators) · Issue #1698 · python/mypy
X Title: disambiguate booleans on conditions ("and" & "or" operators) · Issue #1698 · python/mypy
Description: I've noticed many false positive type errors related to "and" and "or" operators because they have the tendency to create union types. I'm working with some program which I reduced to the following example: REGEX = re.compile(r'(a*)(b*)(...
Open Graph Description: I've noticed many false positive type errors related to "and" and "or" operators because they have the tendency to create union types. I'm working with some program which I reduced to the following...
X Description: I've noticed many false positive type errors related to "and" and "or" operators because they have the tendency to create union types. I'm working with some program whic...
Opengraph URL: https://github.com/python/mypy/issues/1698
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"disambiguate booleans on conditions (\"and\" & \"or\" operators)","articleBody":"I've noticed many false positive type errors related to \"and\" and \"or\" operators because they have the tendency to create union types. I'm working with some program which I reduced to the following example:\n\n``` python\nREGEX = re.compile(r'(a*)(b*)(c*)')\n\ndef example(s: str, ignore_matches: bool) -> int:\n match = not ignore_matches and REGEX.search(s)\n if match:\n x = match.group(1) or match.group(3)\n y = match.start(2)\n return len(x) + y\n```\n\nmypy figures out that the type of match is `Union[bool, re.Match]` (`re.Match` actually doesn't exist, so I can't workaround this with an `isinstance()` check here but this is not the main point here). So inside the if, I get:\n\n```\nfoo.py:9: error: Some element of union has no attribute \"group\"\nfoo.py:10: error: Some element of union has no attribute \"start\"\n```\n\nI understand why this happens, but I'm thinking that this could be analyzed statically, if mypy actually has some \"always True\" and \"always False\" types as subtypes of \"Boolean\". This could help in \"and\" and \"or\" expressions. The type of the expression above would be `Union[FalseType, re.Match]`. Which can be analyzed in an if removing the \"FalseType\" option from the Union, which gives an accurate typing of re.Match.\n\nA dual situation can be found if you have `boolexp or other_exp` which could be typed as `Union[TrueType, other_type]`.\n\nA possible generalization of this is having additional type information for all types saying \"can be true-ish\", \"can be false-ish\"; and that type information can be narrowed down on boolean operators and ifs.\n\nOn first sight, these ideas may look like going beyond the idea of a type checker and more into a program verifier but that's not actually what I'm proposing; the thing is that in actual programs this kind of checks are actually a way to narrow down union types, and my increase the effectiveness of mypy as a typechecker.\n","author":{"url":"https://github.com/dmoisset","@type":"Person","name":"dmoisset"},"datePublished":"2016-06-11T12:18:18Z","url":"https://github.com/python/mypy/issues/1698"}
| 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:bbdffebd-8716-2080-7b5f-64ee37065181 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E810:145630:303042D:3FE6D01:6B002F45 |
| html-safe-nonce | 65d61e7a04cd7638718da97acaf7e3ccd1ef0f3b3572b868f1df0bedec30a765 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFODEwOjE0NTYzMDozMDMwNDJEOjNGRTZEMDE6NkIwMDJGNDUiLCJ2aXNpdG9yX2lkIjoiNDI2MzI1ODY2NjY0NTMzNTg3NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 30e941126e1a185c0746ec27d09dd6066f7fd036cd07491d130151fdc365555d |
| hovercard-subject-tag | issue:159771001 |
| 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/1698/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f0ff9bed12a9350f6a60143f68a1bd98caa9ec2ba9cb009225c4fadb1d763e5b/python/mypy/issues/1698 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f0ff9bed12a9350f6a60143f68a1bd98caa9ec2ba9cb009225c4fadb1d763e5b/python/mypy/issues/1698 |
| og:image:alt | I've noticed many false positive type errors related to "and" and "or" operators because they have the tendency to create union types. I'm working with some program which I reduced to the following... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dmoisset |
| 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