Title: bool(obj) does not infer from obj.__bool__() · Issue #15523 · python/mypy · GitHub
Open Graph Title: bool(obj) does not infer from obj.__bool__() · Issue #15523 · python/mypy
X Title: bool(obj) does not infer from obj.__bool__() · Issue #15523 · python/mypy
Description: Bug Report (Although there are other issues that allude to this problem, I haven't been able to locate an issue which specifically addresses truthiness where __bool__() is defined and @final. The search term "truthy bool" may be helpful ...
Open Graph Description: Bug Report (Although there are other issues that allude to this problem, I haven't been able to locate an issue which specifically addresses truthiness where __bool__() is defined and @final. The s...
X Description: Bug Report (Although there are other issues that allude to this problem, I haven't been able to locate an issue which specifically addresses truthiness where __bool__() is defined and @final. T...
Opengraph URL: https://github.com/python/mypy/issues/15523
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"bool(obj) does not infer from obj.__bool__()","articleBody":"**Bug Report**\r\n\r\n_(Although there are other issues that allude to this problem, I haven't been able to locate an issue which specifically addresses truthiness where `__bool__()` is defined and `@final`. The search term \"truthy bool\" may be helpful for finding related issues.)_\r\n\r\nImplicit truthiness does not (always?) take an object's `__bool__()` into account, even when that method is `@final`.\r\n\r\n**To Reproduce**\r\n\r\nThis example is posted to [mypy-play.net](https://mypy-play.net/?mypy=master&python=3.11&gist=4f03e09fa9453a93c95bf18ca3c6bac7).\r\n\r\n```python\r\nfrom typing import *\r\n\r\n@final\r\nclass SomeClass:\r\n def __bool__(self) -> Literal[True]:\r\n return True\r\n\r\ninstance: SomeClass\r\n\r\nreveal_type(instance.__bool__()) # 'Literal[True]' - correct\r\nreveal_type(bool(instance)) # 'bool' - incorrect (should be 'Literal[True]')\r\n\r\nif instance:\r\n reveal_type(instance) # 'SomeClass' - correct\r\nelse:\r\n assert_never(instance) # ...'SomeClass' - incorrect (should be silent)\r\n```\r\n\r\n(Whether the class or method is decorated `@final` makes no difference.)\r\n\r\n**Expected Behavior**\r\n\r\n`if instance: ...` and `bool(instnace)` should take into account that `SomeClass` has a `@final` `__bool__()` with return type `Literal[True]`, and consistently treat instances of `SomeClass` as definitely truthy.\r\n\r\nIn general, implicit truthiness for an object should take into account the truthiness of the return type of any `@final` `__bool__()` the object's class defines.\r\n\r\nComments inline in the above code highlight differences from expected behaviour.\r\n\r\n**Actual Behavior**\r\n\r\n```\r\nmain.py:10: note: Revealed type is \"Literal[True]\"\r\nmain.py:11: note: Revealed type is \"builtins.bool\"\r\nmain.py:14: note: Revealed type is \"__main__.SomeClass\"\r\nmain.py:16: error: Argument 1 to \"assert_never\" has incompatible type \"SomeClass\"; expected \"NoReturn\" [arg-type]\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: 1.4.0, master (2023-06-26)\r\n- Mypy command-line flags: (none)\r\n- Mypy configuration options from `mypy.ini` (and other config files): (none)\r\n- Python version used: 3.11\r\n","author":{"url":"https://github.com/finite-state-machine","@type":"Person","name":"finite-state-machine"},"datePublished":"2023-06-26T15:43:10Z","url":"https://github.com/python/mypy/issues/15523"}
| 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:07d82d3b-62cf-5d42-1461-670367226d1b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9C14:15DE5D:103A814:154D894:6B00064C |
| html-safe-nonce | 04f0003c0e271cf08fc6bf2b797fd2de22df987643345206c414f952ea91b151 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QzE0OjE1REU1RDoxMDNBODE0OjE1NEQ4OTQ6NkIwMDA2NEMiLCJ2aXNpdG9yX2lkIjoiMTU1OTUzNzc3MjY5MDE0Njg5MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 285cb4a416aab8007230e8a102e4b72735cef0a41df49de4eb2c6d4f5a801b15 |
| hovercard-subject-tag | issue:1775081034 |
| 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/15523/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3909d6d19e503b15f2555eb24558a1c36918c828b7fab32bbdc944b9b218c823/python/mypy/issues/15523 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3909d6d19e503b15f2555eb24558a1c36918c828b7fab32bbdc944b9b218c823/python/mypy/issues/15523 |
| og:image:alt | Bug Report (Although there are other issues that allude to this problem, I haven't been able to locate an issue which specifically addresses truthiness where __bool__() is defined and @final. The s... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | finite-state-machine |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width