Skip to content

Commit

Permalink
Support decorators in source code generator (#2081)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Jan 22, 2023
1 parent d816203 commit c1cb479
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/source_code/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,18 @@ impl<'a> Generator<'a> {
args,
body,
returns,
decorator_list,
..
} => {
// TODO(charlie): Handle decorators.
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
statement!({
for decorator in decorator_list {
statement!({
self.p("@");
self.unparse_expr(decorator, precedence::EXPR);
});
}
self.newline();
self.p("def ");
self.p(name);
self.p("(");
Expand All @@ -168,11 +175,17 @@ impl<'a> Generator<'a> {
args,
body,
returns,
decorator_list,
..
} => {
// TODO(charlie): Handle decorators.
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
statement!({
for decorator in decorator_list {
statement!({
self.unparse_expr(decorator, precedence::EXPR);
});
}
self.newline();
self.p("async def ");
self.p(name);
self.p("(");
Expand All @@ -194,11 +207,17 @@ impl<'a> Generator<'a> {
bases,
keywords,
body,
decorator_list,
..
} => {
// TODO(charlie): Handle decorators.
self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
statement!({
for decorator in decorator_list {
statement!({
self.unparse_expr(decorator, precedence::EXPR);
});
}
self.newline();
self.p("class ");
self.p(name);
let mut first = true;
Expand Down Expand Up @@ -1140,7 +1159,11 @@ mod tests {
r#"def call(*popenargs, timeout=None, **kwargs):
pass"#
);

assert_round_trip!(
r#"@functools.lru_cache(maxsize=None)
def f(x: int, y: int) -> int:
return x + y"#
);
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"#);
}
Expand Down

0 comments on commit c1cb479

Please sign in to comment.