mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:48:32 +00:00
Avoid C1901 violations within subscripts (#3517)
This commit is contained in:
parent
73df267635
commit
373a77e8c2
3 changed files with 17 additions and 0 deletions
|
@ -17,3 +17,7 @@ def errors():
|
|||
def ok():
|
||||
if x and not y:
|
||||
print("x is not an empty string, but y is an empty string")
|
||||
|
||||
|
||||
data.loc[data["a"] != ""]
|
||||
data.loc[data["a"] != "", :]
|
||||
|
|
|
@ -74,6 +74,14 @@ pub fn compare_to_empty_string(
|
|||
ops: &[Cmpop],
|
||||
comparators: &[Expr],
|
||||
) {
|
||||
// Omit string comparison rules within subscripts. This is mostly commonly used within
|
||||
// DataFrame and np.ndarray indexing.
|
||||
for parent in checker.ctx.expr_ancestors() {
|
||||
if matches!(parent.node, ExprKind::Subscript { .. }) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let mut first = true;
|
||||
for ((lhs, rhs), op) in std::iter::once(left)
|
||||
.chain(comparators.iter())
|
||||
|
|
|
@ -251,6 +251,11 @@ impl<'a> Context<'a> {
|
|||
self.exprs.iter().rev().nth(2)
|
||||
}
|
||||
|
||||
/// Return an [`Iterator`] over the current `Expr` parents.
|
||||
pub fn expr_ancestors(&self) -> impl Iterator<Item = &RefEquality<'a, Expr>> {
|
||||
self.exprs.iter().rev().skip(1)
|
||||
}
|
||||
|
||||
/// Return the `Stmt` that immediately follows the current `Stmt`, if any.
|
||||
pub fn current_sibling_stmt(&self) -> Option<&'a Stmt> {
|
||||
self.body.get(self.body_index + 1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue