Skip to content

Add truthy-callable-bool check #12621

Closed
@ikonst

Description

@ikonst

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?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions