facebook / facebook/pyrefly

__bool__ check should look at Never return

Open
#1,021 1 comment 0 reactions 1 assignee Claimed by @asukaminato0721 View on GitHub
typechecking
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.