Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix E713 and E714 false positives for multiple comparisons #4083

Merged
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
4 changes: 2 additions & 2 deletions crates/ruff/resources/test/fixtures/pycodestyle/E714.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#: E714
if not X.B is Y:
pass
#: E714

#: Okay
if not X is Y is not Z:
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this one is arguably a false negative, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How would you rewrite it?

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 guess you're right -- I have no idea 😂

pass

#: Okay
if not X is not Y:
pass

Expand Down
8 changes: 5 additions & 3 deletions crates/ruff/src/rules/pycodestyle/rules/not_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,15 @@ pub fn not_tests(
..
} = &operand.node
{
let should_fix = ops.len() == 1;
if !matches!(&ops[..], [Cmpop::In | Cmpop::Is]) {
return;
}
for op in ops.iter() {
match op {
Cmpop::In => {
if check_not_in {
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(
compare(left, &[Cmpop::NotIn], comparators, checker.stylist),
expr.location,
Expand All @@ -110,7 +112,7 @@ pub fn not_tests(
Cmpop::Is => {
if check_not_is {
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(
compare(left, &[Cmpop::IsNot], comparators, checker.stylist),
expr.location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ E714.py:5:8: E714 [*] Test for object identity should be `is not`
7 | if not X.B is Y:
| ^^^^^^^^ E714
8 | pass
9 | #: E714
|
= help: Convert to `is not`

Expand All @@ -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 X.B is not Y:
6 6 | pass
7 7 | #: E714
8 8 | if not X is Y is not Z:

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`
7 7 |
8 8 | #: Okay