Skip to content

Commit

Permalink
Generator should add a newline before type statement (#11720)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
dhruvmanila authored Jun 3, 2024
1 parent a58bde6 commit 8db147c
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions crates/ruff_python_codegen/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 8db147c

Please sign in to comment.