From 1874d52eda6e520c86b5cfced301153e26aee900 Mon Sep 17 00:00:00 2001 From: Jordy Williams <43758351+jordyjwilliams@users.noreply.github.com> Date: Fri, 27 Jun 2025 16:56:13 +1000 Subject: [PATCH] [pandas]: Fix issue on `non pandas` dataframe `in-place` usage (PD002) (#18963) --- crates/ruff_linter/src/rules/pandas_vet/mod.rs | 15 +++++++++++++++ .../rules/pandas_vet/rules/inplace_argument.rs | 8 ++------ ...__pandas_vet__tests__PD002_pass_no_import.snap | 4 ++++ ...les__pandas_vet__tests__PD002_pass_polars.snap | 4 ++++ 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 crates/ruff_linter/src/rules/pandas_vet/snapshots/ruff_linter__rules__pandas_vet__tests__PD002_pass_no_import.snap create mode 100644 crates/ruff_linter/src/rules/pandas_vet/snapshots/ruff_linter__rules__pandas_vet__tests__PD002_pass_polars.snap diff --git a/crates/ruff_linter/src/rules/pandas_vet/mod.rs b/crates/ruff_linter/src/rules/pandas_vet/mod.rs index e99f7e5ff9..982eedbde5 100644 --- a/crates/ruff_linter/src/rules/pandas_vet/mod.rs +++ b/crates/ruff_linter/src/rules/pandas_vet/mod.rs @@ -29,6 +29,21 @@ mod tests { "#, "PD002_fail" )] + #[test_case( + r" + import polars as pl + x = pl.DataFrame() + x.drop(['a'], inplace=True) + ", + "PD002_pass_polars" + )] + #[test_case( + r" + x = DataFrame() + x.drop(['a'], inplace=True) + ", + "PD002_pass_no_import" + )] #[test_case( r" import pandas as pd diff --git a/crates/ruff_linter/src/rules/pandas_vet/rules/inplace_argument.rs b/crates/ruff_linter/src/rules/pandas_vet/rules/inplace_argument.rs index 25dcccba7d..100a688b79 100644 --- a/crates/ruff_linter/src/rules/pandas_vet/rules/inplace_argument.rs +++ b/crates/ruff_linter/src/rules/pandas_vet/rules/inplace_argument.rs @@ -9,6 +9,7 @@ use crate::Locator; use crate::checkers::ast::Checker; use crate::fix::edits::{Parentheses, remove_argument}; use crate::{Edit, Fix, FixAvailability, Violation}; +use ruff_python_semantic::Modules; /// ## What it does /// Checks for `inplace=True` usages in `pandas` function and method @@ -52,12 +53,7 @@ impl Violation for PandasUseOfInplaceArgument { /// PD002 pub(crate) fn inplace_argument(checker: &Checker, call: &ast::ExprCall) { - // If the function was imported from another module, and it's _not_ Pandas, abort. - if checker - .semantic() - .resolve_qualified_name(&call.func) - .is_some_and(|qualified_name| !matches!(qualified_name.segments(), ["pandas", ..])) - { + if !checker.semantic().seen_module(Modules::PANDAS) { return; } diff --git a/crates/ruff_linter/src/rules/pandas_vet/snapshots/ruff_linter__rules__pandas_vet__tests__PD002_pass_no_import.snap b/crates/ruff_linter/src/rules/pandas_vet/snapshots/ruff_linter__rules__pandas_vet__tests__PD002_pass_no_import.snap new file mode 100644 index 0000000000..07173f8f1b --- /dev/null +++ b/crates/ruff_linter/src/rules/pandas_vet/snapshots/ruff_linter__rules__pandas_vet__tests__PD002_pass_no_import.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff_linter/src/rules/pandas_vet/mod.rs +--- + diff --git a/crates/ruff_linter/src/rules/pandas_vet/snapshots/ruff_linter__rules__pandas_vet__tests__PD002_pass_polars.snap b/crates/ruff_linter/src/rules/pandas_vet/snapshots/ruff_linter__rules__pandas_vet__tests__PD002_pass_polars.snap new file mode 100644 index 0000000000..07173f8f1b --- /dev/null +++ b/crates/ruff_linter/src/rules/pandas_vet/snapshots/ruff_linter__rules__pandas_vet__tests__PD002_pass_polars.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff_linter/src/rules/pandas_vet/mod.rs +--- +