__bool__ check should look at Never return
- Dominant language
- Rust
- Stars
- 7k
- Forks
- 519
- PR merge metrics
- No merged PRs in 30d
Description
### Describe the Bug
```python
from typing import Never
class Foo:
def __bool__(self) -> Never:
raise TypeError
if Foo():
print("Hello World")
```
This works fine, but if I change it to `__bool__ : int` then it gives an error that `Foo` cannot be used as a boolean. It should raise an error even above. The above formulation causes the expected error in Pyright, and recommended in https://discuss.python.org/t/how-to-get-a-type-check-error-on-bool/74879/7
The code in question is at https://github.com/facebook/pyrefly/blob/54a40386569be3c53f3c760611d7f5b1a1793464/pyrefly/lib/alt/attr.rs#L1695-L1709. We check if the type itself is union or never, then get the call target. We should probably be looking at the return type of the call target.
### Sandbox Link
https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoByApgG5EgBQFAxgDYCGAzo1AGJhgBcFUvAJkWBQA%2BsIBGHWqIAUjIrWABKKAFoAfIVLluvXSHpI5UACqIiAURDhKFJEPZhpinboQhUMaQCIAEvNpgUADquLR8XopAA&version=3.12
### (Only applicable for extension issues) IDE Information
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.