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

Skip panda rules if panda module hasn't been seen #14671

Merged
merged 1 commit into from
Nov 29, 2024
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
5 changes: 5 additions & 0 deletions crates/ruff_linter/src/rules/pandas_vet/rules/attr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::{self as ast, Expr};
use ruff_python_semantic::Modules;
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -44,6 +45,10 @@ impl Violation for PandasUseOfDotValues {

/// PD011
pub(crate) fn attr(checker: &mut Checker, attribute: &ast::ExprAttribute) {
if !checker.semantic().seen_module(Modules::PANDAS) {
return;
}

// Avoid, e.g., `x.values = y`.
if !attribute.ctx.is_load() {
return;
Expand Down
5 changes: 5 additions & 0 deletions crates/ruff_linter/src/rules/pandas_vet/rules/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ruff_python_ast::{self as ast, Expr};
use ruff_diagnostics::Violation;
use ruff_diagnostics::{Diagnostic, DiagnosticKind};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_semantic::Modules;
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -163,6 +164,10 @@ impl Violation for PandasUseOfDotStack {
}

pub(crate) fn call(checker: &mut Checker, func: &Expr) {
if !checker.semantic().seen_module(Modules::PANDAS) {
return;
}

let Expr::Attribute(ast::ExprAttribute { value, attr, .. }) = func else {
return;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ruff_diagnostics::Diagnostic;
use ruff_diagnostics::Violation;
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_ast::{self as ast, CmpOp, Expr, Int};
use ruff_python_semantic::Modules;
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -68,6 +69,10 @@ pub(crate) fn nunique_constant_series_check(
ops: &[CmpOp],
comparators: &[Expr],
) {
if !checker.semantic().seen_module(Modules::PANDAS) {
return;
}

let ([op], [right]) = (ops, comparators) else {
return;
};
Expand Down
5 changes: 5 additions & 0 deletions crates/ruff_linter/src/rules/pandas_vet/rules/pd_merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ruff_python_ast::{self as ast, Expr};
use crate::checkers::ast::Checker;
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_semantic::Modules;
use ruff_text_size::Ranged;

/// ## What it does
Expand Down Expand Up @@ -55,6 +56,10 @@ impl Violation for PandasUseOfPdMerge {

/// PD015
pub(crate) fn use_of_pd_merge(checker: &mut Checker, func: &Expr) {
if !checker.semantic().seen_module(Modules::PANDAS) {
return;
}

if let Expr::Attribute(ast::ExprAttribute { attr, value, .. }) = func {
if let Expr::Name(ast::ExprName { id, .. }) = value.as_ref() {
if id == "pd" && attr == "merge" {
Expand Down
5 changes: 5 additions & 0 deletions crates/ruff_linter/src/rules/pandas_vet/rules/subscript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ruff_python_ast::{self as ast, Expr};
use ruff_diagnostics::Violation;
use ruff_diagnostics::{Diagnostic, DiagnosticKind};
use ruff_macros::{derive_message_formats, ViolationMetadata};
use ruff_python_semantic::Modules;
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand Down Expand Up @@ -143,6 +144,10 @@ impl Violation for PandasUseOfDotIat {
}

pub(crate) fn subscript(checker: &mut Checker, value: &Expr, expr: &Expr) {
if !checker.semantic().seen_module(Modules::PANDAS) {
return;
}

let Expr::Attribute(ast::ExprAttribute { attr, value, .. }) = value else {
return;
};
Expand Down