From f07d35051c7b670f55a865af132158079a717756 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 2 Jan 2024 15:44:03 -0400 Subject: [PATCH] Add fix safety note for yield-in-for-loop (#9364) See: https://github.com/astral-sh/ruff/issues/8482. --- .../src/rules/pyupgrade/rules/yield_in_for_loop.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/ruff_linter/src/rules/pyupgrade/rules/yield_in_for_loop.rs b/crates/ruff_linter/src/rules/pyupgrade/rules/yield_in_for_loop.rs index 6325cc9fd2b2f..d371eb96610aa 100644 --- a/crates/ruff_linter/src/rules/pyupgrade/rules/yield_in_for_loop.rs +++ b/crates/ruff_linter/src/rules/pyupgrade/rules/yield_in_for_loop.rs @@ -24,6 +24,16 @@ use crate::checkers::ast::Checker; /// yield from foo /// ``` /// +/// ## Fix safety +/// This rule's fix is marked as unsafe, as converting a `for` loop to a `yield +/// from` expression can change the behavior of the program in rare cases. +/// For example, if a generator is being sent values via `send`, then rewriting +/// to a `yield from` could lead to an attribute error if the underlying +/// generator does not implement the `send` method. +/// +/// In most cases, however, the fix is safe, and such a modification should have +/// no effect on the behavior of the program. +/// /// ## References /// - [Python documentation: The `yield` statement](https://docs.python.org/3/reference/simple_stmts.html#the-yield-statement) /// - [PEP 380](https://peps.python.org/pep-0380/)