ruff
9d641fa7 - [`pylint`] Include parentheses and multiple comparators in check for `boolean-chained-comparison (PLR1716)` (#14781)

Commit
1 year ago
[`pylint`] Include parentheses and multiple comparators in check for `boolean-chained-comparison (PLR1716)` (#14781) This PR introduces three changes to the diagnostic and fix behavior (still under preview) for [boolean-chained-comparison (PLR1716)](https://docs.astral.sh/ruff/rules/boolean-chained-comparison/#boolean-chained-comparison-plr1716). 1. We now offer a _fix_ in the case of parenthesized expressions like `(a < b) and b < c`. The fix will merge the chains of comparisons and then balance parentheses by _adding_ parentheses to one side of the expression. 2. We now trigger a diagnostic (and fix) in the case where some comparisons have multiple comparators like `a < b < c and c < d`. 3. When adjacent comparators are parenthesized, we prefer the left parenthesization and apply the replacement to the whole parenthesized range. So, for example, `a < (b) and ((b)) < c` becomes `a < (b) < c`. While these seem like somewhat disconnected changes, they are actually related. If we only offered (1), then we would see the following fix behavior: ```diff - (a < b) and b < c and ((c < d)) + (a < b < c) and ((c < d)) ``` This is because the fix which add parentheses to the first pair of comparisons overlaps with the fix that removes the `and` between the second two comparisons. So the latter fix is deferred. However, the latter fix does not get a second chance because, upon the next lint iteration, there is no violation of `PLR1716`. Upon adopting (2), however, both fixes occur by the time ruff completes several iterations and we get: ```diff - (a < b) and b < c and ((c < d)) + ((a < b < c < d)) ``` Finally, (3) fixes a previously unobserved bug wherein the autofix for `a < (b) and b < c` used to result in `a<(b<c` which gives a syntax error. It could in theory have been fixed in a separate PR, but seems to be on theme here. ---------- - Closes #13524 - (1), (2), and (3) are implemented in separate commits for ease of review and modification. - Technically a user can trigger an error in ruff (by reaching max iterations) if they have a humongous boolean chained comparison with differing parentheses levels.
Author
Parents
Loading