mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:25:17 +00:00
Preserve yield parentheses (#6766)
This commit is contained in:
parent
b52cc84df6
commit
ccac9681e1
6 changed files with 121 additions and 16 deletions
|
@ -65,3 +65,35 @@ def foo():
|
||||||
test, # comment 4
|
test, # comment 4
|
||||||
1
|
1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
yield ("Cache key will cause errors if used with memcached: %r " "(longer than %s)" % (
|
||||||
|
key,
|
||||||
|
MEMCACHE_MAX_KEY_LENGTH,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
yield "Cache key will cause errors if used with memcached: %r " "(longer than %s)" % (
|
||||||
|
key,
|
||||||
|
MEMCACHE_MAX_KEY_LENGTH,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
yield ("Unnecessary")
|
||||||
|
|
||||||
|
|
||||||
|
yield (
|
||||||
|
"# * Make sure each ForeignKey and OneToOneField has `on_delete` set "
|
||||||
|
"to the desired behavior"
|
||||||
|
)
|
||||||
|
yield (
|
||||||
|
"# * Remove `managed = False` lines if you wish to allow "
|
||||||
|
"Django to create, modify, and delete the table"
|
||||||
|
)
|
||||||
|
yield (
|
||||||
|
"# Feel free to rename the models, but don't rename db_table values or "
|
||||||
|
"field names."
|
||||||
|
)
|
||||||
|
|
||||||
|
yield "# * Make sure each ForeignKey and OneToOneField has `on_delete` set " "to the desired behavior"
|
||||||
|
yield "# * Remove `managed = False` lines if you wish to allow " "Django to create, modify, and delete the table"
|
||||||
|
yield "# Feel free to rename the models, but don't rename db_table values or " "field names."
|
||||||
|
|
|
@ -20,7 +20,7 @@ impl FormatNodeRule<ExprAwait> for FormatExprAwait {
|
||||||
[
|
[
|
||||||
text("await"),
|
text("await"),
|
||||||
space(),
|
space(),
|
||||||
maybe_parenthesize_expression(value, item, Parenthesize::IfRequired)
|
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,7 +90,7 @@ impl Format<PyFormatContext<'_>> for AnyExpressionYield<'_> {
|
||||||
[
|
[
|
||||||
text(keyword),
|
text(keyword),
|
||||||
space(),
|
space(),
|
||||||
maybe_parenthesize_expression(val, self, Parenthesize::IfRequired)
|
maybe_parenthesize_expression(val, self, Parenthesize::Optional)
|
||||||
]
|
]
|
||||||
)?;
|
)?;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -190,16 +190,13 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
|
||||||
return expression.format().with_options(Parentheses::Always).fmt(f);
|
return expression.format().with_options(Parentheses::Always).fmt(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
let needs_parentheses = expression.needs_parentheses(*parent, f.context());
|
let needs_parentheses = match expression.needs_parentheses(*parent, f.context()) {
|
||||||
let needs_parentheses = match parenthesize {
|
OptionalParentheses::Always => OptionalParentheses::Always,
|
||||||
Parenthesize::IfRequired => match needs_parentheses {
|
// The reason to add parentheses is to avoid a syntax error when breaking an expression over multiple lines.
|
||||||
OptionalParentheses::Always => OptionalParentheses::Always,
|
// Therefore, it is unnecessary to add an additional pair of parentheses if an outer expression
|
||||||
_ if f.context().node_level().is_parenthesized() => OptionalParentheses::Never,
|
// is parenthesized.
|
||||||
needs_parentheses => needs_parentheses,
|
_ if f.context().node_level().is_parenthesized() => OptionalParentheses::Never,
|
||||||
},
|
needs_parentheses => needs_parentheses,
|
||||||
Parenthesize::Optional
|
|
||||||
| Parenthesize::IfBreaks
|
|
||||||
| Parenthesize::IfBreaksOrIfRequired => needs_parentheses,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
match needs_parentheses {
|
match needs_parentheses {
|
||||||
|
|
|
@ -41,9 +41,8 @@ pub(crate) enum Parenthesize {
|
||||||
/// Parenthesizes the expression only if it doesn't fit on a line.
|
/// Parenthesizes the expression only if it doesn't fit on a line.
|
||||||
IfBreaks,
|
IfBreaks,
|
||||||
|
|
||||||
/// Only adds parentheses if absolutely necessary:
|
/// Only adds parentheses if the expression has leading or trailing comments.
|
||||||
/// * The expression is not enclosed by another parenthesized expression and it expands over multiple lines
|
/// Adding parentheses is desired to prevent the comments from wandering.
|
||||||
/// * The expression has leading or trailing comments. Adding parentheses is desired to prevent the comments from wandering.
|
|
||||||
IfRequired,
|
IfRequired,
|
||||||
|
|
||||||
/// Parenthesizes the expression if the group doesn't fit on a line (e.g., even name expressions are parenthesized), or if
|
/// Parenthesizes the expression if the group doesn't fit on a line (e.g., even name expressions are parenthesized), or if
|
||||||
|
|
|
@ -71,6 +71,38 @@ def foo():
|
||||||
test, # comment 4
|
test, # comment 4
|
||||||
1
|
1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
yield ("Cache key will cause errors if used with memcached: %r " "(longer than %s)" % (
|
||||||
|
key,
|
||||||
|
MEMCACHE_MAX_KEY_LENGTH,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
yield "Cache key will cause errors if used with memcached: %r " "(longer than %s)" % (
|
||||||
|
key,
|
||||||
|
MEMCACHE_MAX_KEY_LENGTH,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
yield ("Unnecessary")
|
||||||
|
|
||||||
|
|
||||||
|
yield (
|
||||||
|
"# * Make sure each ForeignKey and OneToOneField has `on_delete` set "
|
||||||
|
"to the desired behavior"
|
||||||
|
)
|
||||||
|
yield (
|
||||||
|
"# * Remove `managed = False` lines if you wish to allow "
|
||||||
|
"Django to create, modify, and delete the table"
|
||||||
|
)
|
||||||
|
yield (
|
||||||
|
"# Feel free to rename the models, but don't rename db_table values or "
|
||||||
|
"field names."
|
||||||
|
)
|
||||||
|
|
||||||
|
yield "# * Make sure each ForeignKey and OneToOneField has `on_delete` set " "to the desired behavior"
|
||||||
|
yield "# * Remove `managed = False` lines if you wish to allow " "Django to create, modify, and delete the table"
|
||||||
|
yield "# Feel free to rename the models, but don't rename db_table values or " "field names."
|
||||||
```
|
```
|
||||||
|
|
||||||
## Output
|
## Output
|
||||||
|
@ -110,7 +142,7 @@ def foo():
|
||||||
|
|
||||||
for e in l:
|
for e in l:
|
||||||
# comment
|
# comment
|
||||||
yield e # Too many parentheses
|
yield (e) # Too many parentheses
|
||||||
# comment
|
# comment
|
||||||
|
|
||||||
for ridiculouslylongelementnameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee in l:
|
for ridiculouslylongelementnameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee in l:
|
||||||
|
@ -135,6 +167,51 @@ def foo():
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
yield (
|
||||||
|
"Cache key will cause errors if used with memcached: %r "
|
||||||
|
"(longer than %s)"
|
||||||
|
% (
|
||||||
|
key,
|
||||||
|
MEMCACHE_MAX_KEY_LENGTH,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
yield "Cache key will cause errors if used with memcached: %r " "(longer than %s)" % (
|
||||||
|
key,
|
||||||
|
MEMCACHE_MAX_KEY_LENGTH,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
yield ("Unnecessary")
|
||||||
|
|
||||||
|
|
||||||
|
yield (
|
||||||
|
"# * Make sure each ForeignKey and OneToOneField has `on_delete` set "
|
||||||
|
"to the desired behavior"
|
||||||
|
)
|
||||||
|
yield (
|
||||||
|
"# * Remove `managed = False` lines if you wish to allow "
|
||||||
|
"Django to create, modify, and delete the table"
|
||||||
|
)
|
||||||
|
yield (
|
||||||
|
"# Feel free to rename the models, but don't rename db_table values or "
|
||||||
|
"field names."
|
||||||
|
)
|
||||||
|
|
||||||
|
yield (
|
||||||
|
"# * Make sure each ForeignKey and OneToOneField has `on_delete` set "
|
||||||
|
"to the desired behavior"
|
||||||
|
)
|
||||||
|
yield (
|
||||||
|
"# * Remove `managed = False` lines if you wish to allow "
|
||||||
|
"Django to create, modify, and delete the table"
|
||||||
|
)
|
||||||
|
yield (
|
||||||
|
"# Feel free to rename the models, but don't rename db_table values or "
|
||||||
|
"field names."
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue