Skip to content

__bool__ should fall back to __len__ when it is undefined #700

@tatyam-prime

Description

@tatyam-prime

Hello! I'm trying to use Codon.

According to https://docs.python.org/3.13/reference/datamodel.html#object.__bool__, if a class does not define __bool__, Python should call __len__ to determine truthiness.

However, in Codon 0.19.3, attempting to evaluate such an object in a boolean context causes an error.

Example:

class A:
    def __len__(self) -> int:
        return 0

a = A()
print(bool(a))

This should output:

False

But Codon raises an error:

bool.codon:12 (16-31): error: 'A' object has no attribute '__bool__'
╰─ Main.py:6 (7-14): error: during the realization of __new__(what: A)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions