Describe the Bug
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
|
if let Some(ty) = cond_bool_ty |
|
&& !matches!(ty, Type::Union(_) | Type::Never(_)) |
|
&& self.as_call_target(ty.clone()).is_none() |
|
{ |
|
self.error( |
|
errors, |
|
range, |
|
ErrorInfo::Kind(ErrorKind::InvalidArgument), |
|
format!( |
|
"`{}.__bool__` has type `{}`, which is not callable", |
|
self.for_display(condition_type.clone()), |
|
self.for_display(ty.clone()), |
|
), |
|
); |
|
} |
. 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
Describe the Bug
This works fine, but if I change it to
__bool__ : intthen it gives an error thatFoocannot 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/7The code in question is at
pyrefly/pyrefly/lib/alt/attr.rs
Lines 1695 to 1709 in 54a4038
Sandbox Link
https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoByApgG5EgBQFAxgDYCGAzo1AGJhgBcFUvAJkWBQA%2BsIBGHWqIAUjIrWABKKAFoAfIVLluvXSHpI5UACqIiAURDhKFJEPZhpinboQhUMaQCIAEvNpgUADquLR8XopAA&version=3.12
(Only applicable for extension issues) IDE Information
No response