@@ -116,6 +116,13 @@ impl Unparser {
116116 self . source += str_
117117 }
118118
119+ fn write_type_comment ( & mut self , type_comment : & Option < String > ) {
120+ if let Some ( str_) = type_comment {
121+ self . write_str ( " # type: ignore" ) ;
122+ self . write_str ( str_) ;
123+ }
124+ }
125+
119126 fn block < F > ( & mut self , f : F )
120127 where
121128 F : FnOnce ( & mut Self ) ,
@@ -233,6 +240,7 @@ impl Unparser {
233240 self . unparse_expr ( & returns) ;
234241 }
235242 self . write_str ( ":" ) ;
243+ self . write_type_comment ( & node. type_comment ) ;
236244 self . block ( |block_self| {
237245 for value in & node. body {
238246 block_self. unparse_stmt ( & value) ;
@@ -268,6 +276,7 @@ impl Unparser {
268276 self . unparse_expr ( & returns) ;
269277 }
270278 self . write_str ( ":" ) ;
279+ self . write_type_comment ( & node. type_comment ) ;
271280 self . block ( |block_self| {
272281 for value in & node. body {
273282 block_self. unparse_stmt ( & value) ;
@@ -359,9 +368,7 @@ impl Unparser {
359368 }
360369 self . write_str ( " = " ) ;
361370 self . unparse_expr ( & node. value ) ;
362- if node. type_comment . is_some ( ) {
363- self . write_str ( & node. type_comment . as_ref ( ) . unwrap ( ) ) ;
364- }
371+ self . write_type_comment ( & node. type_comment ) ;
365372 }
366373
367374 fn unparse_stmt_type_alias ( & mut self , node : & StmtTypeAlias < TextRange > ) {
@@ -408,10 +415,7 @@ impl Unparser {
408415 self . write_str ( " in " ) ;
409416 self . unparse_expr ( & node. iter ) ;
410417 self . write_str ( ":" ) ;
411- if let Some ( type_comment) = & node. type_comment {
412- self . write_str ( " #type: ignore" ) ;
413- self . write_str ( type_comment) ;
414- }
418+ self . write_type_comment ( & node. type_comment ) ;
415419 self . block ( |block_self| {
416420 for value in & node. body {
417421 block_self. unparse_stmt ( value) ;
@@ -432,10 +436,7 @@ impl Unparser {
432436 self . write_str ( " in " ) ;
433437 self . unparse_expr ( & node. iter ) ;
434438 self . write_str ( ":" ) ;
435- if let Some ( type_comment) = & node. type_comment {
436- self . write_str ( " #type: ignore" ) ;
437- self . write_str ( type_comment) ;
438- }
439+ self . write_type_comment ( & node. type_comment ) ;
439440 self . block ( |block_self| {
440441 for value in & node. body {
441442 block_self. unparse_stmt ( value) ;
@@ -992,7 +993,8 @@ impl Unparser {
992993 match expr {
993994 Expr :: Constant ( ExprConstant { value, .. } ) => {
994995 if let Constant :: Str ( str_) = value {
995- inner_unparser. write_str ( str_) ;
996+ let escaped = str_. replace ( '{' , "{{" ) . replace ( '}' , "}}" ) ;
997+ inner_unparser. write_str ( & escaped) ;
996998 } else {
997999 unreachable ! ( )
9981000 }
@@ -1202,16 +1204,13 @@ impl Unparser {
12021204
12031205 if posonly_iter. peek ( ) . is_some ( ) {
12041206 self . write_str ( ", " ) ;
1205- } else if args_iter. peek ( ) . is_some ( )
1206- || node. vararg . is_some ( )
1207- || node. vararg . is_some ( )
1208- || kw_iter. peek ( ) . is_some ( )
1209- || node. kwarg . is_some ( )
1210- {
1211- self . write_str ( ", /, " ) ;
12121207 }
12131208 }
12141209
1210+ if node. posonlyargs . len ( ) > 0 {
1211+ self . write_str ( ", /," ) ;
1212+ }
1213+
12151214 while let Some ( arg) = args_iter. next ( ) {
12161215 self . unparse_arg ( arg. as_arg ( ) ) ;
12171216 if let Some ( default) = & arg. default {
@@ -1230,13 +1229,16 @@ impl Unparser {
12301229 if let Some ( vararg) = & node. vararg {
12311230 self . write_str ( "*" ) ;
12321231 self . write_str ( & vararg. arg ) ;
1232+
12331233 if let Some ( annotation) = & vararg. annotation {
12341234 self . write_str ( ": " ) ;
12351235 self . unparse_expr ( annotation) ;
12361236 }
12371237 if kw_iter. peek ( ) . is_some ( ) || node. kwarg . is_some ( ) {
12381238 self . write_str ( ", " ) ;
12391239 }
1240+ } else if node. kwonlyargs . len ( ) > 0 {
1241+ self . write_str ( "*, " ) ;
12401242 }
12411243
12421244 while let Some ( kw) = kw_iter. next ( ) {
0 commit comments