@@ -276,8 +276,8 @@ impl Format<PyFormatContext<'_>> for FormatBody<'_> {
276276 let body_comments = comments. leading_dangling_trailing ( body) ;
277277
278278 if !dangling_header_comments. is_empty ( ) {
279- // Can't use partition_point because there can be additional end of line comments
280- // after the initial set. All of these comments are dangling, for example:
279+ // Split the dangling header comments into trailing comments formatted with the lambda
280+ // header (1) and leading comments formatted with the body (2, 3, 4).
281281 //
282282 // ```python
283283 // (
@@ -289,8 +289,9 @@ impl Format<PyFormatContext<'_>> for FormatBody<'_> {
289289 // )
290290 // ```
291291 //
292- // and alternate between own line and end of line.
293- let ( after_parameters_end_of_line, leading_body_comments) = dangling_header_comments
292+ // Note that these are split based on their line position rather than using
293+ // `partition_point` based on a range, for example.
294+ let ( trailing_header_comments, leading_body_comments) = dangling_header_comments
294295 . split_at (
295296 dangling_header_comments
296297 . iter ( )
@@ -331,7 +332,8 @@ impl Format<PyFormatContext<'_>> for FormatBody<'_> {
331332 trailing_comments ( dangling_header_comments) . fmt ( f) ?;
332333
333334 // Note that `leading_body_comments` have already been formatted as part of
334- // `dangling` above, but their presence still determines the spacing here.
335+ // `dangling_header_comments` above, but their presence still determines the spacing
336+ // here.
335337 if leading_body_comments. is_empty ( ) {
336338 space ( ) . fmt ( f) ?;
337339 } else {
@@ -345,7 +347,7 @@ impl Format<PyFormatContext<'_>> for FormatBody<'_> {
345347 [
346348 space( ) ,
347349 token( "(" ) ,
348- trailing_comments( after_parameters_end_of_line ) ,
350+ trailing_comments( trailing_header_comments ) ,
349351 block_indent( & format_args!(
350352 leading_comments( leading_body_comments) ,
351353 body. format( ) . with_options( Parentheses :: Never )
0 commit comments