Skip to content

Commit

Permalink
Fix round-tripping of nested functions (#4875)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Jun 5, 2023
1 parent 913b9d1 commit d1b8fe6
Showing 1 changed file with 41 additions and 19 deletions.
60 changes: 41 additions & 19 deletions crates/ruff_python_ast/src/source_code/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,13 @@ impl<'a> Generator<'a> {
..
}) => {
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
for decorator in decorator_list {
statement!({
self.p("@");
self.unparse_expr(decorator, precedence::MAX);
});
}
statement!({
for decorator in decorator_list {
statement!({
self.p("@");
self.unparse_expr(decorator, precedence::MAX);
});
}
self.newline();
self.p("def ");
self.p_id(name);
self.p("(");
Expand All @@ -242,13 +241,13 @@ impl<'a> Generator<'a> {
..
}) => {
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
for decorator in decorator_list {
statement!({
self.p("@");
self.unparse_expr(decorator, precedence::MAX);
});
}
statement!({
for decorator in decorator_list {
statement!({
self.unparse_expr(decorator, precedence::MAX);
});
}
self.newline();
self.p("async def ");
self.p_id(name);
self.p("(");
Expand All @@ -274,13 +273,13 @@ impl<'a> Generator<'a> {
range: _range,
}) => {
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
for decorator in decorator_list {
statement!({
self.p("@");
self.unparse_expr(decorator, precedence::MAX);
});
}
statement!({
for decorator in decorator_list {
statement!({
self.unparse_expr(decorator, precedence::MAX);
});
}
self.newline();
self.p("class ");
self.p_id(name);
let mut first = true;
Expand Down Expand Up @@ -1614,6 +1613,29 @@ except* Exception as e:
);
assert_eq!(round_trip(r#"x = (1, 2, 3)"#), r#"x = 1, 2, 3"#);
assert_eq!(round_trip(r#"-(1) + ~(2) + +(3)"#), r#"-1 + ~2 + +3"#);
assert_round_trip!(
r#"def f():
def f():
pass"#
);
assert_round_trip!(
r#"@foo
def f():
@foo
def f():
pass"#
);

assert_round_trip!(
r#"@foo
class Foo:
@foo
def f():
pass"#
);
}

#[test]
Expand Down

0 comments on commit d1b8fe6

Please sign in to comment.