diff --git a/crates/ruff_python_formatter/src/statement/suite.rs b/crates/ruff_python_formatter/src/statement/suite.rs index efcf6eff3d75b3..42756894763434 100644 --- a/crates/ruff_python_formatter/src/statement/suite.rs +++ b/crates/ruff_python_formatter/src/statement/suite.rs @@ -140,7 +140,13 @@ impl FormatRule> for FormatSuite { SuiteChildStatement::Other(first) } } - SuiteKind::TopLevel => SuiteChildStatement::Other(first), + SuiteKind::TopLevel => { + if let Some(docstring) = DocstringStmt::try_from_statement(first, self.kind) { + SuiteChildStatement::Docstring(docstring) + } else { + SuiteChildStatement::Other(first) + } + } }; let first_comments = comments.leading_dangling_trailing(first);