microsoft / pyright

Static Type Checker for Python
Other
13.32k stars 1.45k forks source link

Provide diagnostics for missing brackets. #8693

Closed uKaigo closed 2 months ago

uKaigo commented 2 months ago

Is your feature request related to a problem? Please describe. Currently, if you reference a callable that returns a bool inside a comparision statement without calling it, pyright does not detect an error under normal mode. In strict mode, it does error with reportUnecessaryComparision but that's because the callable is truthy.

Describe the solution you’d like Ideally, pyright could provide a slightly different message from reportUnecessaryComparision, under default mode. Something like Conditional expression references callable which returns a bool.

def is_even(x: int) -> bool:
    return x % 2 == 0

# Could detect that is_even is a "(...) -> bool" and report it as a possible programming error.
if is_even:
    print('2 is even!')
erictraut commented 2 months ago

As you mentioned, this is already covered under the reportUnecessaryComparision rule. I don't think that a separate redundant check makes sense here.

uKaigo commented 2 months ago

Yes, to be honest my reasoning was to make it not require strict mode to be caught. But I guess if someone needs this type of thing they should have it on.