mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 04:55:09 +00:00
Add trailing comma for single-element import-from groups (#6583)
## Summary Unlike other statements, Black always adds a trailing comma if an import-from statement breaks with a single import member. I believe this is for compatibility with isort -- see09f5ee3a19
, https://github.com/psf/black/issues/127, or66648c528a/src/black/linegen.py (L1452)
for the current version. ## Test Plan `cargo test`, notice that a big chunk of the compatibility suite is removed. Before: | project | similarity index | |--------------|------------------| | cpython | 0.75472 | | django | 0.99804 | | transformers | 0.99618 | | twine | 0.99876 | | typeshed | 0.74233 | | warehouse | 0.99601 | | zulip | 0.99727 | After: | project | similarity index | |--------------|------------------| | cpython | 0.75472 | | django | 0.99804 | | transformers | 0.99618 | | twine | 0.99876 | | typeshed | 0.74260 | | warehouse | 0.99601 | | zulip | 0.99727 |
This commit is contained in:
parent
84d178a219
commit
b1c4c7be69
6 changed files with 33 additions and 231 deletions
|
@ -3,7 +3,7 @@ use ruff_formatter::{write, Buffer, Format, FormatResult};
|
|||
use ruff_python_ast::node::AstNode;
|
||||
use ruff_python_ast::{Ranged, StmtImportFrom};
|
||||
|
||||
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions};
|
||||
use crate::builders::{parenthesize_if_expands, PyFormatterExtensions, TrailingComma};
|
||||
use crate::expression::parentheses::parenthesized;
|
||||
use crate::{AsFormat, FormatNodeRule, PyFormatter};
|
||||
|
||||
|
@ -45,6 +45,7 @@ impl FormatNodeRule<StmtImportFrom> for FormatStmtImportFrom {
|
|||
|
||||
let names = format_with(|f| {
|
||||
f.join_comma_separated(item.end())
|
||||
.with_trailing_comma(TrailingComma::OneOrMore)
|
||||
.entries(names.iter().map(|name| (name, name.format())))
|
||||
.finish()
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue