Support decorators in source code generator (#2081)

This commit is contained in:
Charlie Marsh 2023-01-21 23:26:32 -05:00 committed by GitHub
parent d81620397e
commit c1cb4796f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -142,11 +142,18 @@ impl<'a> Generator<'a> {
args, args,
body, body,
returns, returns,
decorator_list,
.. ..
} => { } => {
// TODO(charlie): Handle decorators.
self.newlines(if self.indent_depth == 0 { 2 } else { 1 }); self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
statement!({ statement!({
for decorator in decorator_list {
statement!({
self.p("@");
self.unparse_expr(decorator, precedence::EXPR);
});
}
self.newline();
self.p("def "); self.p("def ");
self.p(name); self.p(name);
self.p("("); self.p("(");
@ -168,11 +175,17 @@ impl<'a> Generator<'a> {
args, args,
body, body,
returns, returns,
decorator_list,
.. ..
} => { } => {
// TODO(charlie): Handle decorators.
self.newlines(if self.indent_depth == 0 { 2 } else { 1 }); self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
statement!({ statement!({
for decorator in decorator_list {
statement!({
self.unparse_expr(decorator, precedence::EXPR);
});
}
self.newline();
self.p("async def "); self.p("async def ");
self.p(name); self.p(name);
self.p("("); self.p("(");
@ -194,11 +207,17 @@ impl<'a> Generator<'a> {
bases, bases,
keywords, keywords,
body, body,
decorator_list,
.. ..
} => { } => {
// TODO(charlie): Handle decorators.
self.newlines(if self.indent_depth == 0 { 2 } else { 1 }); self.newlines(if self.indent_depth == 0 { 2 } else { 1 });
statement!({ statement!({
for decorator in decorator_list {
statement!({
self.unparse_expr(decorator, precedence::EXPR);
});
}
self.newline();
self.p("class "); self.p("class ");
self.p(name); self.p(name);
let mut first = true; let mut first = true;
@ -1140,7 +1159,11 @@ mod tests {
r#"def call(*popenargs, timeout=None, **kwargs): r#"def call(*popenargs, timeout=None, **kwargs):
pass"# 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#"x = (1, 2, 3)"#), r#"x = 1, 2, 3"#);
assert_eq!(round_trip(r#"-(1) + ~(2) + +(3)"#), r#"-1 + ~2 + +3"#); assert_eq!(round_trip(r#"-(1) + ~(2) + +(3)"#), r#"-1 + ~2 + +3"#);
} }