Skip to content

Add truthy-callable-bool check #12621

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
ikonst opened this issue Apr 19, 2022 · 3 comments · Fixed by #13686
Closed

Add truthy-callable-bool check #12621

ikonst opened this issue Apr 19, 2022 · 3 comments · Fixed by #13686
Labels

Comments

@ikonst
Copy link
Contributor

ikonst commented Apr 19, 2022

Feature

Using a method as a property is a common bug that can be detected by a type checker:

if my_obj.my_method:
   ...

I'd like to introduce a way to enable a check like truthy-bool (#10666) but limited to callable types.

Pitch

Users users might be hesitant to enable truthy-bool due to false positives. For callable types, there's a much higher degree of confidence that evaluating a callable as a boolean is unintentional.

  • The error message should be specific, e.g.
    - error: Member "my_method" has type "function" which does not implement __bool__ or __len__ so it could always be true in boolean context 
    + error: Member "my_method" is a method (are you trying to use it as a property?)
    (happy to wordsmith this more, only trying to illustrate that I want it to be friendlier and more actionable)
  • The feature could be a separate error-code (thus splitting "callable evaluated in boolean context" from truthy-bool), or perhaps a way to configure truthy-bool to have a reduced scope? (Is there a common idiom for that?)
@ikonst ikonst added the feature label Apr 19, 2022
@ikonst
Copy link
Contributor Author

ikonst commented Apr 19, 2022

@hauntsaninja what do you think?

@erictraut
Copy link

FWIW, I recently added this check to pyright, and it was well received. This is a common programming error, and it can be detected with a negligible false positive rate. It would be great if mypy could detect and report this error.

@hauntsaninja
Copy link
Collaborator

I'm in favour, the reduced scope means we could likely have it on by default.

hauntsaninja added a commit that referenced this issue Oct 14, 2022
A separate error code for `truthy-function` so it can be enabled by default.

Closes #12621

Co-authored-by: Shantanu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants