diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/ann_assign.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/ann_assign.py index 2e4da78295556..f813a3fd60b01 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/ann_assign.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/ann_assign.py @@ -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 diff --git a/crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs b/crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs index 7407655adf538..0706fadde29f2 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs @@ -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, @@ -37,9 +37,16 @@ impl FormatNodeRule 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) diff --git a/crates/ruff_python_formatter/tests/snapshots/format@statement__ann_assign.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@statement__ann_assign.py.snap index bdc774a0e52d0..70dd73e8d3844 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@statement__ann_assign.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@statement__ann_assign.py.snap @@ -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 @@ -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: