Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = aaaaaaaaaaaaaaaa

D: (E := 4) = (F := 5)

# Regression test: Don't forget the parentheses in the annotation when breaking
class DefaultRunner:
task_runner_cls: TaskRunnerProtocol | typing.Callable[[], typing.Any] = DefaultTaskRunner
13 changes: 10 additions & 3 deletions crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use ruff_formatter::write;
use ruff_python_ast::StmtAnnAssign;

use crate::expression::is_splittable_expression;
use crate::expression::parentheses::Parentheses;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses};
use crate::prelude::*;
use crate::statement::stmt_assign::{
AnyAssignmentOperator, AnyBeforeOperator, FormatStatementsLastExpression,
Expand Down Expand Up @@ -37,9 +37,16 @@ impl FormatNodeRule<StmtAnnAssign> for FormatStmtAnnAssign {
} else {
// Remove unnecessary parentheses around the annotation if the parenthesize long type hints preview style is enabled.
// Ensure we keep the parentheses if the annotation has any comments.
if f.context().comments().has_leading(annotation.as_ref())
let preserve_parentheses = f.context().comments().has_leading(annotation.as_ref())
|| f.context().comments().has_trailing(annotation.as_ref())
{
|| matches!(
annotation
.as_ref()
.needs_parentheses(item.into(), f.context()),
OptionalParentheses::Always
);

if preserve_parentheses {
annotation
.format()
.with_options(Parentheses::Always)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping =

JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = aaaaaaaaaaaaaaaa

D: (E := 4) = (F := 5)

# Regression test: Don't forget the parentheses in the annotation when breaking
class DefaultRunner:
task_runner_cls: TaskRunnerProtocol | typing.Callable[[], typing.Any] = DefaultTaskRunner
Expand Down Expand Up @@ -58,6 +60,8 @@ JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping =
aaaaaaaaaaaaaaaa
)

D: (E := 4) = (F := 5)


# Regression test: Don't forget the parentheses in the annotation when breaking
class DefaultRunner:
Expand Down