-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af433ac
commit 1268ddc
Showing
9 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
crates/ruff/resources/test/fixtures/pylint/yield_from_in_async_function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
async def success(): | ||
yield 42 | ||
|
||
|
||
async def fail(): | ||
l = (1, 2, 3) | ||
yield from l |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
crates/ruff/src/rules/pylint/rules/yield_from_in_async_function.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use rustpython_parser::ast::{ExprYieldFrom, Ranged}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `yield from` in async functions. | ||
/// | ||
/// ## Why is this bad? | ||
/// Python doesn't support the use of `yield from` in async functions, and will | ||
/// raise a `SyntaxError` in such cases. | ||
/// | ||
/// Instead, considering refactoring the code to use an `async for` loop instead. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// async def numbers(): | ||
/// yield from [1, 2, 3, 4, 5] | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// async def numbers(): | ||
/// async for number in [1, 2, 3, 4, 5]: | ||
/// yield number | ||
/// ``` | ||
#[violation] | ||
pub struct YieldFromInAsyncFunction; | ||
|
||
impl Violation for YieldFromInAsyncFunction { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`yield from` statement in async function; use `async for` instead") | ||
} | ||
} | ||
|
||
/// PLE1700 | ||
pub(crate) fn yield_from_in_async_function(checker: &mut Checker, expr: &ExprYieldFrom) { | ||
let scope = checker.semantic_model().scope(); | ||
if scope.kind.is_async_function() { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(YieldFromInAsyncFunction, expr.range())); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...pylint/snapshots/ruff__rules__pylint__tests__PLE1700_yield_from_in_async_function.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
--- | ||
yield_from_in_async_function.py:7:5: PLE1700 `yield from` statement in async function; use `async for` instead | ||
| | ||
7 | async def fail(): | ||
8 | l = (1, 2, 3) | ||
9 | yield from l | ||
| ^^^^^^^^^^^^ PLE1700 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.