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

Ignore PLW2901 when using typing cast #3891

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions crates/ruff/resources/test/fixtures/pylint/redefined_loop_name.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import typing
from typing import cast

# For -> for, variable reused
for i in []:
for i in []: # error
Expand Down Expand Up @@ -43,6 +46,9 @@

# For -> assignment
for i in []:
# ignore typing cast
i = cast(int, i)
i = typing.cast(int, i)
i = 5 # error

# For -> augmented assignment
Expand All @@ -53,6 +59,10 @@
for i in []:
i: int = 5 # error

# For -> annotated assignment without value
for i in []:
i: int # no error

# Async for -> for, variable reused
async for i in []:
for i in []: # error
Expand Down
56 changes: 54 additions & 2 deletions crates/ruff/src/rules/pylint/rules/redefined_loop_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ruff_python_ast::helpers::unparse_expr;
use ruff_python_ast::types::{Node, Range};
use ruff_python_ast::visitor;
use ruff_python_ast::visitor::Visitor;
use ruff_python_semantic::context::Context;

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

Expand Down Expand Up @@ -140,6 +141,7 @@ struct ExprWithInnerBindingKind<'a> {
}

struct InnerForWithAssignTargetsVisitor<'a> {
context: &'a Context<'a>,
dummy_variable_rgx: &'a Regex,
assignment_targets: Vec<ExprWithInnerBindingKind<'a>>,
}
Expand Down Expand Up @@ -174,7 +176,14 @@ where
);
}
// Assignment, augmented assignment, and annotated assignment.
StmtKind::Assign { targets, .. } => {
StmtKind::Assign { targets, value, .. } => {
// Check for single-target assignments which are of the
// form `x = cast(..., x)`.
if targets.first().map_or(false, |target| {
assignment_is_cast_expr(self.context, value, target)
}) {
return;
}
self.assignment_targets.extend(
assignment_targets_from_assign_targets(targets, self.dummy_variable_rgx).map(
|expr| ExprWithInnerBindingKind {
Expand All @@ -184,7 +193,20 @@ where
),
);
}
StmtKind::AugAssign { target, .. } | StmtKind::AnnAssign { target, .. } => {
StmtKind::AugAssign { target, .. } => {
self.assignment_targets.extend(
assignment_targets_from_expr(target, self.dummy_variable_rgx).map(|expr| {
ExprWithInnerBindingKind {
expr,
binding_kind: InnerBindingKind::Assignment,
}
}),
);
}
StmtKind::AnnAssign { target, value, .. } => {
if value.is_none() {
return;
}
self.assignment_targets.extend(
assignment_targets_from_expr(target, self.dummy_variable_rgx).map(|expr| {
ExprWithInnerBindingKind {
Expand All @@ -209,6 +231,34 @@ where
}
}

/// Checks whether the given assignment value is a `typing.cast` expression
/// and that the target name is the same as the argument name.
///
/// Example:
/// ```python
/// from typing import cast
///
/// x = cast(int, x)
/// ```
fn assignment_is_cast_expr(context: &Context, value: &Expr, target: &Expr) -> bool {
let ExprKind::Call { func, args, .. } = &value.node else {
return false;
};
let ExprKind::Name { id: target_id, .. } = &target.node else {
return false;
};
if args.len() != 2 {
return false;
}
let ExprKind::Name { id: arg_id, .. } = &args[1].node else {
return false;
};
if arg_id != target_id {
return false;
}
context.match_typing_expr(func, "cast")
}

fn assignment_targets_from_expr<'a, U>(
expr: &'a Expr<U>,
dummy_variable_rgx: &'a Regex,
Expand Down Expand Up @@ -299,6 +349,7 @@ pub fn redefined_loop_name<'a, 'b>(checker: &'a mut Checker<'b>, node: &Node<'b>
})
.collect();
let mut visitor = InnerForWithAssignTargetsVisitor {
context: &checker.ctx,
dummy_variable_rgx: &checker.settings.dummy_variable_rgx,
assignment_targets: vec![],
};
Expand All @@ -317,6 +368,7 @@ pub fn redefined_loop_name<'a, 'b>(checker: &'a mut Checker<'b>, node: &Node<'b>
})
.collect();
let mut visitor = InnerForWithAssignTargetsVisitor {
context: &checker.ctx,
dummy_variable_rgx: &checker.settings.dummy_variable_rgx,
assignment_targets: vec![],
};
Expand Down
Loading