mirror of https://github.com/astral-sh/ruff
apply Micha's patch, fixing everything?
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
97850661fd
commit
efa372b379
|
|
@ -1,14 +1,11 @@
|
||||||
use ruff_formatter::{FormatRuleWithOptions, RemoveSoftLinesBuffer, write};
|
use ruff_formatter::{FormatRuleWithOptions, RemoveSoftLinesBuffer, format_args, write};
|
||||||
use ruff_python_ast::{AnyNodeRef, Expr, ExprLambda};
|
use ruff_python_ast::{AnyNodeRef, Expr, ExprLambda};
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
use crate::builders::parenthesize_if_expands;
|
use crate::builders::parenthesize_if_expands;
|
||||||
use crate::comments::dangling_comments;
|
use crate::comments::dangling_comments;
|
||||||
use crate::expression::has_own_parentheses;
|
use crate::expression::has_own_parentheses;
|
||||||
use crate::expression::maybe_parenthesize_expression;
|
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses};
|
||||||
use crate::expression::parentheses::{
|
|
||||||
NeedsParentheses, OptionalParentheses, Parenthesize, is_expression_parenthesized,
|
|
||||||
};
|
|
||||||
use crate::other::parameters::ParametersParentheses;
|
use crate::other::parameters::ParametersParentheses;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::preview::is_force_single_line_lambda_parameters_enabled;
|
use crate::preview::is_force_single_line_lambda_parameters_enabled;
|
||||||
|
|
@ -83,28 +80,32 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Avoid parenthesizing lists, dictionaries, etc. that have their own parentheses, but still
|
if is_parenthesize_lambda_bodies_enabled(f.context()) {
|
||||||
// wrap calls and subscripts, which can have long expressions before the parentheses:
|
let fmt_body = format_with(|f| {
|
||||||
// ```py
|
if matches!(&**body, Expr::Call(_) | Expr::Subscript(_)) {
|
||||||
// lambda arg1, arg2, arg3, *args, **kwargs: a_loooooooooooong_call_expression.with_an_attr(inner, args)
|
let body = body.format().with_options(Parentheses::Never).memoized();
|
||||||
// ```
|
|
||||||
let needs_parentheses = has_own_parentheses(body, f.context()).is_none()
|
|
||||||
|| matches!(&**body, Expr::Call(_) | Expr::Subscript(_));
|
|
||||||
|
|
||||||
if is_parenthesize_lambda_bodies_enabled(f.context())
|
best_fitting![
|
||||||
&& needs_parentheses
|
// body all flat
|
||||||
&& !is_expression_parenthesized(body.into(), comments.ranges(), f.context().source())
|
body,
|
||||||
{
|
// body expanded
|
||||||
match self.layout {
|
body,
|
||||||
ExprLambdaLayout::Default => maybe_parenthesize_expression(
|
// parenthesized
|
||||||
body,
|
format_args![token("("), block_indent(&body), token(")")]
|
||||||
item,
|
]
|
||||||
Parenthesize::IfBreaksParenthesizedNested,
|
.fmt(f)
|
||||||
)
|
} else if has_own_parentheses(body, f.context()).is_some() {
|
||||||
.fmt(f),
|
// We probably need to be more careful here and preserve parentheses if there are comments?
|
||||||
ExprLambdaLayout::Assignment => {
|
body.format().fmt(f)
|
||||||
fits_expanded(&parenthesize_if_expands(&body.format())).fmt(f)
|
} else {
|
||||||
|
parenthesize_if_expands(&body.format().with_options(Parentheses::Never)).fmt(f)
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
match self.layout {
|
||||||
|
// Can we move the `fits_expanded` into the assignment formatting?
|
||||||
|
ExprLambdaLayout::Assignment => fits_expanded(&fmt_body).fmt(f),
|
||||||
|
ExprLambdaLayout::Default => fmt_body.fmt(f),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
body.format().fmt(f)
|
body.format().fmt(f)
|
||||||
|
|
|
||||||
|
|
@ -375,7 +375,7 @@ a = b if """
|
||||||
# Another use case
|
# Another use case
|
||||||
data = yaml.load("""\
|
data = yaml.load("""\
|
||||||
a: 1
|
a: 1
|
||||||
@@ -77,19 +106,23 @@
|
@@ -77,10 +106,12 @@
|
||||||
b: 2
|
b: 2
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
@ -390,19 +390,7 @@ a = b if """
|
||||||
|
|
||||||
MULTILINE = """
|
MULTILINE = """
|
||||||
foo
|
foo
|
||||||
""".replace("\n", "")
|
@@ -156,16 +187,24 @@
|
||||||
-generated_readme = lambda project_name: """
|
|
||||||
+generated_readme = lambda project_name: (
|
|
||||||
+ """
|
|
||||||
{}
|
|
||||||
|
|
||||||
<Add content here!>
|
|
||||||
""".strip().format(project_name)
|
|
||||||
+)
|
|
||||||
parser.usage += """
|
|
||||||
Custom extra help summary.
|
|
||||||
|
|
||||||
@@ -156,16 +189,24 @@
|
|
||||||
10 LOAD_CONST 0 (None)
|
10 LOAD_CONST 0 (None)
|
||||||
12 RETURN_VALUE
|
12 RETURN_VALUE
|
||||||
""" % (_C.__init__.__code__.co_firstlineno + 1,)
|
""" % (_C.__init__.__code__.co_firstlineno + 1,)
|
||||||
|
|
@ -433,7 +421,7 @@ a = b if """
|
||||||
[
|
[
|
||||||
"""cow
|
"""cow
|
||||||
moos""",
|
moos""",
|
||||||
@@ -206,7 +247,9 @@
|
@@ -206,7 +245,9 @@
|
||||||
"c"
|
"c"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -444,7 +432,7 @@ a = b if """
|
||||||
|
|
||||||
assert some_var == expected_result, """
|
assert some_var == expected_result, """
|
||||||
test
|
test
|
||||||
@@ -224,10 +267,8 @@
|
@@ -224,10 +265,8 @@
|
||||||
"""Sxxxxxxx xxxxxxxx, xxxxxxx xx xxxxxxxxx
|
"""Sxxxxxxx xxxxxxxx, xxxxxxx xx xxxxxxxxx
|
||||||
xxxxxxxxxxxxx xxxxxxx xxxxxxxxx xxx-xxxxxxxxxx xxxxxx xx xxx-xxxxxx"""
|
xxxxxxxxxxxxx xxxxxxx xxxxxxxxx xxx-xxxxxxxxxx xxxxxx xx xxx-xxxxxx"""
|
||||||
),
|
),
|
||||||
|
|
@ -457,7 +445,7 @@ a = b if """
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,14 +287,12 @@
|
@@ -246,14 +285,12 @@
|
||||||
a
|
a
|
||||||
a"""
|
a"""
|
||||||
),
|
),
|
||||||
|
|
@ -597,13 +585,11 @@ data = yaml.load(
|
||||||
MULTILINE = """
|
MULTILINE = """
|
||||||
foo
|
foo
|
||||||
""".replace("\n", "")
|
""".replace("\n", "")
|
||||||
generated_readme = lambda project_name: (
|
generated_readme = lambda project_name: """
|
||||||
"""
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
<Add content here!>
|
<Add content here!>
|
||||||
""".strip().format(project_name)
|
""".strip().format(project_name)
|
||||||
)
|
|
||||||
parser.usage += """
|
parser.usage += """
|
||||||
Custom extra help summary.
|
Custom extra help summary.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -913,7 +913,27 @@ if 1:
|
||||||
):
|
):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -218,71 +200,79 @@
|
@@ -156,7 +138,8 @@
|
||||||
|
*x: x
|
||||||
|
)
|
||||||
|
|
||||||
|
-lambda: ( # comment
|
||||||
|
+lambda: (
|
||||||
|
+ # comment
|
||||||
|
x
|
||||||
|
)
|
||||||
|
|
||||||
|
@@ -184,7 +167,8 @@
|
||||||
|
|
||||||
|
(
|
||||||
|
lambda: # comment
|
||||||
|
- ( # comment
|
||||||
|
+ (
|
||||||
|
+ # comment
|
||||||
|
x
|
||||||
|
)
|
||||||
|
)
|
||||||
|
@@ -218,71 +202,79 @@
|
||||||
|
|
||||||
# Leading
|
# Leading
|
||||||
lambda x: (
|
lambda x: (
|
||||||
|
|
@ -1052,7 +1072,7 @@ if 1:
|
||||||
|
|
||||||
|
|
||||||
# Regression tests for https://github.com/astral-sh/ruff/issues/8179
|
# Regression tests for https://github.com/astral-sh/ruff/issues/8179
|
||||||
@@ -291,9 +281,9 @@
|
@@ -291,9 +283,9 @@
|
||||||
c,
|
c,
|
||||||
d,
|
d,
|
||||||
e,
|
e,
|
||||||
|
|
@ -1065,7 +1085,7 @@ if 1:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -302,15 +292,9 @@
|
@@ -302,15 +294,9 @@
|
||||||
c,
|
c,
|
||||||
d,
|
d,
|
||||||
e,
|
e,
|
||||||
|
|
@ -1084,7 +1104,7 @@ if 1:
|
||||||
g=10,
|
g=10,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -320,9 +304,9 @@
|
@@ -320,9 +306,9 @@
|
||||||
c,
|
c,
|
||||||
d,
|
d,
|
||||||
e,
|
e,
|
||||||
|
|
@ -1097,7 +1117,7 @@ if 1:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -338,9 +322,9 @@
|
@@ -338,9 +324,9 @@
|
||||||
|
|
||||||
class C:
|
class C:
|
||||||
function_dict: Dict[Text, Callable[[CRFToken], Any]] = {
|
function_dict: Dict[Text, Callable[[CRFToken], Any]] = {
|
||||||
|
|
@ -1110,7 +1130,7 @@ if 1:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -352,42 +336,40 @@
|
@@ -352,42 +338,40 @@
|
||||||
def foo():
|
def foo():
|
||||||
if True:
|
if True:
|
||||||
if True:
|
if True:
|
||||||
|
|
@ -1169,7 +1189,7 @@ if 1:
|
||||||
CREATE TABLE {table} AS
|
CREATE TABLE {table} AS
|
||||||
SELECT ROW_NUMBER() OVER () AS id, {var}
|
SELECT ROW_NUMBER() OVER () AS id, {var}
|
||||||
FROM (
|
FROM (
|
||||||
@@ -408,12 +390,12 @@
|
@@ -408,12 +392,12 @@
|
||||||
# 6
|
# 6
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -1186,7 +1206,7 @@ if 1:
|
||||||
)
|
)
|
||||||
|
|
||||||
very_long_variable_name_x, very_long_variable_name_y = (
|
very_long_variable_name_x, very_long_variable_name_y = (
|
||||||
@@ -421,8 +403,8 @@
|
@@ -421,8 +405,8 @@
|
||||||
lambda b: b * another_very_long_expression_here,
|
lambda b: b * another_very_long_expression_here,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -1197,19 +1217,4 @@ if 1:
|
||||||
x, more_args, additional_parameters
|
x, more_args, additional_parameters
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -433,9 +415,11 @@
|
|
||||||
if 3:
|
|
||||||
if self.location in EVM_EVMLIKE_LOCATIONS and database is not None:
|
|
||||||
exported_dict["notes"] = EVM_ADDRESS_REGEX.sub(
|
|
||||||
- repl=lambda matched_address: self._maybe_add_label_with_address(
|
|
||||||
- database=database,
|
|
||||||
- matched_address=matched_address,
|
|
||||||
+ repl=lambda matched_address: (
|
|
||||||
+ self._maybe_add_label_with_address(
|
|
||||||
+ database=database,
|
|
||||||
+ matched_address=matched_address,
|
|
||||||
+ )
|
|
||||||
),
|
|
||||||
string=exported_dict["notes"],
|
|
||||||
)
|
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -111,15 +111,16 @@ generated_readme = (
|
||||||
```diff
|
```diff
|
||||||
--- Stable
|
--- Stable
|
||||||
+++ Preview
|
+++ Preview
|
||||||
@@ -44,8 +44,8 @@
|
@@ -44,10 +44,8 @@
|
||||||
# this by changing `Lambda::needs_parentheses` to return `BestFit` but it causes
|
# this by changing `Lambda::needs_parentheses` to return `BestFit` but it causes
|
||||||
# issues when the lambda has comments.
|
# issues when the lambda has comments.
|
||||||
# Let's keep this as a known deviation for now.
|
# Let's keep this as a known deviation for now.
|
||||||
-generated_readme = (
|
-generated_readme = (
|
||||||
- lambda project_name: """
|
- lambda project_name: """
|
||||||
+generated_readme = lambda project_name: (
|
+generated_readme = lambda project_name: """
|
||||||
+ """
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
<Add content here!>
|
<Add content here!>
|
||||||
|
""".strip().format(project_name)
|
||||||
|
-)
|
||||||
```
|
```
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue