Skip to content

Commit 533e4dd

Browse files
committed
Avoid introducing new parentheses in annotated assignments
1 parent ff9fb0d commit 533e4dd

5 files changed

Lines changed: 45 additions & 53 deletions

File tree

crates/ruff_python_formatter/README.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -537,28 +537,6 @@ def update_emission_strength():
537537
value = self.emission_strength * 2
538538
```
539539

540-
#### Type annotations may be parenthesized when expanded ([#7315](https://github.com/astral-sh/ruff/issues/7315))
541-
542-
Black will avoid parenthesizing type annotations in an annotated assignment, while Ruff will insert
543-
parentheses in some cases.
544-
545-
For example:
546-
547-
```python
548-
# Black
549-
StartElementHandler: Callable[[str, dict[str, str]], Any] | Callable[[str, list[str]], Any] | Callable[
550-
[str, dict[str, str], list[str]], Any
551-
] | None
552-
553-
# Ruff
554-
StartElementHandler: (
555-
Callable[[str, dict[str, str]], Any]
556-
| Callable[[str, list[str]], Any]
557-
| Callable[[str, dict[str, str], list[str]], Any]
558-
| None
559-
)
560-
```
561-
562540
#### Call chain calls break differently ([#7051](https://github.com/astral-sh/ruff/issues/7051))
563541

564542
Black occasionally breaks call chains differently than Ruff; in particular, Black occasionally

crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/ann_assign.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
# Regression test: Don't forget the parentheses in the value when breaking
22
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int = a + 1 * a
33

4+
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = (
5+
Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb()
6+
)
7+
8+
JSONSerializable: TypeAlias = (
9+
"str | int | float | bool | None | list | tuple | JSONMapping"
10+
)
11+
12+
JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = {1, 2, 3, 4}
13+
14+
JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = aaaaaaaaaaaaaaaa
415

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

crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ impl FormatNodeRule<StmtAnnAssign> for FormatStmtAnnAssign {
2121

2222
write!(
2323
f,
24-
[
25-
target.format(),
26-
token(":"),
27-
space(),
28-
maybe_parenthesize_expression(annotation, item, Parenthesize::IfBreaks)
29-
]
24+
[target.format(), token(":"), space(), annotation.format(),]
3025
)?;
3126

3227
if let Some(value) = value {

crates/ruff_python_formatter/tests/snapshots/format@statement__ann_assign.py.snap

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/
77
# Regression test: Don't forget the parentheses in the value when breaking
88
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int = a + 1 * a
99
10+
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = (
11+
Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb()
12+
)
13+
14+
JSONSerializable: TypeAlias = (
15+
"str | int | float | bool | None | list | tuple | JSONMapping"
16+
)
17+
18+
JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = {1, 2, 3, 4}
19+
20+
JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = aaaaaaaaaaaaaaaa
1021
1122
# Regression test: Don't forget the parentheses in the annotation when breaking
1223
class DefaultRunner:
@@ -20,12 +31,31 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int =
2031
a + 1 * a
2132
)
2233
34+
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb: Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = (
35+
Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb()
36+
)
37+
38+
JSONSerializable: TypeAlias = (
39+
"str | int | float | bool | None | list | tuple | JSONMapping"
40+
)
41+
42+
JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = {
43+
1,
44+
2,
45+
3,
46+
4,
47+
}
48+
49+
JSONSerializable: str | int | float | bool | None | list | tuple | JSONMapping = (
50+
aaaaaaaaaaaaaaaa
51+
)
52+
2353
2454
# Regression test: Don't forget the parentheses in the annotation when breaking
2555
class DefaultRunner:
26-
task_runner_cls: (
27-
TaskRunnerProtocol | typing.Callable[[], typing.Any]
28-
) = DefaultTaskRunner
56+
task_runner_cls: TaskRunnerProtocol | typing.Callable[
57+
[], typing.Any
58+
] = DefaultTaskRunner
2959
```
3060

3161

docs/formatter/black.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -399,28 +399,6 @@ def update_emission_strength():
399399
value = self.emission_strength * 2
400400
```
401401

402-
### Type annotations may be parenthesized when expanded
403-
404-
Black will avoid parenthesizing type annotations in an annotated assignment, while Ruff will insert
405-
parentheses in some cases.
406-
407-
For example:
408-
409-
```python
410-
# Black
411-
StartElementHandler: Callable[[str, dict[str, str]], Any] | Callable[[str, list[str]], Any] | Callable[
412-
[str, dict[str, str], list[str]], Any
413-
] | None
414-
415-
# Ruff
416-
StartElementHandler: (
417-
Callable[[str, dict[str, str]], Any]
418-
| Callable[[str, list[str]], Any]
419-
| Callable[[str, dict[str, str], list[str]], Any]
420-
| None
421-
)
422-
```
423-
424402
### Call chain calls break differently
425403

426404
Black occasionally breaks call chains differently than Ruff; in particular, Black occasionally

0 commit comments

Comments
 (0)