Respect magic trailing comma for set expression (#5782)

<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

This PR uses the `join_comma_separated` builder for formatting set
expressions
to ensure the formatting preserves magic commas, if the setting is
enabled.
<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan
See the fixed black tests

<!-- How was it tested? -->
This commit is contained in:
Micha Reiser 2023-07-15 18:40:38 +02:00 committed by GitHub
parent fa4855e6fe
commit df2efe81c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 319 deletions

View file

@ -1,9 +1,10 @@
use rustpython_parser::ast::{ExprSet, Ranged};
use ruff_python_ast::node::AnyNodeRef;
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::prelude::*;
use crate::FormatNodeRule;
use ruff_formatter::format_args;
use ruff_python_ast::node::AnyNodeRef;
use rustpython_parser::ast::ExprSet;
#[derive(Default)]
pub struct FormatExprSet;
@ -14,13 +15,13 @@ impl FormatNodeRule<ExprSet> for FormatExprSet {
// That would be a dict expression
assert!(!elts.is_empty());
// Avoid second mutable borrow of f
let joined = format_with(|f| {
f.join_with(format_args!(text(","), soft_line_break_or_space()))
.entries(elts.iter().formatted())
let joined = format_with(|f: &mut PyFormatter| {
f.join_comma_separated(item.end())
.nodes(elts.iter())
.finish()
});
parenthesized("{", &format_args![joined, if_group_breaks(&text(","))], "}").fmt(f)
parenthesized("{", &joined, "}").fmt(f)
}
}