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
15 changes: 15 additions & 0 deletions crates/ruff_linter/src/rules/pandas_vet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Member

@MichaReiser MichaReiser Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is now outdated.

Can you tell me more why we remove this check entirely?

CC: @dhruvmanila

Copy link
Contributor Author

@jordyjwilliams jordyjwilliams Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is now outdated.

Can you tell me more why we remove this check entirely?

CC: @dhruvmanila

Fair call, I will remove the comment (7348b1c). I removed the check for the following reasons:

  • To align better with the code styling and implementation from: Skip panda rules if panda module hasn't been seen #14671
  • As noted in that PR yes this may give false negatives. But this is preferred to false positives (less end-user gripe)
  • I believe the check is now unnecessary given the additional tests added
    • On current main these would fail, eg they would trigger the pandas linter issue.
    • With these changes, they now pass.
    • All existing tests pass, thus no existing functionality (that I'm aware of) will change.

If there's something I'm missing here please let me know. Happy to add back in the check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EG with these changes we are using the cheker however in a different way (as introduced in #14671.

!checker.semantic().seen_module(Modules::PANDAS) we will now skip the check pandas has not been seen (used) within the offending code.

Thus; this will allow for less false positives with similar codes on in-place operations. eg pl.dataframe

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaReiser Isn't this change in line with what you did in #14671? In other words, why wasn't that change applied to this rule?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing check would only work when the method is used directly like pandas.DataFrame.sort_values and not when used as df = pandas.DataFrame(); df.sort_values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing check would only work when the method is used directly like pandas.DataFrame.sort_values and not when used as df = pandas.DataFrame(); df.sort_values.

Yeah, I think that's the main difference. This PR removes some false positives (when panda isn't enabled at all) but introduces some new false positives (when using panda but the method doesn't come from pandas). This is different from my PRs where I only added the check.

However, I think this is still fine because it is in line with all other pandas rules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaReiser good to merge from your point of view here? What I guess I was going for here was standardization.

And also basically looking to commit to this repo for the first time. @dhruvmanila's suggestion is what I was implementing here for robustness, completeness and to close out #6432 (comment)

In my experience people who would use pandas.DataFrame would typically have these as variables and run my_df.sort_values(in_place=True) or so... Thus I figured it's better to pick up on these cases (when we know pandas has been seen)... And rather have all pd methods work the same.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it's fine. Thank you for working on this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nws, thanks for the approval and merge.

Anything further required from my side after merge?

I have read the contributing guide just trying to make sure there's nothing I've missed here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, all good from your side. This will go out in the next release

.semantic()
.resolve_qualified_name(&call.func)
.is_some_and(|qualified_name| !matches!(qualified_name.segments(), ["pandas", ..]))
{
if !checker.semantic().seen_module(Modules::PANDAS) {
return;
}

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

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

Loading