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

Asyncio lint errors -- ruff check --select=ASYNC #1999

Open
cclauss opened this issue Nov 21, 2024 · 0 comments
Open

Asyncio lint errors -- ruff check --select=ASYNC #1999

cclauss opened this issue Nov 21, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@cclauss
Copy link
Contributor

cclauss commented Nov 21, 2024

Describe the bug
% ruff check --select=ASYNC --statistics

54	ASYNC230	blocking-open-call-in-async-function
 6	ASYNC221	run-process-in-async-function

Expected behavior
What was supposed to happen?
% ruff check --select=ASYNC --statistics

No errors.

% ruff rule ASYNC230

blocking-open-call-in-async-function (ASYNC230)

Derived from the flake8-async linter.

What it does

Checks that async functions do not open files with blocking methods like open.

Why is this bad?

Blocking an async function via a blocking call will block the entire
event loop, preventing it from executing other tasks while waiting for the
call to complete, negating the benefits of asynchronous programming.

Instead of making a blocking call, use an equivalent asynchronous library
or function.

Example

async def foo():
    with open("bar.txt") as f:
        contents = f.read()

Use instead:

import anyio


async def foo():
    async with await anyio.open_file("bar.txt") as f:
        contents = await f.read()

% ruff rule ASYNC221

run-process-in-async-function (ASYNC221)

Derived from the flake8-async linter.

What it does

Checks that async functions do not run processes with blocking methods.

Why is this bad?

Blocking an async function via a blocking call will block the entire
event loop, preventing it from executing other tasks while waiting for the
call to complete, negating the benefits of asynchronous programming.

Instead of making a blocking call, use an equivalent asynchronous library
or function.

Example

async def foo():
    subprocess.run(cmd)

Use instead:

async def foo():
    asyncio.create_subprocess_shell(cmd)
@cclauss cclauss added the bug Something isn't working label Nov 21, 2024
@cclauss cclauss changed the title Asyncio lint errors -- ruff check ASYNC221,ASYNC230 Asyncio lint errors -- ruff check ASYNC221,ASYNC230 Nov 21, 2024
@cclauss cclauss changed the title Asyncio lint errors -- ruff check ASYNC221,ASYNC230 Asyncio lint errors -- ruff check --select=ASYNC Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant