[pylint] Ignore identical members (PLR1714) (#22220)

## Summary

This PR closes #21692. `PLR1714` will no longer flag if all members are
identical. I iterate through the equality comparisons and if they are
all equal the rule does not flag.

## Test Plan

Additional tests were added with identical members.
This commit is contained in:
Nikolas Hearp
2026-01-02 12:56:17 -05:00
committed by GitHub
parent 26230b1ed3
commit 0804030ee9
3 changed files with 38 additions and 0 deletions

View File

@@ -73,3 +73,7 @@ foo == 1 or foo == 1.0 # Different types, same hashed value
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
foo == "bar" or foo == "bar" or foo == "buzz" # All but one members identical

View File

@@ -140,6 +140,18 @@ pub(crate) fn repeated_equality_comparison(checker: &Checker, bool_op: &ast::Exp
continue;
}
if let Some((&first, rest)) = comparators.split_first() {
let first_comparable = ComparableExpr::from(first);
if rest
.iter()
.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

View File

@@ -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}`.
@@ -445,6 +446,8 @@ 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 |
@@ -452,4 +455,23 @@ help: Merge multiple comparisons
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