mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-25 11:30:58 +00:00
Insert trailing comma when function breaks with single argument (#8921)
## Summary Given: ```python def _example_function_xxxxxxx( variable: Optional[List[str]] ) -> List[example.ExampleConfig]: pass ``` We should be inserting a trailing comma after the argument (as long as it's a single-argument function). This was an inconsistency with Black, but also led to some internal inconsistencies, whereby we added the comma if the argument contained a trailing end-of-line comment, but not otherwise. Closes https://github.com/astral-sh/ruff/issues/8912. ## Test Plan `cargo test` Before: | project | similarity index | total files | changed files | |----------------|------------------:|------------------:|------------------:| | cpython | 0.75804 | 1799 | 1648 | | django | 0.99984 | 2772 | 34 | | home-assistant | 0.99963 | 10596 | 146 | | poetry | 0.99925 | 317 | 12 | | transformers | 0.99967 | 2657 | 322 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99980 | 3669 | 18 | | warehouse | 0.99977 | 654 | 13 | | zulip | 0.99970 | 1459 | 21 | After: | project | similarity index | total files | changed files | |----------------|------------------:|------------------:|------------------:| | cpython | 0.75804 | 1799 | 1648 | | django | 0.99984 | 2772 | 34 | | home-assistant | 0.99955 | 10596 | 213 | | poetry | 0.99917 | 317 | 13 | | transformers | 0.99967 | 2657 | 324 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99980 | 3669 | 18 | | warehouse | 0.99976 | 654 | 14 | | zulip | 0.99957 | 1459 | 36 |
This commit is contained in:
parent
019d9aebe9
commit
eaa310429f
7 changed files with 60 additions and 66 deletions
|
@ -252,6 +252,19 @@ impl FormatNodeRule<Parameters> for FormatParameters {
|
|||
let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);
|
||||
// No parameters, format any dangling comments between `()`
|
||||
write!(f, [empty_parenthesized("(", dangling, ")")])
|
||||
} else if num_parameters == 1 {
|
||||
// If we have a single argument, avoid the inner group, to ensure that we insert a
|
||||
// trailing comma if the outer group breaks.
|
||||
let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
token("("),
|
||||
dangling_open_parenthesis_comments(parenthesis_dangling),
|
||||
soft_block_indent(&format_inner),
|
||||
token(")")
|
||||
]
|
||||
)
|
||||
} else {
|
||||
// Intentionally avoid `parenthesized`, which groups the entire formatted contents.
|
||||
// We want parameters to be grouped alongside return types, one level up, so we
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue