@@ -789,11 +789,15 @@ impl Unparser {
789
789
fn unparse_expr_if_exp ( & mut self , node : & ExprIfExp < TextRange > ) {
790
790
let enum_member = Expr :: IfExp ( node. to_owned ( ) ) ;
791
791
self . delimit_precedence ( & enum_member, |block_self| {
792
- block_self. unparse_expr ( & node. body ) ;
793
- block_self. write_str ( " if " ) ;
794
- block_self. unparse_expr ( & node. test ) ;
795
- block_self. write_str ( " else " ) ;
796
- block_self. unparse_expr ( & node. orelse ) ;
792
+ block_self. with_precedence_num ( Precedence :: Test . value ( ) + 1 , |prec_self| {
793
+ prec_self. unparse_expr ( & node. body ) ;
794
+ prec_self. write_str ( " if " ) ;
795
+ prec_self. unparse_expr ( & node. test ) ;
796
+ } ) ;
797
+ block_self. with_precedence ( Precedence :: Test , |prec_self| {
798
+ prec_self. write_str ( " else " ) ;
799
+ prec_self. unparse_expr ( & node. orelse ) ;
800
+ } ) ;
797
801
} )
798
802
}
799
803
@@ -811,7 +815,10 @@ impl Unparser {
811
815
self . write_str ( "**" ) ;
812
816
}
813
817
}
814
- self . unparse_expr ( value) ;
818
+ self . with_precedence_num ( EXPR_PRECEDENCE , |prec_self| {
819
+ prec_self. unparse_expr ( value) ;
820
+ } ) ;
821
+
815
822
if zipped. peek ( ) . is_some ( ) {
816
823
self . write_str ( ", " ) ;
817
824
}
@@ -1173,14 +1180,21 @@ impl Unparser {
1173
1180
1174
1181
fn unparse_expr_tuple ( & mut self , node : & ExprTuple < TextRange > ) {
1175
1182
let mut elts_iter = node. elts . iter ( ) . peekable ( ) ;
1176
- self . write_str ( "(" ) ;
1183
+ let should_delimit =
1184
+ node. elts . len ( ) == 0 || self . precedence_level > Precedence :: Tuple . value ( ) ;
1185
+ if should_delimit {
1186
+ self . write_str ( "(" ) ;
1187
+ }
1188
+
1177
1189
while let Some ( expr) = elts_iter. next ( ) {
1178
1190
self . unparse_expr ( expr) ;
1179
1191
if elts_iter. peek ( ) . is_some ( ) || node. elts . len ( ) == 1 {
1180
1192
self . write_str ( ", " ) ;
1181
1193
}
1182
1194
}
1183
- self . write_str ( ")" ) ;
1195
+ if should_delimit {
1196
+ self . write_str ( ")" ) ;
1197
+ }
1184
1198
}
1185
1199
1186
1200
fn unparse_expr_slice ( & mut self , node : & ExprSlice < TextRange > ) {
@@ -1221,13 +1235,19 @@ impl Unparser {
1221
1235
} else {
1222
1236
self . write_str ( " for " ) ;
1223
1237
}
1224
- self . unparse_expr ( & node. target ) ;
1238
+ self . with_precedence ( Precedence :: Tuple , |prec_self| {
1239
+ prec_self. unparse_expr ( & node. target ) ;
1240
+ } ) ;
1241
+
1225
1242
self . write_str ( " in " ) ;
1226
- self . unparse_expr ( & node. iter ) ;
1227
- for if_ in & node. ifs {
1228
- self . write_str ( " if " ) ;
1229
- self . unparse_expr ( if_) ;
1230
- }
1243
+
1244
+ self . with_precedence_num ( Precedence :: Test . value ( ) + 1 , |prec_self| {
1245
+ prec_self. unparse_expr ( & node. iter ) ;
1246
+ for if_ in & node. ifs {
1247
+ prec_self. write_str ( " if " ) ;
1248
+ prec_self. unparse_expr ( if_) ;
1249
+ }
1250
+ } ) ;
1231
1251
}
1232
1252
1233
1253
fn unparse_excepthandler ( & mut self , node : & ExceptHandler < TextRange > ) {
0 commit comments