Omit tuple parentheses in for statements except when absolutely necessary (#6765)

This commit is contained in:
Micha Reiser 2023-08-22 12:18:59 +02:00 committed by GitHub
parent fec6fc2fab
commit b52cc84df6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 3 deletions

View file

@ -42,3 +42,31 @@ for x in (1, 2, 3):
for x in 1, 2, 3,:
pass
# Don't keep parentheses around right target if it can made fit by breaking sub expressions
for column_name, (
referenced_column_name,
referenced_table_name,
) in relations.items():
pass
for column_name, [
referenced_column_name,
referenced_table_name,
] in relations.items():
pass
for column_name, [
referenced_column_name,
referenced_table_name,
], in relations.items():
pass
for (
# leading comment
column_name, [
referenced_column_name,
referenced_table_name,
]) in relations.items():
pass

View file

@ -8,7 +8,7 @@ use ruff_text_size::TextRange;
use crate::builders::parenthesize_if_expands;
use crate::comments::SourceComment;
use crate::expression::parentheses::{
empty_parenthesized, parenthesized, NeedsParentheses, OptionalParentheses,
empty_parenthesized, optional_parentheses, parenthesized, NeedsParentheses, OptionalParentheses,
};
use crate::prelude::*;
@ -138,7 +138,7 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
}
},
// If the tuple has parentheses, we generally want to keep them. The exception are for
// loops, see `TupleParentheses::StripInsideForLoop` doc comment.
// loops, see `TupleParentheses::NeverPreserve` doc comment.
//
// Unlike other expression parentheses, tuple parentheses are part of the range of the
// tuple itself.
@ -159,7 +159,12 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
.finish()
}
TupleParentheses::Preserve => group(&ExprSequence::new(item)).fmt(f),
_ => parenthesize_if_expands(&ExprSequence::new(item)).fmt(f),
TupleParentheses::NeverPreserve => {
optional_parentheses(&ExprSequence::new(item)).fmt(f)
}
TupleParentheses::Default => {
parenthesize_if_expands(&ExprSequence::new(item)).fmt(f)
}
},
}
}

View file

@ -48,6 +48,34 @@ for x in (1, 2, 3):
for x in 1, 2, 3,:
pass
# Don't keep parentheses around right target if it can made fit by breaking sub expressions
for column_name, (
referenced_column_name,
referenced_table_name,
) in relations.items():
pass
for column_name, [
referenced_column_name,
referenced_table_name,
] in relations.items():
pass
for column_name, [
referenced_column_name,
referenced_table_name,
], in relations.items():
pass
for (
# leading comment
column_name, [
referenced_column_name,
referenced_table_name,
]) in relations.items():
pass
```
## Output
@ -100,6 +128,38 @@ for x in (
3,
):
pass
# Don't keep parentheses around right target if it can made fit by breaking sub expressions
for column_name, (
referenced_column_name,
referenced_table_name,
) in relations.items():
pass
for column_name, [
referenced_column_name,
referenced_table_name,
] in relations.items():
pass
for (
column_name,
[
referenced_column_name,
referenced_table_name,
],
) in relations.items():
pass
for (
# leading comment
column_name,
[
referenced_column_name,
referenced_table_name,
],
) in relations.items():
pass
```