Update TupleParentheses usage (#5810)

This commit is contained in:
Chris Pryer 2023-07-24 10:44:36 -04:00 committed by GitHub
parent 8a7dcb794b
commit 8eadacda33
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 88 additions and 30 deletions

View file

@ -49,7 +49,7 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
let result = if let Expr::Tuple(tuple) = slice.as_ref() {
tuple
.format()
.with_options(TupleParentheses::Subscript)
.with_options(TupleParentheses::Preserve)
.fmt(f)
} else {
slice.format().fmt(f)

View file

@ -6,29 +6,53 @@ use ruff_formatter::{format_args, write, FormatRuleWithOptions};
use ruff_python_ast::node::AnyNodeRef;
use crate::builders::{empty_parenthesized_with_dangling_comments, parenthesize_if_expands};
use crate::expression::parentheses::{
parenthesized, NeedsParentheses, OptionalParentheses, Parentheses,
};
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::prelude::*;
#[derive(Eq, PartialEq, Debug, Default)]
pub enum TupleParentheses {
/// Black omits parentheses for tuples inside of comprehensions.
Comprehension,
/// Effectively `None` in `Option<Parentheses>`
/// By default tuples with a single element will include parentheses. Tuples with multiple elements
/// will parenthesize if the expression expands. This means that tuples will often *preserve*
/// their parentheses, but this differs from `Preserve` in that we may also *introduce*
/// parentheses as well.
#[default]
Default,
/// Effectively `Some(Parentheses)` in `Option<Parentheses>`
Expr(Parentheses),
/// Black omits parentheses for tuples inside of subscripts except if the tuple is parenthesized
/// in the source code.
Subscript,
/// Handle the special case where we remove parentheses even if they were initially present
/// Handle special cases where parentheses are to be preserved.
///
/// Normally, black keeps parentheses, but in the case of loops it formats
/// Black omits parentheses for tuples inside subscripts except if the tuple is already
/// parenthesized in the source code.
/// ```python
/// x[a, :]
/// x[a, b:]
/// x[(a, b):]
/// ```
Preserve,
/// Handle the special cases where we don't include parentheses at all.
///
///
/// Black never formats tuple targets of for loops with parentheses if inside a comprehension.
/// For example, tuple targets will always be formatted on the same line, except when an element supports
/// line-breaking in an un-parenthesized context.
/// ```python
/// # Input
/// {k: v for x, (k, v) in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension}
///
/// # Black
/// {
/// k: v
/// for x, (
/// k,
/// v,
/// ) in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension
/// }
/// ```
Never,
/// Handle the special cases where we don't include parentheses if they are not required.
///
/// Normally, black keeps parentheses, but in the case of for loops it formats
/// ```python
/// for (a, b) in x:
/// pass
@ -38,9 +62,24 @@ pub enum TupleParentheses {
/// for a, b in x:
/// pass
/// ```
/// Black still does use parentheses in this position if the group breaks or magic trailing
/// Black still does use parentheses in these positions if the group breaks or magic trailing
/// comma is used.
StripInsideForLoop,
///
/// Additional examples:
/// ```python
/// for (a,) in []:
/// pass
/// for a, b in []:
/// pass
/// for a, b in []: # Strips parentheses
/// pass
/// for (
/// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
/// b,
/// ) in []:
/// pass
/// ```
NeverPreserve,
}
#[derive(Default)]
@ -86,7 +125,7 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
.fmt(f);
}
[single] => match self.parentheses {
TupleParentheses::Subscript
TupleParentheses::Preserve
if !is_parenthesized(*range, elts, f.context().source()) =>
{
write!(f, [single.format(), text(",")])
@ -103,19 +142,19 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
// Unlike other expression parentheses, tuple parentheses are part of the range of the
// tuple itself.
_ if is_parenthesized(*range, elts, f.context().source())
&& self.parentheses != TupleParentheses::StripInsideForLoop =>
&& self.parentheses != TupleParentheses::NeverPreserve =>
{
parenthesized("(", &ExprSequence::new(item), ")").fmt(f)
}
_ => match self.parentheses {
TupleParentheses::Comprehension => {
TupleParentheses::Never => {
let separator =
format_with(|f| group(&format_args![text(","), space()]).fmt(f));
f.join_with(separator)
.entries(elts.iter().formatted())
.finish()
}
TupleParentheses::Subscript => group(&ExprSequence::new(item)).fmt(f),
TupleParentheses::Preserve => group(&ExprSequence::new(item)).fmt(f),
_ => parenthesize_if_expands(&ExprSequence::new(item)).fmt(f),
},
}

View file

@ -9,7 +9,6 @@ use ruff_python_ast::visitor::preorder::{walk_expr, PreorderVisitor};
use crate::builders::parenthesize_if_expands;
use crate::context::NodeLevel;
use crate::expression::expr_tuple::TupleParentheses;
use crate::expression::parentheses::{
is_expression_parenthesized, optional_parentheses, parenthesized, NeedsParentheses,
OptionalParentheses, Parentheses, Parenthesize,
@ -91,10 +90,7 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
Expr::Starred(expr) => expr.format().fmt(f),
Expr::Name(expr) => expr.format().fmt(f),
Expr::List(expr) => expr.format().fmt(f),
Expr::Tuple(expr) => expr
.format()
.with_options(TupleParentheses::Expr(parentheses))
.fmt(f),
Expr::Tuple(expr) => expr.format().fmt(f),
Expr::Slice(expr) => expr.format().fmt(f),
});

View file

@ -113,7 +113,7 @@ impl Format<PyFormatContext<'_>> for ExprTupleWithoutParentheses<'_> {
match self.0 {
Expr::Tuple(expr_tuple) => expr_tuple
.format()
.with_options(TupleParentheses::Comprehension)
.with_options(TupleParentheses::Never)
.fmt(f),
other => other.format().fmt(f),
}

View file

@ -17,7 +17,7 @@ impl Format<PyFormatContext<'_>> for ExprTupleWithoutParentheses<'_> {
match self.0 {
Expr::Tuple(expr_tuple) => expr_tuple
.format()
.with_options(TupleParentheses::StripInsideForLoop)
.with_options(TupleParentheses::NeverPreserve)
.fmt(f),
other => maybe_parenthesize_expression(other, self.0, Parenthesize::IfBreaks).fmt(f),
}