From 810f53a9bf11c9888a7112cd916301dcc5157ffd Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 25 Oct 2023 18:09:36 -0400 Subject: [PATCH] Avoid introducing new parentheses in annotated assignments --- .../fixtures/ruff/statement/ann_assign.py | 11 ++++++ .../src/statement/stmt_ann_assign.rs | 7 +--- .../format@statement__ann_assign.py.snap | 36 +++++++++++++++++-- 3 files changed, 45 insertions(+), 9 deletions(-) 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 b965199c982609..3cd52cb2e0ab9c 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 @@ -1,6 +1,17 @@ # Regression test: Don't forget the parentheses in the value when breaking aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int = a + 1 * a +bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = ( + Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb() +) + +JSONSerializable: TypeAlias = ( + "str | int | float | bool | None | list | tuple | JSONMapping" +) + +JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = {1, 2, 3, 4} + +JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = aaaaaaaaaaaaaaaa # Regression test: Don't forget the parentheses in the annotation when breaking class DefaultRunner: 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 a29798d4c33160..f22457cc4b56a4 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs @@ -21,12 +21,7 @@ impl FormatNodeRule for FormatStmtAnnAssign { write!( f, - [ - target.format(), - token(":"), - space(), - maybe_parenthesize_expression(annotation, item, Parenthesize::IfBreaks) - ] + [target.format(), token(":"), space(), annotation.format(),] )?; if let Some(value) = value { 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 52d136a3bef135..a27cafa30feb73 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 @@ -7,6 +7,17 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/ # Regression test: Don't forget the parentheses in the value when breaking aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int = a + 1 * a +bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = ( + Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb() +) + +JSONSerializable: TypeAlias = ( + "str | int | float | bool | None | list | tuple | JSONMapping" +) + +JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = {1, 2, 3, 4} + +JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = aaaaaaaaaaaaaaaa # Regression test: Don't forget the parentheses in the annotation when breaking class DefaultRunner: @@ -20,12 +31,31 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int = a + 1 * a ) +bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = ( + Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb() +) + +JSONSerializable: TypeAlias = ( + "str | int | float | bool | None | list | tuple | JSONMapping" +) + +JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = { + 1, + 2, + 3, + 4, +} + +JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = ( + aaaaaaaaaaaaaaaa +) + # Regression test: Don't forget the parentheses in the annotation when breaking class DefaultRunner: - task_runner_cls: ( - TaskRunnerProtocol | typing.Callable[[], typing.Any] - ) = DefaultTaskRunner + task_runner_cls: TaskRunnerProtocol | typing.Callable[ + [], typing.Any + ] = DefaultTaskRunner ```