python / python/mypy

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

Open
#14,529 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example and inspect how the [truthy-function] diagnostic formats callable expressions. Compare direct function references, module-qualified functions, methods, callable variables or attributes, and expressions returning callables. Done means direct references use the clearer function name where appropriate, while other cases retain a useful callable type or expression description.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.