Title: Type of 'and' expressions may be too general · Issue #2716 · python/mypy · GitHub
Open Graph Title: Type of 'and' expressions may be too general · Issue #2716 · python/mypy
X Title: Type of 'and' expressions may be too general · Issue #2716 · python/mypy
Description: Mypy complains about this code, but perhaps it shouldn't (I'm assuming no --strict-optional): class A: pass def f(a: A, b: bool) -> bool: return a and b # Incompatible return value type (got "Union[A, bool]", expected "bool") f may retur...
Open Graph Description: Mypy complains about this code, but perhaps it shouldn't (I'm assuming no --strict-optional): class A: pass def f(a: A, b: bool) -> bool: return a and b # Incompatible return value type (got "Union...
X Description: Mypy complains about this code, but perhaps it shouldn't (I'm assuming no --strict-optional): class A: pass def f(a: A, b: bool) -> bool: return a and b # Incompatible return value type ...
Opengraph URL: https://github.com/python/mypy/issues/2716
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type of 'and' expressions may be too general","articleBody":"Mypy complains about this code, but perhaps it shouldn't (I'm assuming no `--strict-optional`):\r\n\r\n```\r\nclass A: pass\r\n\r\ndef f(a: A, b: bool) -> bool:\r\n return a and b # Incompatible return value type (got \"Union[A, bool]\", expected \"bool\")\r\n```\r\n\r\n`f` may return a `bool` object (if `a` is not `None`) or `None` (if `a` is `None`) but it can never return an `A` instance (unless a subclass of `A` defines `__bool__` -- which we might not care about) so the inferred type for the return value may not be what we want.\r\n\r\nI think I saw some real-world false positives caused by this, though I didn't look very carefully.\r\n\r\nWhen using `--strict-optional`, we'll probably require using `bool(...)` around the return value since the type would otherwise be `Optional[bool]`, which is not compatible with `bool`.\r\n\r\n(I remember having a similar earlier discussion but couldn't find an existing issue.)","author":{"url":"https://github.com/JukkaL","@type":"Person","name":"JukkaL"},"datePublished":"2017-01-19T16:59:56Z","url":"https://github.com/python/mypy/issues/2716"}
| 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:3103056a-3645-9d03-49cf-58733cd05a65 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8274:33DDEF:206B975:2C604CC:6B002A99 |
| html-safe-nonce | eb2c0d5f3e52ea445a606fa8c11760de2f6c845c4c5e29c22e15efa7ffb71ce3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Mjc0OjMzRERFRjoyMDZCOTc1OjJDNjA0Q0M6NkIwMDJBOTkiLCJ2aXNpdG9yX2lkIjoiMzE5NTgxMTU1NTMwNDI4Njg3MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f59d36b29cb9238cb31ce6ee20283f0588691f3428db13c3e53a080b99652ddb |
| hovercard-subject-tag | issue:201917403 |
| 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/2716/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e927ad92fc8f5a3472a374aff8b18d6bdb76da468c4015bebbda737731be9e62/python/mypy/issues/2716 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e927ad92fc8f5a3472a374aff8b18d6bdb76da468c4015bebbda737731be9e62/python/mypy/issues/2716 |
| og:image:alt | Mypy complains about this code, but perhaps it shouldn't (I'm assuming no --strict-optional): class A: pass def f(a: A, b: bool) -> bool: return a and b # Incompatible return value type (got "Union... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JukkaL |
| 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