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

Implement Pylint's yield-inside-async-function rule (PLE1700) #4668

Merged
merged 5 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3084,6 +3084,9 @@ where
if self.enabled(Rule::YieldInInit) {
pylint::rules::yield_in_init(self, expr);
}
if self.enabled(Rule::YieldFromInAsyncFunction) {
pylint::rules::yield_from_in_async_function(self, expr);
charliermarsh marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expr::Await(_) => {
if self.enabled(Rule::YieldOutsideFunction) {
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Pylint, "E1307") => (RuleGroup::Unspecified, Rule::BadStringFormatType),
(Pylint, "E1310") => (RuleGroup::Unspecified, Rule::BadStrStripCall),
(Pylint, "E1507") => (RuleGroup::Unspecified, Rule::InvalidEnvvarValue),
(Pylint, "E1700") => (RuleGroup::Unspecified, Rule::YieldFromInAsyncFunction),
(Pylint, "E2502") => (RuleGroup::Unspecified, Rule::BidirectionalUnicode),
(Pylint, "E2510") => (RuleGroup::Unspecified, Rule::InvalidCharacterBackspace),
(Pylint, "E2512") => (RuleGroup::Unspecified, Rule::InvalidCharacterSub),
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ ruff_macros::register_rules!(
// pylint
rules::pylint::rules::AssertOnStringLiteral,
rules::pylint::rules::UselessReturn,
rules::pylint::rules::YieldFromInAsyncFunction,
rules::pylint::rules::YieldInInit,
rules::pylint::rules::InvalidAllObject,
rules::pylint::rules::InvalidAllFormat,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/rules/pylint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ mod tests {
#[test_case(Rule::UselessElseOnLoop, Path::new("useless_else_on_loop.py"); "PLW0120")]
#[test_case(Rule::UselessImportAlias, Path::new("import_aliasing.py"); "PLC0414")]
#[test_case(Rule::UselessReturn, Path::new("useless_return.py"); "PLR1711")]
#[test_case(Rule::YieldFromInAsyncFunction, Path::new("yield_from_in_async_function.py"); "PLE1700")]
#[test_case(Rule::YieldInInit, Path::new("yield_in_init.py"); "PLE0100")]
#[test_case(Rule::NestedMinMax, Path::new("nested_min_max.py"); "PLW3301")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff/src/rules/pylint/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pub(crate) use unnecessary_direct_lambda_call::{
pub(crate) use useless_else_on_loop::{useless_else_on_loop, UselessElseOnLoop};
pub(crate) use useless_import_alias::{useless_import_alias, UselessImportAlias};
pub(crate) use useless_return::{useless_return, UselessReturn};
pub(crate) use yield_from_in_async_function::{
yield_from_in_async_function, YieldFromInAsyncFunction,
};
pub(crate) use yield_in_init::{yield_in_init, YieldInInit};

mod assert_on_string_literal;
Expand Down Expand Up @@ -91,4 +94,5 @@ mod unnecessary_direct_lambda_call;
mod useless_else_on_loop;
mod useless_import_alias;
mod useless_return;
mod yield_from_in_async_function;
mod yield_in_init;
43 changes: 43 additions & 0 deletions crates/ruff/src/rules/pylint/rules/yield_from_in_async_function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use rustpython_parser::ast::{Expr, Ranged, StmtAsyncFunctionDef};

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};

use ruff_python_semantic::scope::ScopeKind;

use crate::checkers::ast::Checker;

/// ## What it does
/// Checks if `yield from` is used inside of an async function
///
/// ## Why is this bad?
/// This will result in a `SyntaxError`
///
/// ## Example
/// ```python
/// def foo():
/// l = (1, 2, 3)
/// yield from l
/// ```
///
/// ## References
/// [pylint E1700: `yield-inside-async-function`] https://pylint.pycqa.org/en/latest/user_guide/messages/error/yield-inside-async-function.html
#[violation]
pub struct YieldFromInAsyncFunction;

impl Violation for YieldFromInAsyncFunction {
#[derive_message_formats]
fn message(&self) -> String {
format!("`yield from` inside async function consider refactoring with `async for`")
}
}

/// PLE1700
pub(crate) fn yield_from_in_async_function(checker: &mut Checker, expr: &Expr) {
let scope = checker.semantic_model().scope();
if let ScopeKind::AsyncFunction(StmtAsyncFunctionDef { .. }) = scope.kind {
checker
.diagnostics
.push(Diagnostic::new(YieldFromInAsyncFunction, expr.range()));
}
}
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` inside async function consider refactoring with `async for`
|
7 | async def fail():
8 | l = (1, 2, 3)
9 | yield from l
| ^^^^^^^^^^^^ PLE1700
|


3 changes: 3 additions & 0 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.