Avoid fixing PD002 in a lambda expression (#4286)

This commit is contained in:
Dhruv Manilawala 2023-05-08 23:54:27 +05:30 committed by GitHub
parent d7a369e7dc
commit 3344d367f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 3 deletions

View file

@ -22,3 +22,5 @@ if True:
x.drop(["a"], axis=1, **kwargs, inplace=True) x.drop(["a"], axis=1, **kwargs, inplace=True)
x.drop(["a"], axis=1, inplace=True, **kwargs) x.drop(["a"], axis=1, inplace=True, **kwargs)
f(x.drop(["a"], axis=1, inplace=True)) f(x.drop(["a"], axis=1, inplace=True))
x.apply(lambda x: x.sort_values('a', inplace=True))

View file

@ -191,8 +191,9 @@ pub fn unittest_assertion(
if let Ok(unittest_assert) = UnittestAssert::try_from(attr.as_str()) { if let Ok(unittest_assert) = UnittestAssert::try_from(attr.as_str()) {
// We're converting an expression to a statement, so avoid applying the fix if // We're converting an expression to a statement, so avoid applying the fix if
// the assertion is part of a larger expression. // the assertion is part of a larger expression.
let fixable = checker.ctx.current_expr_parent().is_none() let fixable = matches!(checker.ctx.current_stmt().node, StmtKind::Expr { .. })
&& matches!(checker.ctx.current_stmt().node, StmtKind::Expr { .. }) && checker.ctx.current_expr_parent().is_none()
&& !checker.ctx.scope().kind.is_lambda()
&& !has_comments_in(expr.range(), checker.locator); && !has_comments_in(expr.range(), checker.locator);
let mut diagnostic = Diagnostic::new( let mut diagnostic = Diagnostic::new(
PytestUnittestAssertion { PytestUnittestAssertion {

View file

@ -77,9 +77,14 @@ pub fn inplace_argument(
// the star argument _doesn't_ contain an override). // the star argument _doesn't_ contain an override).
// 2. The call is part of a larger expression (we're converting an expression to a // 2. The call is part of a larger expression (we're converting an expression to a
// statement, and expressions can't contain statements). // statement, and expressions can't contain statements).
// 3. The call is in a lambda (we can't assign to a variable in a lambda). This
// should be unnecessary, as lambdas are expressions, and so (2) should apply,
// but we don't currently restore expression stacks when parsing deferred nodes,
// and so the parent is lost.
let fixable = !seen_star let fixable = !seen_star
&& matches!(checker.ctx.current_stmt().node, StmtKind::Expr { .. }) && matches!(checker.ctx.current_stmt().node, StmtKind::Expr { .. })
&& checker.ctx.current_expr_parent().is_none(); && checker.ctx.current_expr_parent().is_none()
&& !checker.ctx.scope().kind.is_lambda();
let mut diagnostic = let mut diagnostic =
Diagnostic::new(PandasUseOfInplaceArgument { fixable }, keyword.range()); Diagnostic::new(PandasUseOfInplaceArgument { fixable }, keyword.range());
if fixable && checker.patch(diagnostic.kind.rule()) { if fixable && checker.patch(diagnostic.kind.rule()) {

View file

@ -105,6 +105,7 @@ PD002.py:22:33: PD002 [*] `inplace=True` should be avoided; it has inconsistent
22 |+x = x.drop(["a"], axis=1, **kwargs) 22 |+x = x.drop(["a"], axis=1, **kwargs)
23 23 | x.drop(["a"], axis=1, inplace=True, **kwargs) 23 23 | x.drop(["a"], axis=1, inplace=True, **kwargs)
24 24 | f(x.drop(["a"], axis=1, inplace=True)) 24 24 | f(x.drop(["a"], axis=1, inplace=True))
25 25 |
PD002.py:23:23: PD002 `inplace=True` should be avoided; it has inconsistent behavior PD002.py:23:23: PD002 `inplace=True` should be avoided; it has inconsistent behavior
| |
@ -120,6 +121,16 @@ PD002.py:24:25: PD002 `inplace=True` should be avoided; it has inconsistent beha
25 | x.drop(["a"], axis=1, inplace=True, **kwargs) 25 | x.drop(["a"], axis=1, inplace=True, **kwargs)
26 | f(x.drop(["a"], axis=1, inplace=True)) 26 | f(x.drop(["a"], axis=1, inplace=True))
| ^^^^^^^^^^^^ PD002 | ^^^^^^^^^^^^ PD002
27 |
28 | x.apply(lambda x: x.sort_values('a', inplace=True))
|
PD002.py:26:38: PD002 `inplace=True` should be avoided; it has inconsistent behavior
|
26 | f(x.drop(["a"], axis=1, inplace=True))
27 |
28 | x.apply(lambda x: x.sort_values('a', inplace=True))
| ^^^^^^^^^^^^ PD002
| |