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

Forbid coroutines usage as boolean conditions #2694

Open
butvinm opened this issue Jul 18, 2023 · 0 comments
Open

Forbid coroutines usage as boolean conditions #2694

butvinm opened this issue Jul 18, 2023 · 0 comments
Labels
rule request Adding a new rule

Comments

@butvinm
Copy link

butvinm commented Jul 18, 2023

Rule request:

Restrict usage of coroutines (initialized, but not awaited) as boolean conditions

Thesis

Example:

async def some_coro() -> bool:
    return True

if some_coro(): # The rule should fail at this line
    pass

#  Also, it should work with variable declaration
supposed_boolean = some_coro()
if supposed_boolean: # The rule should fail at this line
    pass

If you use class-based awaitable with an overloaded __bool__ method and want to check its actual condition, you can use the following construction:

class MyCoro:
    def __await__():
        pass

    def __bool___():
         return True

coro_condition = bool(MyCoro()) # The rule is passed

Reasoning

This rule aims to prevent common errors that may occur due to overlooking the await keyword before using coroutines in if statements.

linters and type checkers do not consider the usage of coroutines in if statements as an error since any type can be treated as a boolean, leading to subtle bugs.

@butvinm butvinm added the rule request Adding a new rule label Jul 18, 2023
@butvinm butvinm changed the title Forbid coros usage as boolean conditions Forbid coroutines usage as boolean conditions Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule request Adding a new rule
Projects
None yet
Development

No branches or pull requests

1 participant