Skip to content

Commit 8db147c

Browse files
authored
Generator should add a newline before type statement (#11720)
## Summary This PR fixes a bug where the `Generator` wouldn't add a newline before a type alias statement. This is because it wasn't using the `statement` macro which takes care of the newline. Without this fix, a code like: ```py type X = int type Y = str ``` The generator would produce: ```py type X = inttype Y = str ``` ## Test Plan Add a test case.
1 parent a58bde6 commit 8db147c

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

crates/ruff_python_codegen/src/generator.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,15 @@ impl<'a> Generator<'a> {
482482
type_params,
483483
value,
484484
}) => {
485-
self.p("type ");
486-
self.unparse_expr(name, precedence::MAX);
487-
if let Some(type_params) = type_params {
488-
self.unparse_type_params(type_params);
489-
}
490-
self.p(" = ");
491-
self.unparse_expr(value, precedence::ASSIGN);
485+
statement!({
486+
self.p("type ");
487+
self.unparse_expr(name, precedence::MAX);
488+
if let Some(type_params) = type_params {
489+
self.unparse_type_params(type_params);
490+
}
491+
self.p(" = ");
492+
self.unparse_expr(value, precedence::ASSIGN);
493+
});
492494
}
493495
Stmt::Raise(ast::StmtRaise {
494496
exc,
@@ -1634,6 +1636,10 @@ except* Exception as e:
16341636
return 2
16351637
case 4 as y:
16361638
return y"
1639+
);
1640+
assert_round_trip!(
1641+
r"type X = int
1642+
type Y = str"
16371643
);
16381644
assert_eq!(round_trip(r"x = (1, 2, 3)"), r"x = 1, 2, 3");
16391645
assert_eq!(round_trip(r"-(1) + ~(2) + +(3)"), r"-1 + ~2 + +3");

0 commit comments

Comments
 (0)