@@ -19,7 +19,7 @@ use crate::verbatim::{
19
19
} ;
20
20
21
21
/// Level at which the [`Suite`] appears in the source code.
22
- #[ derive( Copy , Clone , Debug , Default ) ]
22
+ #[ derive( Copy , Clone , Debug , Default , PartialEq , Eq ) ]
23
23
pub enum SuiteKind {
24
24
/// Statements at the module level / top level
25
25
TopLevel ,
@@ -123,7 +123,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
123
123
124
124
let first_comments = comments. leading_dangling_trailing ( first) ;
125
125
126
- let ( mut preceding, mut after_class_docstring ) = if first_comments
126
+ let ( mut preceding, mut empty_line_after_docstring ) = if first_comments
127
127
. leading
128
128
. iter ( )
129
129
. any ( |comment| comment. is_suppression_off_comment ( source) )
@@ -143,11 +143,24 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
143
143
)
144
144
} else {
145
145
first. fmt ( f) ?;
146
- (
147
- first. statement ( ) ,
148
- matches ! ( first, SuiteChildStatement :: Docstring ( _) )
149
- && matches ! ( self . kind, SuiteKind :: Class ) ,
150
- )
146
+
147
+ #[ allow( clippy:: if_same_then_else) ]
148
+ let empty_line_after_docstring = if matches ! ( first, SuiteChildStatement :: Docstring ( _) )
149
+ && self . kind == SuiteKind :: Class
150
+ {
151
+ true
152
+ } else if f. options ( ) . preview ( ) . is_enabled ( )
153
+ && self . kind == SuiteKind :: TopLevel
154
+ && DocstringStmt :: try_from_statement ( first. statement ( ) ) . is_some ( )
155
+ {
156
+ // Only in preview mode, insert a newline after a module level docstring, but treat
157
+ // it as a docstring otherwise. See: https://github.com/psf/black/pull/3932.
158
+ true
159
+ } else {
160
+ false
161
+ } ;
162
+
163
+ ( first. statement ( ) , empty_line_after_docstring)
151
164
} ;
152
165
153
166
let mut preceding_comments = comments. leading_dangling_trailing ( preceding) ;
@@ -303,7 +316,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
303
316
}
304
317
} ,
305
318
}
306
- } else if after_class_docstring {
319
+ } else if empty_line_after_docstring {
307
320
// Enforce an empty line after a class docstring, e.g., these are both stable
308
321
// formatting:
309
322
// ```python
@@ -389,7 +402,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
389
402
preceding_comments = following_comments;
390
403
}
391
404
392
- after_class_docstring = false ;
405
+ empty_line_after_docstring = false ;
393
406
}
394
407
395
408
Ok ( ( ) )
@@ -547,9 +560,11 @@ impl<'a> DocstringStmt<'a> {
547
560
} ;
548
561
549
562
if let Expr :: Constant ( ExprConstant { value, .. } ) = value. as_ref ( ) {
550
- if !value. is_implicit_concatenated ( ) {
551
- return Some ( DocstringStmt ( stmt) ) ;
552
- }
563
+ return match value {
564
+ Constant :: Str ( value) if !value. implicit_concatenated => Some ( DocstringStmt ( stmt) ) ,
565
+ Constant :: Bytes ( value) if !value. implicit_concatenated => Some ( DocstringStmt ( stmt) ) ,
566
+ _ => None ,
567
+ } ;
553
568
}
554
569
555
570
None
0 commit comments