From 8db147c09d4a3c0621077858f3c6154bf8215461 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 3 Jun 2024 18:44:21 +0530 Subject: [PATCH] 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. --- crates/ruff_python_codegen/src/generator.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/ruff_python_codegen/src/generator.rs b/crates/ruff_python_codegen/src/generator.rs index 9cb98dd7c6..01ff10a9ab 100644 --- a/crates/ruff_python_codegen/src/generator.rs +++ b/crates/ruff_python_codegen/src/generator.rs @@ -482,13 +482,15 @@ impl<'a> Generator<'a> { type_params, value, }) => { - self.p("type "); - self.unparse_expr(name, precedence::MAX); - if let Some(type_params) = type_params { - self.unparse_type_params(type_params); - } - self.p(" = "); - self.unparse_expr(value, precedence::ASSIGN); + statement!({ + self.p("type "); + self.unparse_expr(name, precedence::MAX); + if let Some(type_params) = type_params { + self.unparse_type_params(type_params); + } + self.p(" = "); + self.unparse_expr(value, precedence::ASSIGN); + }); } Stmt::Raise(ast::StmtRaise { exc, @@ -1634,6 +1636,10 @@ except* Exception as e: return 2 case 4 as y: return y" + ); + assert_round_trip!( + r"type X = int +type Y = str" ); 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");