From 178c882740b734860de401fbc240e07966d9d53a Mon Sep 17 00:00:00 2001 From: Max Mynter <32773644+maxmynter@users.noreply.github.com> Date: Mon, 5 May 2025 20:02:06 +0200 Subject: [PATCH] [semantic-syntax-tests] Add test fixtures for `AwaitOutsideAsyncFunction` (#17785) Re: #17526 ## Summary Add test fixtures for `AwaitOutsideAsync` and `AsyncComprehensionOutsideAsyncFunction` errors. ## Test Plan This is a test. --- .../syntax_errors/async_comprehension.py | 11 +++++++ .../await_outside_async_function.py | 5 +++ crates/ruff_linter/src/linter.rs | 2 ++ ...linter__tests__async_comprehension.py.snap | 31 +++++++++++++++++++ ...ests__await_outside_async_function.py.snap | 18 +++++++++++ 5 files changed, 67 insertions(+) create mode 100644 crates/ruff_linter/resources/test/fixtures/syntax_errors/async_comprehension.py create mode 100644 crates/ruff_linter/resources/test/fixtures/syntax_errors/await_outside_async_function.py create mode 100644 crates/ruff_linter/src/snapshots/ruff_linter__linter__tests__async_comprehension.py.snap create mode 100644 crates/ruff_linter/src/snapshots/ruff_linter__linter__tests__await_outside_async_function.py.snap diff --git a/crates/ruff_linter/resources/test/fixtures/syntax_errors/async_comprehension.py b/crates/ruff_linter/resources/test/fixtures/syntax_errors/async_comprehension.py new file mode 100644 index 0000000000..c2000f141a --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/syntax_errors/async_comprehension.py @@ -0,0 +1,11 @@ +async def elements(n): + yield n + +def regular_function(): + [x async for x in elements(1)] + + async with elements(1) as x: + pass + + async for _ in elements(1): + pass diff --git a/crates/ruff_linter/resources/test/fixtures/syntax_errors/await_outside_async_function.py b/crates/ruff_linter/resources/test/fixtures/syntax_errors/await_outside_async_function.py new file mode 100644 index 0000000000..dd49cb05f9 --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/syntax_errors/await_outside_async_function.py @@ -0,0 +1,5 @@ +def func(): + await 1 + +# Top-level await +await 1 diff --git a/crates/ruff_linter/src/linter.rs b/crates/ruff_linter/src/linter.rs index f2445b128c..b41ce44a6b 100644 --- a/crates/ruff_linter/src/linter.rs +++ b/crates/ruff_linter/src/linter.rs @@ -1157,6 +1157,8 @@ mod tests { Rule::LoadBeforeGlobalDeclaration, Path::new("load_before_global_declaration.py") )] + #[test_case(Rule::AwaitOutsideAsync, Path::new("await_outside_async_function.py"))] + #[test_case(Rule::AwaitOutsideAsync, Path::new("async_comprehension.py"))] fn test_syntax_errors(rule: Rule, path: &Path) -> Result<()> { let snapshot = path.to_string_lossy().to_string(); let path = Path::new("resources/test/fixtures/syntax_errors").join(path); diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__linter__tests__async_comprehension.py.snap b/crates/ruff_linter/src/snapshots/ruff_linter__linter__tests__async_comprehension.py.snap new file mode 100644 index 0000000000..9e570b889c --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__linter__tests__async_comprehension.py.snap @@ -0,0 +1,31 @@ +--- +source: crates/ruff_linter/src/linter.rs +--- +resources/test/fixtures/syntax_errors/async_comprehension.py:5:8: PLE1142 `await` should be used within an async function + | +4 | def regular_function(): +5 | [x async for x in elements(1)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PLE1142 +6 | +7 | async with elements(1) as x: + | + +resources/test/fixtures/syntax_errors/async_comprehension.py:7:5: PLE1142 `await` should be used within an async function + | + 5 | [x async for x in elements(1)] + 6 | + 7 | / async with elements(1) as x: + 8 | | pass + | |____________^ PLE1142 + 9 | +10 | async for _ in elements(1): + | + +resources/test/fixtures/syntax_errors/async_comprehension.py:10:5: PLE1142 `await` should be used within an async function + | + 8 | pass + 9 | +10 | / async for _ in elements(1): +11 | | pass + | |____________^ PLE1142 + | diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__linter__tests__await_outside_async_function.py.snap b/crates/ruff_linter/src/snapshots/ruff_linter__linter__tests__await_outside_async_function.py.snap new file mode 100644 index 0000000000..fb45718a9a --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__linter__tests__await_outside_async_function.py.snap @@ -0,0 +1,18 @@ +--- +source: crates/ruff_linter/src/linter.rs +--- +resources/test/fixtures/syntax_errors/await_outside_async_function.py:2:5: PLE1142 `await` should be used within an async function + | +1 | def func(): +2 | await 1 + | ^^^^^^^ PLE1142 +3 | +4 | # Top-level await + | + +resources/test/fixtures/syntax_errors/await_outside_async_function.py:5:1: PLE1142 `await` should be used within an async function + | +4 | # Top-level await +5 | await 1 + | ^^^^^^^ PLE1142 + |