mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
[pandas]: Fix issue on non pandas
dataframe in-place
usage (PD002) (#18963)
This commit is contained in:
parent
18efe2ab46
commit
1874d52eda
4 changed files with 25 additions and 6 deletions
|
@ -29,6 +29,21 @@ mod tests {
|
||||||
"#,
|
"#,
|
||||||
"PD002_fail"
|
"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(
|
#[test_case(
|
||||||
r"
|
r"
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
|
@ -9,6 +9,7 @@ use crate::Locator;
|
||||||
use crate::checkers::ast::Checker;
|
use crate::checkers::ast::Checker;
|
||||||
use crate::fix::edits::{Parentheses, remove_argument};
|
use crate::fix::edits::{Parentheses, remove_argument};
|
||||||
use crate::{Edit, Fix, FixAvailability, Violation};
|
use crate::{Edit, Fix, FixAvailability, Violation};
|
||||||
|
use ruff_python_semantic::Modules;
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
/// Checks for `inplace=True` usages in `pandas` function and method
|
/// Checks for `inplace=True` usages in `pandas` function and method
|
||||||
|
@ -52,12 +53,7 @@ impl Violation for PandasUseOfInplaceArgument {
|
||||||
|
|
||||||
/// PD002
|
/// PD002
|
||||||
pub(crate) fn inplace_argument(checker: &Checker, call: &ast::ExprCall) {
|
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().seen_module(Modules::PANDAS) {
|
||||||
if checker
|
|
||||||
.semantic()
|
|
||||||
.resolve_qualified_name(&call.func)
|
|
||||||
.is_some_and(|qualified_name| !matches!(qualified_name.segments(), ["pandas", ..]))
|
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_linter/src/rules/pandas_vet/mod.rs
|
||||||
|
---
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_linter/src/rules/pandas_vet/mod.rs
|
||||||
|
---
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue