Skip to content

Commit

Permalink
Fix StmtAnnAssign formatting by mirroring StmtAssign (#5732)
Browse files Browse the repository at this point in the history
## Summary

`StmtAnnAssign` would not insert parentheses when breaking the same way
`StmtAssign` does, causing unstable formatting and likely some syntax
errors.

## Test Plan

I added a regression test.
  • Loading branch information
konstin committed Jul 13, 2023
1 parent b1781ab commit 549173b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
tree_depth += 1
# Regression test: Don't forget the parentheses in the value when breaking
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int = a + 1 * a

greeting += "This is very long, formal greeting for whomever is name here. Dear %s, it will break the line" % len(
name
)

# Regression test: Don't forget the parentheses in the annotation when breaking
class DefaultRunner:
task_runner_cls: TaskRunnerProtocol | typing.Callable[[], typing.Any] = DefaultTaskRunner
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tree_depth += 1

greeting += "This is very long, formal greeting for whomever is name here. Dear %s, it will break the line" % len(
name
)
19 changes: 17 additions & 2 deletions crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::FormatNodeRule;
use ruff_formatter::write;
Expand All @@ -18,11 +20,24 @@ impl FormatNodeRule<StmtAnnAssign> for FormatStmtAnnAssign {

write!(
f,
[target.format(), text(":"), space(), annotation.format()]
[
target.format(),
text(":"),
space(),
maybe_parenthesize_expression(annotation, item, Parenthesize::IfBreaks)
]
)?;

if let Some(value) = value {
write!(f, [space(), text("="), space(), value.format()])?;
write!(
f,
[
space(),
text("="),
space(),
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
]
)?;
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/
---
## Input
```py
tree_depth += 1
# Regression test: Don't forget the parentheses in the value when breaking
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int = a + 1 * a
greeting += "This is very long, formal greeting for whomever is name here. Dear %s, it will break the line" % len(
name
)
# Regression test: Don't forget the parentheses in the annotation when breaking
class DefaultRunner:
task_runner_cls: TaskRunnerProtocol | typing.Callable[[], typing.Any] = DefaultTaskRunner
```

## Output
```py
tree_depth += 1
greeting += (
"This is very long, formal greeting for whomever is name here. Dear %s, it will break the line"
% len(name)
# Regression test: Don't forget the parentheses in the value when breaking
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: int = (
a + 1 * a
)
# 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/aug_assign.py
---
## Input
```py
tree_depth += 1
greeting += "This is very long, formal greeting for whomever is name here. Dear %s, it will break the line" % len(
name
)
```

## Output
```py
tree_depth += 1
greeting += (
"This is very long, formal greeting for whomever is name here. Dear %s, it will break the line"
% len(name)
)
```



0 comments on commit 549173b

Please sign in to comment.