Skip to content

Commit

Permalink
Merge branch 'main' into PT012
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 17, 2025
2 parents 95c4457 + fa239f7 commit c2f7dc5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,9 @@ def f():
def f():
for x, y in z:
yield x, y, x + y


# https://github.com/astral-sh/ruff/issues/15540
def f():
for a in 1,:
yield a
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub(crate) fn yield_in_for_loop(checker: &mut Checker, stmt_for: &ast::StmtFor)
}

let mut diagnostic = Diagnostic::new(YieldInForLoop, stmt_for.range());

let contents = checker.locator().slice(
parenthesized_range(
iter.as_ref().into(),
Expand All @@ -123,7 +124,12 @@ pub(crate) fn yield_in_for_loop(checker: &mut Checker, stmt_for: &ast::StmtFor)
)
.unwrap_or(iter.range()),
);
let contents = format!("yield from {contents}");
let contents = if iter.as_tuple_expr().is_some_and(|it| !it.parenthesized) {
format!("yield from ({contents})")
} else {
format!("yield from {contents}")
};

diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
contents,
stmt_for.range(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
---
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
snapshot_kind: text
---
UP028_1.py:128:5: UP028 [*] Replace `yield` over `for` loop with `yield from`
|
126 | # https://github.com/astral-sh/ruff/issues/15540
127 | def f():
128 | / for a in 1,:
129 | | yield a
| |_______________^ UP028
|
= help: Replace with `yield from`

Unsafe fix
125 125 |
126 126 | # https://github.com/astral-sh/ruff/issues/15540
127 127 | def f():
128 |- for a in 1,:
129 |- yield a
128 |+ yield from (1,)

0 comments on commit c2f7dc5

Please sign in to comment.