mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:51:30 +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
|
@ -92,11 +92,23 @@ impl Entries {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
|
||||
pub(crate) enum TrailingComma {
|
||||
/// Add a trailing comma if the group breaks and there's more than one element (or if the last
|
||||
/// element has a trailing comma and the magical trailing comma option is enabled).
|
||||
#[default]
|
||||
MoreThanOne,
|
||||
/// Add a trailing comma if the group breaks (or if the last element has a trailing comma and
|
||||
/// the magical trailing comma option is enabled).
|
||||
OneOrMore,
|
||||
}
|
||||
|
||||
pub(crate) struct JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
|
||||
result: FormatResult<()>,
|
||||
fmt: &'fmt mut PyFormatter<'ast, 'buf>,
|
||||
entries: Entries,
|
||||
sequence_end: TextSize,
|
||||
trailing_comma: TrailingComma,
|
||||
}
|
||||
|
||||
impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
|
||||
|
@ -106,9 +118,19 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
|
|||
result: Ok(()),
|
||||
entries: Entries::None,
|
||||
sequence_end,
|
||||
trailing_comma: TrailingComma::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the trailing comma behavior for the builder. Trailing commas will only be inserted if
|
||||
/// the group breaks, and will _always_ be inserted if the last element has a trailing comma
|
||||
/// (and the magical trailing comma option is enabled). However, this setting dictates whether
|
||||
/// trailing commas are inserted for single element groups.
|
||||
pub(crate) fn with_trailing_comma(mut self, trailing_comma: TrailingComma) -> Self {
|
||||
self.trailing_comma = trailing_comma;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn entry<T>(
|
||||
&mut self,
|
||||
node: &T,
|
||||
|
@ -194,8 +216,11 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
|
|||
};
|
||||
|
||||
// If there is a single entry, only keep the magic trailing comma, don't add it if
|
||||
// it wasn't there. If there is more than one entry, always add it.
|
||||
if magic_trailing_comma || self.entries.is_more_than_one() {
|
||||
// it wasn't there -- unless the trailing comma behavior is set to one-or-more.
|
||||
if magic_trailing_comma
|
||||
|| self.trailing_comma == TrailingComma::OneOrMore
|
||||
|| self.entries.is_more_than_one()
|
||||
{
|
||||
if_group_breaks(&text(",")).fmt(self.fmt)?;
|
||||
}
|
||||
|
||||
|
|
|
@ -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