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

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

Merged
merged 14 commits into from
Dec 9, 2024

Conversation

dylwil3
Copy link
Collaborator

@dylwil3 dylwil3 commented Dec 5, 2024

This PR introduces three changes to the diagnostic and fix behavior (still under preview) for 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:

- (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:

- (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.


@dylwil3 dylwil3 added rule Implementing or modifying a lint rule fixes Related to suggested fixes for violations preview Related to preview mode features labels Dec 5, 2024
Copy link
Contributor

github-actions bot commented Dec 5, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+0 -0 violations, +2 -0 fixes in 1 projects; 54 projects unchanged)

astropy/astropy (+0 -0 violations, +2 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

- astropy/cosmology/flrw/lambdacdm.py:267:15: PLR1716 Contains chained boolean comparison that can be simplified
+ astropy/cosmology/flrw/lambdacdm.py:267:15: PLR1716 [*] Contains chained boolean comparison that can be simplified

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLR1716 2 0 0 2 0

@dylwil3
Copy link
Collaborator Author

dylwil3 commented Dec 6, 2024

No obligation to review @zanieb , but figured I'd ping you since you wrote the first PR addressing this issue. Let me know if this fix is way off base from what you had in mind 😄

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

This is great. I suggest adding some more tests with comments because the comment range (or parentheses for that fact) isn't part of the expression range

@dylwil3 dylwil3 merged commit 9d641fa into astral-sh:main Dec 9, 2024
21 checks passed
@dylwil3 dylwil3 deleted the chained-parens branch December 9, 2024 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PLR1716 fix introduces a syntax error with unbalanced parentheses
2 participants