Ruff 2025 style guide (#13906)

Closes #13371
This commit is contained in:
Micha Reiser 2025-01-03 14:16:10 +01:00
parent a95deec00f
commit 424b720c19
62 changed files with 1799 additions and 3890 deletions

View file

@ -3,10 +3,8 @@ use ruff_formatter::write;
use ruff_python_ast::StmtAssert;
use crate::comments::SourceComment;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::preview::is_join_implicit_concatenated_string_enabled;
use crate::{has_skip_comment, prelude::*};
#[derive(Default)]
@ -30,18 +28,12 @@ impl FormatNodeRule<StmtAssert> for FormatStmtAssert {
)?;
if let Some(msg) = msg {
let parenthesize = if is_join_implicit_concatenated_string_enabled(f.context()) {
Parenthesize::IfBreaksParenthesized
} else {
Parenthesize::IfBreaks
};
write!(
f,
[
token(","),
space(),
maybe_parenthesize_expression(msg, item, parenthesize),
maybe_parenthesize_expression(msg, item, Parenthesize::IfBreaksParenthesized),
]
)?;
}

View file

@ -1,7 +1,7 @@
use ruff_formatter::{format_args, write, FormatError, RemoveSoftLinesBuffer};
use ruff_python_ast::{
AnyNodeRef, Expr, ExprAttribute, ExprCall, FStringPart, Operator, StmtAssign, StringLike,
TypeParams,
AnyNodeRef, Expr, ExprAttribute, ExprCall, FString, FStringPart, Operator, StmtAssign,
StringLike, TypeParams,
};
use crate::builders::parenthesize_if_expands;
@ -9,7 +9,6 @@ use crate::comments::{
trailing_comments, Comments, LeadingDanglingTrailingComments, SourceComment,
};
use crate::context::{NodeLevel, WithNodeLevel};
use crate::expression::expr_f_string::f_string_quoting;
use crate::expression::parentheses::{
is_expression_parenthesized, optional_parentheses, NeedsParentheses, OptionalParentheses,
Parentheses, Parenthesize,
@ -18,10 +17,7 @@ use crate::expression::{
can_omit_optional_parentheses, has_own_parentheses, has_parentheses,
maybe_parenthesize_expression,
};
use crate::other::f_string::{FStringLayout, FormatFString};
use crate::preview::{
is_f_string_formatting_enabled, is_join_implicit_concatenated_string_enabled,
};
use crate::other::f_string::FStringLayout;
use crate::statement::trailing_semicolon;
use crate::string::implicit::{
FormatImplicitConcatenatedStringExpanded, FormatImplicitConcatenatedStringFlat,
@ -456,7 +452,7 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
let f_string_flat = format_with(|f| {
let mut buffer = RemoveSoftLinesBuffer::new(&mut *f);
write!(buffer, [format_f_string])
write!(buffer, [format_f_string.format()])
})
.memoized();
@ -518,7 +514,7 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
// }moreeeeeeeeeeeeeeeee"
// ```
let format_f_string =
format_with(|f| write!(f, [format_f_string, inline_comments]));
format_with(|f| write!(f, [format_f_string.format(), inline_comments]));
best_fitting![single_line, joined_parenthesized, format_f_string]
.with_mode(BestFittingMode::AllLines)
@ -668,7 +664,7 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
// Similar to above, remove any soft line breaks emitted by the f-string
// formatting.
let mut buffer = RemoveSoftLinesBuffer::new(&mut *f);
write!(buffer, [format_f_string])
write!(buffer, [format_f_string.format()])
} else {
value.format().with_options(Parentheses::Never).fmt(f)
}
@ -701,20 +697,22 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
// )
// ```
let flat_target_parenthesize_value = format_with(|f| {
write!(f, [last_target, space(), operator, space(), token("("),])?;
if is_join_implicit_concatenated_string_enabled(f.context()) {
group(&soft_block_indent(&format_args![
format_value,
inline_comments
]))
.should_expand(true)
.fmt(f)?;
} else {
block_indent(&format_args![format_value, inline_comments]).fmt(f)?;
}
token(")").fmt(f)
write!(
f,
[
last_target,
space(),
operator,
space(),
token("("),
group(&soft_block_indent(&format_args![
format_value,
inline_comments
]))
.should_expand(true),
token(")")
]
)
});
// Fall back to parenthesizing (or splitting) the last target part if we can't make the value
@ -726,15 +724,16 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
// ] = c
// ```
let split_target_flat_value = format_with(|f| {
if is_join_implicit_concatenated_string_enabled(f.context()) {
group(&last_target).should_expand(true).fmt(f)?;
} else {
last_target.fmt(f)?;
}
write!(
f,
[space(), operator, space(), format_value, inline_comments]
[
group(&last_target).should_expand(true),
space(),
operator,
space(),
format_value,
inline_comments
]
)
});
@ -935,7 +934,8 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
}
let format_f_string =
format_with(|f| write!(f, [format_f_string, inline_comments])).memoized();
format_with(|f| write!(f, [format_f_string.format(), inline_comments]))
.memoized();
// Considering the following initial source:
//
@ -1102,11 +1102,7 @@ impl Format<PyFormatContext<'_>> for FormatStatementsLastExpression<'_> {
fn format_f_string_assignment<'a>(
string: StringLike<'a>,
context: &PyFormatContext,
) -> Option<FormatFString<'a>> {
if !is_f_string_formatting_enabled(context) {
return None;
}
) -> Option<&'a FString> {
let StringLike::FString(expr) = string else {
return None;
};
@ -1128,10 +1124,7 @@ fn format_f_string_assignment<'a>(
return None;
}
Some(FormatFString::new(
f_string,
f_string_quoting(expr, context.source()),
))
Some(f_string)
}
#[derive(Debug, Default)]