Fix `E713` and `E714` false positives for multiple comparisons (#4083)

This commit is contained in:
Jonathan Plasse 2023-04-25 19:37:56 +02:00 committed by GitHub
parent 464a0ff483
commit 4df7bc0bcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 18 deletions

View File

@ -4,11 +4,11 @@ if not X is Y:
#: E714 #: E714
if not X.B is Y: if not X.B is Y:
pass pass
#: E714
#: Okay
if not X is Y is not Z: if not X is Y is not Z:
pass pass
#: Okay
if not X is not Y: if not X is not Y:
pass pass

View File

@ -91,13 +91,15 @@ pub fn not_tests(
.. ..
} = &operand.node } = &operand.node
{ {
let should_fix = ops.len() == 1; if !matches!(&ops[..], [Cmpop::In | Cmpop::Is]) {
return;
}
for op in ops.iter() { for op in ops.iter() {
match op { match op {
Cmpop::In => { Cmpop::In => {
if check_not_in { if check_not_in {
let mut diagnostic = Diagnostic::new(NotInTest, Range::from(operand)); let mut diagnostic = Diagnostic::new(NotInTest, Range::from(operand));
if checker.patch(diagnostic.kind.rule()) && should_fix { if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Edit::replacement( diagnostic.set_fix(Edit::replacement(
compare(left, &[Cmpop::NotIn], comparators, checker.stylist), compare(left, &[Cmpop::NotIn], comparators, checker.stylist),
expr.location, expr.location,
@ -110,7 +112,7 @@ pub fn not_tests(
Cmpop::Is => { Cmpop::Is => {
if check_not_is { if check_not_is {
let mut diagnostic = Diagnostic::new(NotIsTest, Range::from(operand)); let mut diagnostic = Diagnostic::new(NotIsTest, Range::from(operand));
if checker.patch(diagnostic.kind.rule()) && should_fix { if checker.patch(diagnostic.kind.rule()) {
diagnostic.set_fix(Edit::replacement( diagnostic.set_fix(Edit::replacement(
compare(left, &[Cmpop::IsNot], comparators, checker.stylist), compare(left, &[Cmpop::IsNot], comparators, checker.stylist),
expr.location, expr.location,

View File

@ -26,7 +26,6 @@ E714.py:5:8: E714 [*] Test for object identity should be `is not`
7 | if not X.B is Y: 7 | if not X.B is Y:
| ^^^^^^^^ E714 | ^^^^^^^^ E714
8 | pass 8 | pass
9 | #: E714
| |
= help: Convert to `is not` = help: Convert to `is not`
@ -37,17 +36,7 @@ E714.py:5:8: E714 [*] Test for object identity should be `is not`
5 |-if not X.B is Y: 5 |-if not X.B is Y:
5 |+if X.B is not Y: 5 |+if X.B is not Y:
6 6 | pass 6 6 | pass
7 7 | #: E714 7 7 |
8 8 | if not X is Y is not Z: 8 8 | #: Okay
E714.py:8:8: E714 [*] Test for object identity should be `is not`
|
8 | pass
9 | #: E714
10 | if not X is Y is not Z:
| ^^^^^^^^^^^^^^^ E714
11 | pass
|
= help: Convert to `is not`