ruff/crates
Dylan 9d641fa714
[`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.
2024-12-08 22:58:45 -06:00
..
red_knot Possible fix for flaky file watching test (#14543) 2024-12-03 08:22:42 +01:00
red_knot_python_semantic Understand `type[A | B]` special form in annotations (#14830) 2024-12-07 17:34:50 +00:00
red_knot_server Upgrade Rust toolchain to 1.83 (#14677) 2024-11-29 12:05:05 +00:00
red_knot_test [red-knot] Support for TOML configs in Markdown tests (#14785) 2024-12-06 10:22:08 +01:00
red_knot_vendored Sync vendored typeshed stubs (#14696) 2024-12-01 01:38:31 +00:00
red_knot_wasm Add tests for python version compatibility (#14430) 2024-11-18 12:26:55 +00:00
red_knot_workspace Possible fix for flaky file watching test (#14543) 2024-12-03 08:22:42 +01:00
ruff Fix infinite watch loop by ignoring 'uninteresting' watch events (#14809) 2024-12-06 08:50:29 +00:00
ruff_benchmark Fix Red Knot benchmarks on Windows (#14400) 2024-11-17 16:21:09 +00:00
ruff_cache Fix cache key collisions for paths with separators (#12159) 2024-07-03 07:36:46 -05:00
ruff_db Upgrade Rust toolchain to 1.83 (#14677) 2024-11-29 12:05:05 +00:00
ruff_dev Alternate quotes for strings inside f-strings in preview (#13860) 2024-10-23 07:57:53 +02:00
ruff_diagnostics Upgrade Rust toolchain to 1.83 (#14677) 2024-11-29 12:05:05 +00:00
ruff_formatter Fix fstring formatting removing overlong implicit concatenated string in expression part (#14811) 2024-12-06 13:01:04 +01:00
ruff_graph Bump MSRV to Rust 1.80 (#13826) 2024-10-20 10:55:36 +02:00
ruff_index [red-knot] small efficiency improvements and bugfixes to use-def map building (#12373) 2024-07-18 09:24:58 -07:00
ruff_linter [`pylint`] Include parentheses and multiple comparators in check for `boolean-chained-comparison (PLR1716)` (#14781) 2024-12-08 22:58:45 -06:00
ruff_macros Use a derive macro for Violations (#14557) 2024-11-27 09:41:40 +00:00
ruff_notebook Bump MSRV to Rust 1.80 (#13826) 2024-10-20 10:55:36 +02:00
ruff_python_ast [`ruff`] Unnecessary cast to `int` (`RUF046`) (#14697) 2024-12-05 10:30:06 +01:00
ruff_python_ast_integration_tests Update insta snapshots (#14366) 2024-11-15 19:31:15 +01:00
ruff_python_codegen Extract `LineIndex` independent methods from `Locator` (#13938) 2024-10-28 07:53:41 +00:00
ruff_python_formatter Fix fstring formatting removing overlong implicit concatenated string in expression part (#14811) 2024-12-06 13:01:04 +01:00
ruff_python_index Extract `LineIndex` independent methods from `Locator` (#13938) 2024-10-28 07:53:41 +00:00
ruff_python_literal Upgrade Rust toolchain to 1.83 (#14677) 2024-11-29 12:05:05 +00:00
ruff_python_parser Upgrade Rust toolchain to 1.83 (#14677) 2024-11-29 12:05:05 +00:00
ruff_python_resolver Update insta snapshots (#14366) 2024-11-15 19:31:15 +01:00
ruff_python_semantic Fix `PLW1508` false positive for default string created via a mult operation (#14841) 2024-12-08 18:25:47 +00:00
ruff_python_stdlib [`ruff`] Implemented `used-dummy-variable` (`RUF052`) (#14611) 2024-12-03 08:36:16 +01:00
ruff_python_trivia Extract `LineIndex` independent methods from `Locator` (#13938) 2024-10-28 07:53:41 +00:00
ruff_python_trivia_integration_tests Update insta snapshots (#14366) 2024-11-15 19:31:15 +01:00
ruff_server Upgrade Rust toolchain to 1.83 (#14677) 2024-11-29 12:05:05 +00:00
ruff_source_file Extract `LineIndex` independent methods from `Locator` (#13938) 2024-10-28 07:53:41 +00:00
ruff_text_size Extract `LineIndex` independent methods from `Locator` (#13938) 2024-10-28 07:53:41 +00:00
ruff_wasm Bump version to 0.8.2 (#14789) 2024-12-05 18:06:35 +05:30
ruff_workspace [`flake8-import-conventions`] Improve syntax check for aliases supplied in configuration for `unconventional-import-alias (ICN001)` (#14745) 2024-12-02 22:41:47 -06:00