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

This commit is contained in:
Micha Reiser 2024-11-29 22:32:51 +01:00 committed by GitHub
parent f3d8c023d3
commit 90487b8cbd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 25 additions and 0 deletions

View File

@ -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;
@ -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;

View File

@ -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;
@ -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;
};

View File

@ -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;
@ -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;
};

View File

@ -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
@ -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" {

View File

@ -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;
@ -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;
};