Skip to content

Commit a6f32dd

Browse files
authored
Ruff 2024.2 style (#9639)
1 parent 0293908 commit a6f32dd

File tree

47 files changed

+836
-2363
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+836
-2363
lines changed

crates/ruff/tests/format.rs

+42-48
Original file line numberDiff line numberDiff line change
@@ -358,58 +358,52 @@ def f(x):
358358
'''
359359
pass
360360
"#), @r###"
361-
success: true
362-
exit_code: 0
363-
----- stdout -----
364-
def f(x):
365-
"""
366-
Something about `f`. And an example:
367-
368-
.. code-block:: python
369-
370-
(
371-
foo,
372-
bar,
373-
quux,
374-
) = this_is_a_long_line(
375-
lion,
376-
hippo,
377-
lemur,
378-
bear,
361+
success: true
362+
exit_code: 0
363+
----- stdout -----
364+
def f(x):
365+
"""
366+
Something about `f`. And an example:
367+
368+
.. code-block:: python
369+
370+
foo, bar, quux = (
371+
this_is_a_long_line(
372+
lion,
373+
hippo,
374+
lemur,
375+
bear,
376+
)
377+
)
378+
379+
Another example:
380+
381+
```py
382+
foo, bar, quux = (
383+
this_is_a_long_line(
384+
lion,
385+
hippo,
386+
lemur,
387+
bear,
388+
)
379389
)
390+
```
380391
381-
Another example:
392+
And another:
382393
383-
```py
384-
(
385-
foo,
386-
bar,
387-
quux,
388-
) = this_is_a_long_line(
389-
lion,
390-
hippo,
391-
lemur,
392-
bear,
393-
)
394-
```
395-
396-
And another:
394+
>>> foo, bar, quux = (
395+
... this_is_a_long_line(
396+
... lion,
397+
... hippo,
398+
... lemur,
399+
... bear,
400+
... )
401+
... )
402+
"""
403+
pass
397404
398-
>>> (
399-
... foo,
400-
... bar,
401-
... quux,
402-
... ) = this_is_a_long_line(
403-
... lion,
404-
... hippo,
405-
... lemur,
406-
... bear,
407-
... )
408-
"""
409-
pass
410-
411-
----- stderr -----
412-
"###);
405+
----- stderr -----
406+
"###);
413407
Ok(())
414408
}
415409

crates/ruff_python_formatter/src/comments/format.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use ruff_text_size::{Ranged, TextLen, TextRange};
1010
use crate::comments::SourceComment;
1111
use crate::context::NodeLevel;
1212
use crate::prelude::*;
13-
use crate::preview::is_blank_line_after_nested_stub_class_enabled;
1413
use crate::statement::suite::should_insert_blank_line_after_class_in_stub_file;
1514

1615
/// Formats the leading comments of a node.
@@ -544,10 +543,7 @@ pub(crate) fn empty_lines_before_trailing_comments<'a>(
544543
// Black has different rules for stub vs. non-stub and top level vs. indented
545544
let empty_lines = match (f.options().source_type(), f.context().node_level()) {
546545
(PySourceType::Stub, NodeLevel::TopLevel(_)) => 1,
547-
(PySourceType::Stub, _) => u32::from(
548-
is_blank_line_after_nested_stub_class_enabled(f.context())
549-
&& node_kind == NodeKind::StmtClassDef,
550-
),
546+
(PySourceType::Stub, _) => u32::from(node_kind == NodeKind::StmtClassDef),
551547
(_, NodeLevel::TopLevel(_)) => 2,
552548
(_, _) => 1,
553549
};

crates/ruff_python_formatter/src/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ impl<'a> PyFormatContext<'a> {
9999
}
100100

101101
/// Returns `true` if preview mode is enabled.
102+
#[allow(unused)]
102103
pub(crate) const fn is_preview(&self) -> bool {
103104
self.options.preview().is_enabled()
104105
}

crates/ruff_python_formatter/src/expression/binary_like.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::expression::parentheses::{
2020
};
2121
use crate::expression::OperatorPrecedence;
2222
use crate::prelude::*;
23-
use crate::preview::is_fix_power_op_line_length_enabled;
2423
use crate::string::{AnyString, FormatStringContinuation};
2524

2625
#[derive(Copy, Clone, Debug)]
@@ -722,9 +721,7 @@ impl Format<PyFormatContext<'_>> for FlatBinaryExpressionSlice<'_> {
722721
{
723722
hard_line_break().fmt(f)?;
724723
} else if is_pow {
725-
if is_fix_power_op_line_length_enabled(f.context()) {
726-
in_parentheses_only_if_group_breaks(&space()).fmt(f)?;
727-
}
724+
in_parentheses_only_if_group_breaks(&space()).fmt(f)?;
728725
} else {
729726
space().fmt(f)?;
730727
}

crates/ruff_python_formatter/src/other/arguments.rs

-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::expression::is_expression_huggable;
99
use crate::expression::parentheses::{empty_parenthesized, parenthesized, Parentheses};
1010
use crate::other::commas;
1111
use crate::prelude::*;
12-
use crate::preview::is_multiline_string_handling_enabled;
1312
use crate::string::AnyString;
1413

1514
#[derive(Default)]
@@ -238,10 +237,6 @@ fn is_huggable_string_argument(
238237
arguments: &Arguments,
239238
context: &PyFormatContext,
240239
) -> bool {
241-
if !is_multiline_string_handling_enabled(context) {
242-
return false;
243-
}
244-
245240
if string.is_implicit_concatenated() || !string.is_multiline(context.source()) {
246241
return false;
247242
}

crates/ruff_python_formatter/src/other/f_string_element.rs

-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use ruff_text_size::Ranged;
99
use crate::comments::{dangling_open_parenthesis_comments, trailing_comments};
1010
use crate::context::{FStringState, NodeLevel, WithFStringState, WithNodeLevel};
1111
use crate::prelude::*;
12-
use crate::preview::is_hex_codes_in_unicode_sequences_enabled;
1312
use crate::string::normalize_string;
1413
use crate::verbatim::verbatim_text;
1514

@@ -62,7 +61,6 @@ impl Format<PyFormatContext<'_>> for FormatFStringLiteralElement<'_> {
6261
0,
6362
self.context.quotes(),
6463
self.context.prefix(),
65-
is_hex_codes_in_unicode_sequences_enabled(f.context()),
6664
true,
6765
);
6866
match &normalized {

crates/ruff_python_formatter/src/other/with_item.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::expression::parentheses::{
77
is_expression_parenthesized, parenthesized, Parentheses, Parenthesize,
88
};
99
use crate::prelude::*;
10-
use crate::preview::is_wrap_multiple_context_managers_in_parens_enabled;
1110

1211
#[derive(Default)]
1312
pub struct FormatWithItem;
@@ -30,9 +29,7 @@ impl FormatNodeRule<WithItem> for FormatWithItem {
3029
);
3130

3231
// Remove the parentheses of the `with_items` if the with statement adds parentheses
33-
if f.context().node_level().is_parenthesized()
34-
&& is_wrap_multiple_context_managers_in_parens_enabled(f.context())
35-
{
32+
if f.context().node_level().is_parenthesized() {
3633
if is_parenthesized {
3734
// ...except if the with item is parenthesized, then use this with item as a preferred breaking point
3835
// or when it has comments, then parenthesize it to prevent comments from moving.

crates/ruff_python_formatter/src/preview.rs

+1-69
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
//! to stable. The challenge with directly using [`is_preview`](PyFormatContext::is_preview) is that it is unclear
55
//! for which specific feature this preview check is for. Having named functions simplifies the promotion:
66
//! Simply delete the function and let Rust tell you which checks you have to remove.
7-
use crate::PyFormatContext;
87
9-
/// Returns `true` if the [`fix_power_op_line_length`](https://github.com/astral-sh/ruff/issues/8938) preview style is enabled.
10-
pub(crate) const fn is_fix_power_op_line_length_enabled(context: &PyFormatContext) -> bool {
11-
context.is_preview()
12-
}
8+
use crate::PyFormatContext;
139

1410
/// Returns `true` if the [`hug_parens_with_braces_and_square_brackets`](https://github.com/astral-sh/ruff/issues/8279) preview style is enabled.
1511
pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(
@@ -18,70 +14,6 @@ pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(
1814
context.is_preview()
1915
}
2016

21-
/// Returns `true` if the [`prefer_splitting_right_hand_side_of_assignments`](https://github.com/astral-sh/ruff/issues/6975) preview style is enabled.
22-
pub(crate) const fn is_prefer_splitting_right_hand_side_of_assignments_enabled(
23-
context: &PyFormatContext,
24-
) -> bool {
25-
context.is_preview()
26-
}
27-
28-
/// Returns `true` if the [`parenthesize_long_type_hints`](https://github.com/astral-sh/ruff/issues/8894) preview style is enabled.
29-
pub(crate) const fn is_parenthesize_long_type_hints_enabled(context: &PyFormatContext) -> bool {
30-
context.is_preview()
31-
}
32-
33-
/// Returns `true` if the [`no_blank_line_before_class_docstring`] preview style is enabled.
34-
///
35-
/// [`no_blank_line_before_class_docstring`]: https://github.com/astral-sh/ruff/issues/8888
36-
pub(crate) const fn is_no_blank_line_before_class_docstring_enabled(
37-
context: &PyFormatContext,
38-
) -> bool {
39-
context.is_preview()
40-
}
41-
42-
/// Returns `true` if the [`wrap_multiple_context_managers_in_parens`](https://github.com/astral-sh/ruff/issues/8889) preview style is enabled.
43-
///
44-
/// Unlike Black, we re-use the same preview style feature flag for [`improved_async_statements_handling`](https://github.com/astral-sh/ruff/issues/8890)
45-
pub(crate) const fn is_wrap_multiple_context_managers_in_parens_enabled(
46-
context: &PyFormatContext,
47-
) -> bool {
48-
context.is_preview()
49-
}
50-
51-
/// Returns `true` if the [`blank_line_after_nested_stub_class`](https://github.com/astral-sh/ruff/issues/8891) preview style is enabled.
52-
pub(crate) const fn is_blank_line_after_nested_stub_class_enabled(
53-
context: &PyFormatContext,
54-
) -> bool {
55-
context.is_preview()
56-
}
57-
58-
/// Returns `true` if the [`module_docstring_newlines`](https://github.com/astral-sh/ruff/issues/7995) preview style is enabled.
59-
pub(crate) const fn is_module_docstring_newlines_enabled(context: &PyFormatContext) -> bool {
60-
context.is_preview()
61-
}
62-
63-
/// Returns `true` if the [`dummy_implementations`](https://github.com/astral-sh/ruff/issues/8357) preview style is enabled.
64-
pub(crate) const fn is_dummy_implementations_enabled(context: &PyFormatContext) -> bool {
65-
context.is_preview()
66-
}
67-
68-
/// Returns `true` if the [`hex_codes_in_unicode_sequences`](https://github.com/psf/black/pull/2916) preview style is enabled.
69-
pub(crate) const fn is_hex_codes_in_unicode_sequences_enabled(context: &PyFormatContext) -> bool {
70-
context.is_preview()
71-
}
72-
73-
/// Returns `true` if the [`multiline_string_handling`](https://github.com/astral-sh/ruff/issues/8896) preview style is enabled.
74-
pub(crate) const fn is_multiline_string_handling_enabled(context: &PyFormatContext) -> bool {
75-
context.is_preview()
76-
}
77-
78-
/// Returns `true` if the [`multiline_string_handling`](https://github.com/astral-sh/ruff/pull/9725) preview style is enabled.
79-
/// Black does not [`format docstrings`](https://github.com/psf/black/issues/3493) so we keep this
80-
/// preview for compatibility with Black.
81-
pub(crate) const fn is_format_module_docstring_enabled(context: &PyFormatContext) -> bool {
82-
context.is_preview()
83-
}
84-
8517
/// Returns `true` if the [`f-string formatting`](https://github.com/astral-sh/ruff/issues/7594) preview style is enabled.
8618
pub(crate) fn is_f_string_formatting_enabled(context: &PyFormatContext) -> bool {
8719
context.is_preview()

crates/ruff_python_formatter/src/statement/clause.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
88
use ruff_text_size::{Ranged, TextRange, TextSize};
99

1010
use crate::comments::{leading_alternate_branch_comments, trailing_comments, SourceComment};
11-
use crate::preview::is_dummy_implementations_enabled;
1211
use crate::statement::suite::{contains_only_an_ellipsis, SuiteKind};
1312
use crate::verbatim::write_suppressed_clause_header;
1413
use crate::{has_skip_comment, prelude::*};
@@ -405,8 +404,7 @@ impl Format<PyFormatContext<'_>> for FormatClauseBody<'_> {
405404
// In stable, stubs are only collapsed in stub files, in preview stubs in functions
406405
// or classes are collapsed too
407406
let should_collapse_stub = f.options().source_type().is_stub()
408-
|| (is_dummy_implementations_enabled(f.context())
409-
&& matches!(self.kind, SuiteKind::Function | SuiteKind::Class));
407+
|| matches!(self.kind, SuiteKind::Function | SuiteKind::Class);
410408

411409
if should_collapse_stub
412410
&& contains_only_an_ellipsis(self.body, f.context().comments())

crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs

+15-32
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ use ruff_formatter::write;
22
use ruff_python_ast::StmtAnnAssign;
33

44
use crate::comments::SourceComment;
5+
use crate::expression::is_splittable_expression;
56
use crate::expression::parentheses::Parentheses;
6-
use crate::expression::{has_parentheses, is_splittable_expression};
7-
use crate::preview::{
8-
is_parenthesize_long_type_hints_enabled,
9-
is_prefer_splitting_right_hand_side_of_assignments_enabled,
10-
};
117
use crate::statement::stmt_assign::{
128
AnyAssignmentOperator, AnyBeforeOperator, FormatStatementsLastExpression,
139
};
@@ -30,13 +26,7 @@ impl FormatNodeRule<StmtAnnAssign> for FormatStmtAnnAssign {
3026
write!(f, [target.format(), token(":"), space()])?;
3127

3228
if let Some(value) = value {
33-
if is_prefer_splitting_right_hand_side_of_assignments_enabled(f.context())
34-
// The `has_parentheses` check can be removed when stabilizing `is_parenthesize_long_type_hints`.
35-
// because `is_splittable_expression` covers both.
36-
&& (has_parentheses(annotation, f.context()).is_some()
37-
|| (is_parenthesize_long_type_hints_enabled(f.context())
38-
&& is_splittable_expression(annotation, f.context())))
39-
{
29+
if is_splittable_expression(annotation, f.context()) {
4030
FormatStatementsLastExpression::RightToLeft {
4131
before_operator: AnyBeforeOperator::Expression(annotation),
4232
operator: AnyAssignmentOperator::Assign,
@@ -47,23 +37,20 @@ impl FormatNodeRule<StmtAnnAssign> for FormatStmtAnnAssign {
4737
} else {
4838
// Remove unnecessary parentheses around the annotation if the parenthesize long type hints preview style is enabled.
4939
// Ensure we keep the parentheses if the annotation has any comments.
50-
if is_parenthesize_long_type_hints_enabled(f.context()) {
51-
if f.context().comments().has_leading(annotation.as_ref())
52-
|| f.context().comments().has_trailing(annotation.as_ref())
53-
{
54-
annotation
55-
.format()
56-
.with_options(Parentheses::Always)
57-
.fmt(f)?;
58-
} else {
59-
annotation
60-
.format()
61-
.with_options(Parentheses::Never)
62-
.fmt(f)?;
63-
}
40+
if f.context().comments().has_leading(annotation.as_ref())
41+
|| f.context().comments().has_trailing(annotation.as_ref())
42+
{
43+
annotation
44+
.format()
45+
.with_options(Parentheses::Always)
46+
.fmt(f)?;
6447
} else {
65-
annotation.format().fmt(f)?;
48+
annotation
49+
.format()
50+
.with_options(Parentheses::Never)
51+
.fmt(f)?;
6652
}
53+
6754
write!(
6855
f,
6956
[
@@ -83,11 +70,7 @@ impl FormatNodeRule<StmtAnnAssign> for FormatStmtAnnAssign {
8370
// Decimal # the user's age, used to determine if it's safe for them to use ruff
8471
// )
8572
// ```
86-
if is_parenthesize_long_type_hints_enabled(f.context()) {
87-
FormatStatementsLastExpression::left_to_right(annotation, item).fmt(f)?;
88-
} else {
89-
annotation.format().fmt(f)?;
90-
}
73+
FormatStatementsLastExpression::left_to_right(annotation, item).fmt(f)?;
9174
}
9275

9376
if f.options().source_type().is_ipynb()

0 commit comments

Comments
 (0)