mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:48:32 +00:00
Fix instability in trailing clause body comments (#7556)
## Summary When we format the trailing comments on a clause body, we check if there are any newlines after the last statement; if not, we insert one. This logic didn't take into account that the last statement could itself have trailing comments, as in: ```python if True: pass # comment else: pass ``` We were thus inserting a newline after the comment, like: ```python if True: pass # comment else: pass ``` In the context of function definitions, this led to an instability, since we insert a newline _after_ a function, which would in turn lead to the bug above appearing in the second formatting pass. Closes https://github.com/astral-sh/ruff/issues/7465. ## Test Plan `cargo test` Small improvement in `transformers`, but no regressions. Before: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1631 | | django | 0.99983 | 2760 | 36 | | transformers | 0.99956 | 2587 | 404 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99983 | 3496 | 18 | | warehouse | 0.99967 | 648 | 15 | | zulip | 0.99972 | 1437 | 21 | After: | project | similarity index | total files | changed files | |--------------|------------------:|------------------:|------------------:| | cpython | 0.76083 | 1789 | 1631 | | django | 0.99983 | 2760 | 36 | | **transformers** | **0.99957** | **2587** | **402** | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99983 | 3496 | 18 | | warehouse | 0.99967 | 648 | 15 | | zulip | 0.99972 | 1437 | 21 |
This commit is contained in:
parent
ab643017f9
commit
124d95d246
5 changed files with 138 additions and 2 deletions
|
@ -376,3 +376,11 @@ def f( # first
|
|||
def this_is_unusual() -> (please := no): ...
|
||||
|
||||
def this_is_unusual(x) -> (please := no): ...
|
||||
|
||||
# Regression test for: https://github.com/astral-sh/ruff/issues/7465
|
||||
try:
|
||||
def test():
|
||||
pass
|
||||
#comment
|
||||
except ImportError:
|
||||
pass
|
||||
|
|
|
@ -104,6 +104,40 @@ if True: print("a") # 1
|
|||
elif True: print("b") # 2
|
||||
else: print("c") # 3
|
||||
|
||||
# Regression test for: https://github.com/astral-sh/ruff/issues/7465
|
||||
if True:
|
||||
pass
|
||||
|
||||
# comment
|
||||
else:
|
||||
pass
|
||||
|
||||
if True:
|
||||
pass
|
||||
|
||||
# comment
|
||||
|
||||
else:
|
||||
pass
|
||||
|
||||
if True:
|
||||
pass
|
||||
# comment
|
||||
|
||||
else:
|
||||
pass
|
||||
|
||||
if True:
|
||||
pass
|
||||
# comment
|
||||
else:
|
||||
pass
|
||||
|
||||
if True:
|
||||
pass # comment
|
||||
else:
|
||||
pass
|
||||
|
||||
# Regression test for https://github.com/astral-sh/ruff/issues/5337
|
||||
if parent_body:
|
||||
if current_body:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue