Skip to content
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
4 changes: 2 additions & 2 deletions crates/ruff/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
pylint::rules::load_before_global_declaration(checker, id, expr);
}
}
Expr::Attribute(ast::ExprAttribute { attr, value, .. }) => {
Expr::Attribute(attribute) => {
// Ex) typing.List[...]
if checker.any_enabled(&[
Rule::FutureRewritableTypeAnnotation,
Expand Down Expand Up @@ -323,7 +323,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
if checker.enabled(Rule::CollectionsNamedTuple) {
flake8_pyi::rules::collections_named_tuple(checker, expr);
}
pandas_vet::rules::attr(checker, attr, value, expr);
pandas_vet::rules::attr(checker, attribute);
}
Expr::Call(
call @ ast::ExprCall {
Expand Down
9 changes: 8 additions & 1 deletion crates/ruff/src/rules/pandas_vet/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ pub(super) fn test_expression(expr: &Expr, semantic: &SemanticModel) -> Resoluti
.resolve_name(name)
.map_or(Resolution::IrrelevantBinding, |id| {
match &semantic.binding(id).kind {
BindingKind::Argument => {
// Avoid, e.g., `self.values`.
if matches!(name.id.as_str(), "self" | "cls") {
Resolution::IrrelevantBinding
} else {
Resolution::RelevantLocal
}
}
BindingKind::Annotation
| BindingKind::Argument
| BindingKind::Assignment
| BindingKind::NamedExprAssignment
| BindingKind::UnpackedAssignment
Expand Down
17 changes: 17 additions & 0 deletions crates/ruff/src/rules/pandas_vet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,23 @@ mod tests {
"#,
"PD011_pass_values_call"
)]
#[test_case(
r#"
import pandas as pd
x = pd.DataFrame()
x.values = 1
"#,
"PD011_pass_values_store"
)]
#[test_case(
r#"
class Class:
def __init__(self, values: str) -> None:
self.values = values
print(self.values)
"#,
"PD011_pass_values_instance"
)]
#[test_case(
r#"
import pandas as pd
Expand Down
16 changes: 11 additions & 5 deletions crates/ruff/src/rules/pandas_vet/rules/attr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_python_ast::{Expr, Ranged};
use ruff_python_ast::{self as ast, Expr, ExprContext, Ranged};

use ruff_diagnostics::Violation;
use ruff_diagnostics::{Diagnostic, DiagnosticKind};
Expand Down Expand Up @@ -44,8 +44,14 @@ impl Violation for PandasUseOfDotValues {
}
}

pub(crate) fn attr(checker: &mut Checker, attr: &str, value: &Expr, attr_expr: &Expr) {
let violation: DiagnosticKind = match attr {
pub(crate) fn attr(checker: &mut Checker, attribute: &ast::ExprAttribute) {
// Avoid, e.g., `x.values = y`.
if matches!(attribute.ctx, ExprContext::Store | ExprContext::Del) {
return;
}

let violation: DiagnosticKind = match attribute.attr.as_str() {
// PD011
"values" if checker.settings.rules.enabled(Rule::PandasUseOfDotValues) => {
PandasUseOfDotValues.into()
}
Expand All @@ -62,13 +68,13 @@ pub(crate) fn attr(checker: &mut Checker, attr: &str, value: &Expr, attr_expr: &
// Avoid flagging on non-DataFrames (e.g., `{"a": 1}.values`), and on irrelevant bindings
// (like imports).
if !matches!(
test_expression(value, checker.semantic()),
test_expression(attribute.value.as_ref(), checker.semantic()),
Resolution::RelevantLocal
) {
return;
}

checker
.diagnostics
.push(Diagnostic::new(violation, attr_expr.range()));
.push(Diagnostic::new(violation, attribute.range()));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff/src/rules/pandas_vet/mod.rs
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff/src/rules/pandas_vet/mod.rs
---