Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
KotlinIsland opened this issue Feb 10, 2022 · 2 comments 路 May be fixed by #12187
Closed

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

KotlinIsland opened this issue Feb 10, 2022 · 2 comments 路 May be fixed by #12187

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Feb 10, 2022

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]")
@KotlinIsland KotlinIsland added the bug mypy got something wrong label Feb 10, 2022
@hauntsaninja hauntsaninja added feature and removed bug mypy got something wrong labels Feb 15, 2022
@KotlinIsland KotlinIsland changed the title Final class without __bool__ method can never be false Final class without a __bool__ or __len__ method can never test false Feb 16, 2022
@KotlinIsland KotlinIsland changed the title Final class without a __bool__ or __len__ method can never test false (馃巵) final class without a __bool__ or __len__ method can never test false Mar 19, 2022
@AlexWaygood AlexWaygood added the topic-final PEP 591 label Mar 25, 2022
@KotlinIsland
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants