Skip to content

(🎁) final class without a __bool__ or __len__ method can never test false #12158

Closed
@KotlinIsland

Description

@KotlinIsland

This clashes with truthy-bool error code, which assumes that subtypes won't implement these methods.

from typing import final

@final
class A:
    b: int

if A():
    print("hi")  # no unreachable error

print(A() and "hi")

Any instance of A will never have a __bool__ or __len__, method so these condition will always test true.

More real life scenario:

from typing import final

@final
class A:
    b: int
    
a: A | None
r1: int | None = a.b if a else None
r2: int | None = a and a.b  # error: Incompatible types in assignment (expression has type "Union[A, None, int]", variable has type "Optional[int]")

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions