Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,7 @@
foo == False or foo == 0 # Different types, same hashed value

foo == 0.0 or foo == 0j # Different types, same hashed value

foo == "bar" or foo == "bar" # All members identical
Copy link
Contributor Author

@njhearp njhearp Jan 2, 2026

Choose a reason for hiding this comment

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

I think we should consider a rule to get this to reduce to foo == "bar". Opening a separate issue would probably be best.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is possibly tracked in #21690. Micha mentioned a more general unreachable rule there and on #21692.


foo == "bar" or foo == "bar" or foo == "buzz" # All but one members identical
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ pub(crate) fn repeated_equality_comparison(checker: &Checker, bool_op: &ast::Exp
continue;
}

let first_comparable = ComparableExpr::from(comparators[0]);
if comparators
.iter()
.skip(1)
.all(|&c| ComparableExpr::from(c) == first_comparable)
{
// Do not flag if all members are identical
continue;
}
// if we can determine that all the values are hashable, we can use a set
// TODO: improve with type inference
let all_hashable = comparators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ help: Merge multiple comparisons
73 + foo in {False, 0} # Different types, same hashed value
74 |
75 | foo == 0.0 or foo == 0j # Different types, same hashed value
76 |
note: This is an unsafe fix and may change runtime behavior

PLR1714 [*] Consider merging multiple comparisons: `foo in {0.0, 0j}`.
Expand All @@ -445,11 +446,32 @@ PLR1714 [*] Consider merging multiple comparisons: `foo in {0.0, 0j}`.
74 |
75 | foo == 0.0 or foo == 0j # Different types, same hashed value
| ^^^^^^^^^^^^^^^^^^^^^^^
76 |
77 | foo == "bar" or foo == "bar" # All members identical
|
help: Merge multiple comparisons
72 |
73 | foo == False or foo == 0 # Different types, same hashed value
74 |
- foo == 0.0 or foo == 0j # Different types, same hashed value
75 + foo in {0.0, 0j} # Different types, same hashed value
76 |
77 | foo == "bar" or foo == "bar" # All members identical
78 |
note: This is an unsafe fix and may change runtime behavior

PLR1714 [*] Consider merging multiple comparisons: `foo in {"bar", "bar", "buzz"}`.
--> repeated_equality_comparison.py:79:1
|
77 | foo == "bar" or foo == "bar" # All members identical
78 |
79 | foo == "bar" or foo == "bar" or foo == "buzz" # All but one members identical
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: Merge multiple comparisons
76 |
77 | foo == "bar" or foo == "bar" # All members identical
78 |
- foo == "bar" or foo == "bar" or foo == "buzz" # All but one members identical
79 + foo in {"bar", "bar", "buzz"} # All but one members identical
note: This is an unsafe fix and may change runtime behavior
Loading