Skip to content

__bool__ check should look at Never return #1021

@ndmitchell

Description

@ndmitchell

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

Metadata

Metadata

Assignees

No one assigned
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions