Skip to content

Commit

Permalink
No newline after function docstrings
Browse files Browse the repository at this point in the history
Fixup for #8216 to not apply to function docstrings.

Main before #8216:

| project        | similarity index  | total files       | changed files     |
|----------------|------------------:|------------------:|------------------:|
| cpython        |           0.75804 |              1799 |              1648 |
| django         |           0.99984 |              2772 |                33 |
| home-assistant |           0.99963 |             10596 |               148 |
| poetry         |           0.99925 |               317 |                12 |
| transformers   |           0.99967 |              2657 |               328 |
| twine          |           1.00000 |                33 |                 0 |
| typeshed       |           0.99980 |              3669 |                18 |
| warehouse      |           0.99977 |               654 |                13 |
| zulip          |           0.99970 |              1459 |                22 |

main now:

| project        | similarity index  | total files       | changed files     |
|----------------|------------------:|------------------:|------------------:|
| cpython        |           0.75804 |              1799 |              1648 |
| django         |           0.99984 |              2772 |                48 |
| home-assistant |           0.99963 |             10596 |               181 |
| poetry         |           0.99925 |               317 |                12 |
| transformers   |           0.99967 |              2657 |               339 |
| twine          |           1.00000 |                33 |                 0 |
| typeshed       |           0.99980 |              3669 |                18 |
| warehouse      |           0.99977 |               654 |                13 |
| zulip          |           0.99970 |              1459 |                23 |

PR:

| project        | similarity index  | total files       | changed files     |
|----------------|------------------:|------------------:|------------------:|
| cpython        |           0.75804 |              1799 |              1648 |
| django         |           0.99984 |              2772 |                33 |
| home-assistant |           0.99963 |             10596 |               148 |
| poetry         |           0.99925 |               317 |                12 |
| transformers   |           0.99967 |              2657 |               328 |
| twine          |           1.00000 |                33 |                 0 |
| typeshed       |           0.99980 |              3669 |                18 |
| warehouse      |           0.99977 |               654 |                13 |
| zulip          |           0.99970 |              1459 |                22 |
  • Loading branch information
konstin committed Oct 31, 2023
1 parent 982ae6f commit f30631c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ class CommentAfterDocstring5:
# This class is also the base class for pathbrowser.PathBrowser.


def f():
"""Browse module classes and functions in IDLE."""
# ^ Do not insert a newline above here

pass


class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down
36 changes: 19 additions & 17 deletions crates/ruff_python_formatter/src/statement/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,23 +587,25 @@ impl Format<PyFormatContext<'_>> for DocstringStmt<'_> {
]
)?;

// Comments after docstrings need a newline between the docstring and the comment.
// (https://github.com/astral-sh/ruff/issues/7948)
// ```python
// class ModuleBrowser:
// """Browse module classes and functions in IDLE."""
// # ^ Insert a newline above here
//
// def __init__(self, master, path, *, _htest=False, _utest=False):
// pass
// ```
if let Some(own_line) = node_comments
.trailing
.iter()
.find(|comment| comment.line_position().is_own_line())
{
if lines_before(own_line.start(), f.context().source()) < 2 {
empty_line().fmt(f)?;
if matches!(self.0, Stmt::ClassDef(_)) {
// Comments after docstrings need a newline between the docstring and the comment.
// (https://github.com/astral-sh/ruff/issues/7948)
// ```python
// class ModuleBrowser:
// """Browse module classes and functions in IDLE."""
// # ^ Insert a newline above here
//
// def __init__(self, master, path, *, _htest=False, _utest=False):
// pass
// ```
if let Some(own_line) = node_comments
.trailing
.iter()
.find(|comment| comment.line_position().is_own_line())
{
if lines_before(own_line.start(), f.context().source()) < 2 {
empty_line().fmt(f)?;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,8 @@ d={'a':1,
# fmt: on
goes + here,
andhere,
@@ -120,10 +121,13 @@
The comments between will be formatted. This is a known limitation.
@@ -122,8 +123,10 @@
"""
+
# fmt: off
- # hey, that won't work
Expand All @@ -240,7 +237,7 @@ d={'a':1,
# fmt: on
pass
@@ -138,7 +142,7 @@
@@ -138,7 +141,7 @@
now . considers . multiple . fmt . directives . within . one . prefix
# fmt: on
# fmt: off
Expand All @@ -249,7 +246,7 @@ d={'a':1,
# fmt: on
@@ -178,14 +182,18 @@
@@ -178,14 +181,18 @@
$
""",
# fmt: off
Expand Down Expand Up @@ -398,7 +395,6 @@ def off_and_on_without_data():
The comments between will be formatted. This is a known limitation.
"""
# fmt: off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ class CommentAfterDocstring5:
# This class is also the base class for pathbrowser.PathBrowser.
def f():
"""Browse module classes and functions in IDLE."""
# ^ Do not insert a newline above here
pass
class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down Expand Up @@ -263,7 +270,6 @@ class ByteDocstring:
class CommentAfterDocstring1:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
Expand Down Expand Up @@ -297,10 +303,16 @@ class CommentAfterDocstring4:
class CommentAfterDocstring5:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def f():
"""Browse module classes and functions in IDLE."""
# ^ Do not insert a newline above here
pass
class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down Expand Up @@ -422,7 +434,6 @@ class ByteDocstring:
class CommentAfterDocstring1:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
Expand Down Expand Up @@ -456,10 +467,16 @@ class CommentAfterDocstring4:
class CommentAfterDocstring5:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def f():
"""Browse module classes and functions in IDLE."""
# ^ Do not insert a newline above here
pass
class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down Expand Up @@ -581,7 +598,6 @@ class ByteDocstring:
class CommentAfterDocstring1:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
Expand Down Expand Up @@ -615,10 +631,16 @@ class CommentAfterDocstring4:
class CommentAfterDocstring5:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def f():
"""Browse module classes and functions in IDLE."""
# ^ Do not insert a newline above here
pass
class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down Expand Up @@ -740,7 +762,6 @@ class ByteDocstring:
class CommentAfterDocstring1:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def __init__(self):
Expand Down Expand Up @@ -774,10 +795,16 @@ class CommentAfterDocstring4:
class CommentAfterDocstring5:
"""Browse module classes and functions in IDLE."""
# This class is also the base class for pathbrowser.PathBrowser.
def f():
"""Browse module classes and functions in IDLE."""
# ^ Do not insert a newline above here
pass
class TabbedIndent:
def tabbed_indent(self):
"""check for correct tabbed formatting
Expand Down

0 comments on commit f30631c

Please sign in to comment.