Skip to content

Commit

Permalink
Avoid false-positives for yields with non-identical references (#1665)
Browse files Browse the repository at this point in the history
Resolves #1663.
  • Loading branch information
charliermarsh committed Jan 5, 2023
1 parent 9f8ef17 commit 5eb03d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
10 changes: 10 additions & 0 deletions resources/test/fixtures/pyupgrade/UP028_1.py
Expand Up @@ -111,3 +111,13 @@ def f():
class C:
def __init__(self):
print(x)


def f():
for x in y:
yield x, x + 1


def f():
for x, y in z:
yield x, y, x + y
21 changes: 17 additions & 4 deletions src/pyupgrade/plugins/rewrite_yield_from.rs
Expand Up @@ -8,11 +8,25 @@ use crate::autofix::Fix;
use crate::checkers::ast::Checker;
use crate::registry::{Check, CheckKind};

/// Return `true` if the two expressions are equivalent, and consistent solely
/// of tuples and names.
fn is_same_expr(a: &Expr, b: &Expr) -> bool {
match (&a.node, &b.node) {
(ExprKind::Name { id: a, .. }, ExprKind::Name { id: b, .. }) => a == b,
(ExprKind::Tuple { elts: a, .. }, ExprKind::Tuple { elts: b, .. }) => {
a.len() == b.len() && a.iter().zip(b).all(|(a, b)| is_same_expr(a, b))
}
_ => false,
}
}

/// Collect all named variables in an expression consisting solely of tuples and
/// names.
fn collect_names(expr: &Expr) -> Vec<&str> {
match &expr.node {
ExprKind::Name { id, .. } => vec![id],
ExprKind::Tuple { elts, .. } => elts.iter().flat_map(collect_names).collect(),
_ => vec![],
_ => unreachable!("Expected: ExprKind::Name | ExprKind::Tuple"),
}
}

Expand Down Expand Up @@ -51,13 +65,12 @@ impl<'a> Visitor<'a> for YieldFromVisitor<'a> {
let body = &body[0];
if let StmtKind::Expr { value } = &body.node {
if let ExprKind::Yield { value: Some(value) } = &value.node {
let names = collect_names(target);
if names == collect_names(value) {
if is_same_expr(target, value) {
self.yields.push(YieldFrom {
stmt,
body,
iter,
names,
names: collect_names(target),
});
}
}
Expand Down

0 comments on commit 5eb03d5

Please sign in to comment.