mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 21:43:52 +00:00
![]() ## Summary This PR adds support for parenthesized comments. A parenthesized comment is a comment that appears within a parenthesis, but not within the range of the expression enclosed by the parenthesis. For example, the comment here is a parenthesized comment: ```python if ( # comment True ): ... ``` The parentheses enclose the `True`, but the range of `True` doesn’t include the `# comment`. There are at least two problems associated with parenthesized comments: (1) associating the comment with the correct (i.e., enclosed) node; and (2) formatting the comment correctly, once it has been associated with the enclosed node. The solution proposed here for (1) is to search for parentheses between preceding and following node, and use open and close parentheses to break ties, rather than always assigning to the preceding node. For (2), we handle these special parenthesized comments in `FormatExpr`. The biggest risk with this approach is that we forget some codepath that force-disables parenthesization (by passing in `Parentheses::Never`). I've audited all usages of that enum and added additional handling + test coverage for such cases. Closes https://github.com/astral-sh/ruff/issues/6390. ## Test Plan `cargo test` with new cases. Before: | project | similarity index | |--------------|------------------| | build | 0.75623 | | cpython | 0.75472 | | django | 0.99804 | | transformers | 0.99618 | | typeshed | 0.74233 | | warehouse | 0.99601 | | zulip | 0.99727 | After: | project | similarity index | |--------------|------------------| | build | 0.75623 | | cpython | 0.75472 | | django | 0.99804 | | transformers | 0.99618 | | typeshed | 0.74237 | | warehouse | 0.99601 | | zulip | 0.99727 | |
||
---|---|---|
.. | ||
snapshots | ||
fixtures.rs |