Skip to content
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

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

Open
JukkaL opened this issue Jan 26, 2023 · 6 comments
Open

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

JukkaL opened this issue Jan 26, 2023 · 6 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 26, 2023

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.
@ashaik4
Copy link

ashaik4 commented Feb 1, 2023

@JukkaL This is my initial attempt to make the message better. Please let me know if that looks good.
I will proceed with other cases as mentioned above.

@Aryanryn09
Copy link

can we implement this behaviour instead

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

if callable(f):
pass

@ikonst
Copy link
Contributor

ikonst commented Mar 17, 2023

While at it, would it favor usability by adding the presumptuous "; did you mean to call it?" ...?

@spatinom
Copy link

can this be assigned to me?

tgo-netizen added a commit to tgo-netizen/mypy that referenced this issue Apr 4, 2023
Better message for "Function ,could always be true" python#14529
@Neo77-cyber
Copy link

Is this issue still open?

@JukkaL
Copy link
Collaborator Author

JukkaL commented Apr 22, 2023

This issue is still open, since there is no PR with test cases so we don't whether any attempts are working or not. PRs are welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants