mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 02:12:22 +00:00
Omit tuple parentheses in for statements except when absolutely necessary (#6765)
This commit is contained in:
parent
fec6fc2fab
commit
b52cc84df6
3 changed files with 96 additions and 3 deletions
|
@ -42,3 +42,31 @@ for x in (1, 2, 3):
|
||||||
|
|
||||||
for x in 1, 2, 3,:
|
for x in 1, 2, 3,:
|
||||||
pass
|
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
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ use ruff_text_size::TextRange;
|
||||||
use crate::builders::parenthesize_if_expands;
|
use crate::builders::parenthesize_if_expands;
|
||||||
use crate::comments::SourceComment;
|
use crate::comments::SourceComment;
|
||||||
use crate::expression::parentheses::{
|
use crate::expression::parentheses::{
|
||||||
empty_parenthesized, parenthesized, NeedsParentheses, OptionalParentheses,
|
empty_parenthesized, optional_parentheses, parenthesized, NeedsParentheses, OptionalParentheses,
|
||||||
};
|
};
|
||||||
use crate::prelude::*;
|
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
|
// 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
|
// Unlike other expression parentheses, tuple parentheses are part of the range of the
|
||||||
// tuple itself.
|
// tuple itself.
|
||||||
|
@ -159,7 +159,12 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
TupleParentheses::Preserve => group(&ExprSequence::new(item)).fmt(f),
|
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)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,34 @@ for x in (1, 2, 3):
|
||||||
|
|
||||||
for x in 1, 2, 3,:
|
for x in 1, 2, 3,:
|
||||||
pass
|
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
|
## Output
|
||||||
|
@ -100,6 +128,38 @@ for x in (
|
||||||
3,
|
3,
|
||||||
):
|
):
|
||||||
pass
|
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
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue