Better message for "Function ... could always be true"
Nobody has claimed this yet.
- 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:
- Direct reference to a function using a short name of a function (similar to the above example).
- Direct reference to a function using a module prefix (e.g.
if mod.func:). - Reference to a method (e.g.
if obj.method:). - Reference to a variable/attribute (
Varnode) with typeCallable[...](in this case we could show the name of the variable/attribute or the callable type -- both would be okay). - Any other expression that produces a callable value, such as
if func():wherefuncreturns a callable. Here the best option is to show the callable type as we do now.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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