Skip to content

Better message for "Function ... could always be true" #14529

Open
@JukkaL

Description

@JukkaL

Consider this example:

def f(x: int) -> str: pass

if f:
    pass

It generates this error:

t.py:3: error: Function "Callable[[int], str]" could always be true in boolean context  [truthy-function]

Instead, if we have a direct reference to a function definition, we could generate a message like this which would be clearer, especially if the signature is complex:

t.py:3: error: Function "f" could always be true in boolean context  [truthy-function]

There are at least these different cases to consider:

  1. Direct reference to a function using a short name of a function (similar to the above example).
  2. Direct reference to a function using a module prefix (e.g. if mod.func:).
  3. Reference to a method (e.g. if obj.method:).
  4. Reference to a variable/attribute (Var node) with type Callable[...] (in this case we could show the name of the variable/attribute or the callable type -- both would be okay).
  5. Any other expression that produces a callable value, such as if func(): where func returns a callable. Here the best option is to show the callable type as we do now.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions