Skip to content

truthy-bool check does not check the not operator #17769

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
komodovaran opened this issue Sep 13, 2024 · 1 comment · Fixed by #17773
Closed

truthy-bool check does not check the not operator #17769

komodovaran opened this issue Sep 13, 2024 · 1 comment · Fixed by #17773

Comments

@komodovaran
Copy link

class Foo:
    @property
    def prop(self) -> bool:
        return False


def bools_only(value: bool) -> bool:
    return value


bools_only(Foo().prop) # Standard behavior of properties
bools_only(Foo.prop) # Expected type 'bool', got 'property' instead 
bools_only(not Foo.prop) # no error after casting with `not`!

Python interprets not object as not bool(object) which doesn't seem to be covered by any checks.

Tested with Mypy 1.11.2 and Python 3.12

https://mypy-play.net/?mypy=latest&python=3.12&gist=1c5f3510b686dd0e2e04e0a9ef191a96

@komodovaran komodovaran added the bug mypy got something wrong label Sep 13, 2024
@JelleZijlstra
Copy link
Member

This falls conceptually within the https://mypy.readthedocs.io/en/stable/error_code_list2.html#check-that-expression-is-not-implicitly-true-in-boolean-context-truthy-bool code. However, it doesn't appear mypy checks for this at all in the not operator.

To fix this,

result: Type = self.bool_type()
needs to be changed to call a variation of
def _check_for_truthy_type(self, t: Type, expr: Expression) -> None:
.

@JelleZijlstra JelleZijlstra changed the title Python's not allows arbitrary objects to be bools truthy-bool check does not check the not operator Sep 13, 2024
@JelleZijlstra JelleZijlstra added feature and removed bug mypy got something wrong labels Sep 13, 2024
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.

3 participants