From b23b4071eba46d8b5b3ce07ba6ce9916b9f8b2f0 Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Mon, 30 Jun 2025 06:47:29 -0700 Subject: [PATCH] [`flake8-async`] Make `ASYNC220`, `ASYNC221`, and `ASYNC222` examples error out-of-the-box (#18978) ## Summary Part of #18972 All three in one PR since they are in the same file. This PR makes [create-subprocess-in-async-function (ASYNC220)](https://docs.astral.sh/ruff/rules/create-subprocess-in-async-function/#create-subprocess-in-async-function-async220)'s example error out-of-the-box [Old example](https://play.ruff.rs/465036af-d75f-4bda-ba24-e50e8618bf16) ```py async def foo(): os.popen(cmd) ``` [New example](https://play.ruff.rs/8cf43d50-f9e1-45d6-b711-968c7135f2e0) ```py import os async def foo(): os.popen(cmd) ``` Imports were also added to the `Use instead:` section to make it valid code out-of-the-box. This PR makes [run-process-in-async-function (ASYNC221)](https://docs.astral.sh/ruff/rules/run-process-in-async-function/#run-process-in-async-function-async221)'s example error out-of-the-box [Old example](https://play.ruff.rs/0698aaa1-c722-4f04-b56c-61edec06945c) ```py async def foo(): subprocess.run(cmd) ``` [New example](https://play.ruff.rs/e05bfcbc-e681-4a28-8f50-2c0c2537d038) ```py import subprocess async def foo(): subprocess.run(cmd) ``` Imports were also added to the `Use instead:` section to make it valid code out-of-the-box. This PR makes [wait-for-process-in-async-function (ASYNC222)](https://docs.astral.sh/ruff/rules/wait-for-process-in-async-function/#wait-for-process-in-async-function-async222)'s example error out-of-the-box [Old example](https://play.ruff.rs/4305d477-8995-462d-83ae-435731d71e67) ```py async def foo(): os.waitpid(0) ``` [New example](https://play.ruff.rs/ad10c042-3b18-49ca-8f5c-5ab720516da1) ```py import os async def foo(): os.waitpid(0) ``` Imports were also added to the `Use instead:` section to make it valid code out-of-the-box. ## Test Plan N/A, no functionality/tests affected --- .../rules/blocking_process_invocation.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/ruff_linter/src/rules/flake8_async/rules/blocking_process_invocation.rs b/crates/ruff_linter/src/rules/flake8_async/rules/blocking_process_invocation.rs index e056d324aa..2995ab0e3c 100644 --- a/crates/ruff_linter/src/rules/flake8_async/rules/blocking_process_invocation.rs +++ b/crates/ruff_linter/src/rules/flake8_async/rules/blocking_process_invocation.rs @@ -21,12 +21,18 @@ use crate::checkers::ast::Checker; /// /// ## Example /// ```python +/// import os +/// +/// /// async def foo(): /// os.popen(cmd) /// ``` /// /// Use instead: /// ```python +/// import asyncio +/// +/// /// async def foo(): /// asyncio.create_subprocess_shell(cmd) /// ``` @@ -54,12 +60,18 @@ impl Violation for CreateSubprocessInAsyncFunction { /// /// ## Example /// ```python +/// import subprocess +/// +/// /// async def foo(): /// subprocess.run(cmd) /// ``` /// /// Use instead: /// ```python +/// import asyncio +/// +/// /// async def foo(): /// asyncio.create_subprocess_shell(cmd) /// ``` @@ -87,12 +99,19 @@ impl Violation for RunProcessInAsyncFunction { /// /// ## Example /// ```python +/// import os +/// +/// /// async def foo(): /// os.waitpid(0) /// ``` /// /// Use instead: /// ```python +/// import asyncio +/// import os +/// +/// /// def wait_for_process(): /// os.waitpid(0) ///