Skip to content

Commit

Permalink
Format StmtAugAssign (#5655)
Browse files Browse the repository at this point in the history
## Summary

Format statements such as `tree_depth += 1`. This is a statement that
does not allow any line breaks, the only thing to be mindful of is to
parenthesize the assigned expression

Jaccard index on django: 0.915 -> 0.918

## Test Plan

black tests, and two new tests, a basic one and one that ensures that
the child gets parentheses. I ran the django stability check.
  • Loading branch information
konstin authored Jul 11, 2023
1 parent 15c7b6b commit b7794f8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 27 deletions.
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
)
22 changes: 20 additions & 2 deletions crates/ruff_python_formatter/src/statement/stmt_aug_assign.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::expression::parentheses::Parenthesize;
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{space, text};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::StmtAugAssign;

Expand All @@ -7,6 +9,22 @@ pub struct FormatStmtAugAssign;

impl FormatNodeRule<StmtAugAssign> for FormatStmtAugAssign {
fn fmt_fields(&self, item: &StmtAugAssign, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
let StmtAugAssign {
target,
op,
value,
range: _,
} = item;
write!(
f,
[
target.format(),
space(),
op.format(),
text("="),
space(),
value.format().with_options(Parenthesize::IfBreaks)
]
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,14 @@ class DebugVisitor(Visitor[T]):
if isinstance(node, Node):
_type = type_repr(node.type)
- out(f'{indent}{_type}', fg='yellow')
- self.tree_depth += 1
+ out(NOT_YET_IMPLEMENTED_ExprJoinedStr, fg="yellow")
+ NOT_YET_IMPLEMENTED_StmtAugAssign
self.tree_depth += 1
for child in node.children:
- yield from self.visit(child)
+ NOT_YET_IMPLEMENTED_ExprYieldFrom
- self.tree_depth -= 1
self.tree_depth -= 1
- out(f'{indent}/{_type}', fg='yellow', bold=False)
+ NOT_YET_IMPLEMENTED_StmtAugAssign
+ out(NOT_YET_IMPLEMENTED_ExprJoinedStr, fg="yellow", bold=False)
else:
_type = token.tok_name.get(node.type, str(node.type))
Expand Down Expand Up @@ -102,11 +100,11 @@ class DebugVisitor(Visitor[T]):
if isinstance(node, Node):
_type = type_repr(node.type)
out(NOT_YET_IMPLEMENTED_ExprJoinedStr, fg="yellow")
NOT_YET_IMPLEMENTED_StmtAugAssign
self.tree_depth += 1
for child in node.children:
NOT_YET_IMPLEMENTED_ExprYieldFrom
NOT_YET_IMPLEMENTED_StmtAugAssign
self.tree_depth -= 1
out(NOT_YET_IMPLEMENTED_ExprJoinedStr, fg="yellow", bold=False)
else:
_type = token.tok_name.get(node.type, str(node.type))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,6 @@ long_unmergable_string_with_pragma = (
```diff
--- Black
+++ Ruff
@@ -1,6 +1,6 @@
x = "This is a really long string that can't possibly be expected to fit all together on one line. In fact it may even take up three or more lines... like four or five... but probably just three."

-x += "This is a really long string that can't possibly be expected to fit all together on one line. In fact it may even take up three or more lines... like four or five... but probably just three."
+NOT_YET_IMPLEMENTED_StmtAugAssign

y = "Short string"

@@ -70,8 +70,8 @@
bad_split3 = (
"What if we have inline comments on " # First Comment
Expand Down Expand Up @@ -471,7 +463,7 @@ long_unmergable_string_with_pragma = (
```py
x = "This is a really long string that can't possibly be expected to fit all together on one line. In fact it may even take up three or more lines... like four or five... but probably just three."

NOT_YET_IMPLEMENTED_StmtAugAssign
x += "This is a really long string that can't possibly be expected to fit all together on one line. In fact it may even take up three or more lines... like four or five... but probably just three."

y = "Short string"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,6 @@ with match() as match:
# At least one of the above branches must have been taken, because every Python
# version has exactly one of the two 'ASYNC_*' flags
@@ -91,7 +83,7 @@
def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node:
"""Given a string with source, return the lib2to3 Node."""
if not src_txt.endswith("\n"):
- src_txt += "\n"
+ NOT_YET_IMPLEMENTED_StmtAugAssign
grammars = get_grammars(set(target_versions))
@@ -99,9 +91,9 @@
re.match()
match = a
Expand Down Expand Up @@ -298,7 +289,7 @@ def get_grammars(target_versions: Set[TargetVersion]) -> List[Grammar]:
def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -> Node:
"""Given a string with source, return the lib2to3 Node."""
if not src_txt.endswith("\n"):
NOT_YET_IMPLEMENTED_StmtAugAssign
src_txt += "\n"
grammars = get_grammars(set(target_versions))
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/ann_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 b7794f8

Please sign in to comment.