Handle right parens in join comma builder (#5711)

This commit is contained in:
Micha Reiser 2023-07-12 18:21:28 +02:00 committed by GitHub
parent f0aa6bd4d3
commit 653429bef9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 159 additions and 136 deletions

View file

@ -108,14 +108,14 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
//
// Unlike other expression parentheses, tuple parentheses are part of the range of the
// tuple itself.
elts if is_parenthesized(*range, elts, f.context().source())
_ if is_parenthesized(*range, elts, f.context().source())
&& self.parentheses != TupleParentheses::StripInsideForLoop =>
{
parenthesized("(", &ExprSequence::new(elts), ")").fmt(f)
parenthesized("(", &ExprSequence::new(item), ")").fmt(f)
}
elts => match self.parentheses {
TupleParentheses::Subscript => group(&ExprSequence::new(elts)).fmt(f),
_ => parenthesize_if_expands(&ExprSequence::new(elts)).fmt(f),
_ => match self.parentheses {
TupleParentheses::Subscript => group(&ExprSequence::new(item)).fmt(f),
_ => parenthesize_if_expands(&ExprSequence::new(item)).fmt(f),
},
}
}
@ -128,18 +128,20 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
#[derive(Debug)]
struct ExprSequence<'a> {
elts: &'a [Expr],
tuple: &'a ExprTuple,
}
impl<'a> ExprSequence<'a> {
const fn new(elts: &'a [Expr]) -> Self {
Self { elts }
const fn new(expr: &'a ExprTuple) -> Self {
Self { tuple: expr }
}
}
impl Format<PyFormatContext<'_>> for ExprSequence<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> FormatResult<()> {
f.join_comma_separated().nodes(self.elts.iter()).finish()
f.join_comma_separated(self.tuple.end())
.nodes(&self.tuple.elts)
.finish()
}
}