Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add newline after module docstrings in preview style #8283

Merged
merged 3 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions crates/ruff_python_formatter/src/statement/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::verbatim::{
};

/// Level at which the [`Suite`] appears in the source code.
#[derive(Copy, Clone, Debug, Default)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum SuiteKind {
/// Statements at the module level / top level
TopLevel,
Expand Down Expand Up @@ -123,7 +123,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {

let first_comments = comments.leading_dangling_trailing(first);

let (mut preceding, mut after_class_docstring) = if first_comments
let (mut preceding, mut empty_line_after_docstring) = if first_comments
.leading
.iter()
.any(|comment| comment.is_suppression_off_comment(source))
Expand All @@ -143,11 +143,24 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
)
} else {
first.fmt(f)?;
(
first.statement(),
matches!(first, SuiteChildStatement::Docstring(_))
&& matches!(self.kind, SuiteKind::Class),
)

#[allow(clippy::if_same_then_else)]
let empty_line_after_docstring = if matches!(first, SuiteChildStatement::Docstring(_))
&& self.kind == SuiteKind::Class
{
true
} else if f.options().preview().is_enabled()
&& self.kind == SuiteKind::TopLevel
&& DocstringStmt::try_from_statement(first.statement()).is_some()
{
// Only in preview mode, insert a newline after a module level docstring, but treat
// it as a docstring otherwise. See: https://github.com/psf/black/pull/3932.
true
} else {
false
};

(first.statement(), empty_line_after_docstring)
};

let mut preceding_comments = comments.leading_dangling_trailing(preceding);
Expand Down Expand Up @@ -303,7 +316,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
}
},
}
} else if after_class_docstring {
} else if empty_line_after_docstring {
// Enforce an empty line after a class docstring, e.g., these are both stable
// formatting:
// ```python
Expand Down Expand Up @@ -389,7 +402,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
preceding_comments = following_comments;
}

after_class_docstring = false;
empty_line_after_docstring = false;
}

Ok(())
Expand Down Expand Up @@ -547,9 +560,11 @@ impl<'a> DocstringStmt<'a> {
};

if let Expr::Constant(ExprConstant { value, .. }) = value.as_ref() {
if !value.is_implicit_concatenated() {
return Some(DocstringStmt(stmt));
}
return match value {
Constant::Str(value) if !value.implicit_concatenated => Some(DocstringStmt(stmt)),
Constant::Bytes(value) if !value.implicit_concatenated => Some(DocstringStmt(stmt)),
_ => None,
};
Comment on lines +563 to +567
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think bytes can be used as docstrings, right?

$ cat src/play.py                                           
b"docstring"

$ python
Python 3.11.3 (main, Apr 19 2023, 22:45:48) [Clang 16.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from src import play
>>> play.__doc__
>>> 

$ cat src/play.py
"docstring"

$ python
Python 3.11.3 (main, Apr 19 2023, 22:45:48) [Clang 16.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from src import play
>>> play.__doc__
'docstring'
>>> 

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making double check! psf/black#4002

}

None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ preview = Enabled
"""
Black's `Preview.module_docstring_newlines`
"""

first_stmt_after_module_level_docstring = 1


Expand Down
Loading