Skip to content

D202: Conflict with black's 2023 style #2592

@ngnpope

Description

@ngnpope

For the following code:

def outer():
    """This is a docstring."""
    # This is a comment.
    def inner():
        return

    return inner

I see the following behaviour:

ruff --version
ruff 0.0.240ruff --select D202 bug.py
# ... no output ...black --version
black, 23.1.0 (compiled: yes)
Python (CPython) 3.10.9black --diff bug.py 
--- bug.py	2023-02-05 21:19:16.413892 +0000
+++ bug.py	2023-02-05 21:19:22.497046 +0000
@@ -1,7 +1,8 @@
 def outer():
     """This is a docstring."""
+
     # This is a comment.
     def inner():
         return
 
     return inner()
would reformat bug.py

All done! ✨ 🍰 ✨
1 file would be reformatted.black bug.py 
reformatted bug.py

All done! ✨ 🍰 ✨
1 file reformatted.ruff --select D202 bug.py 
bug.py:2:5: D202 No blank lines allowed after function docstring (found 1)
Found 1 error.
1 potentially fixable with the --fix option.

It seems that this works fine if the comment is removed. Black wants a blank line before and after a nested function. But black treats comment lines before the function as "attached" to the function. This doesn't happen with the D202 rule in ruff which treats them as separate. We should follow black's lead here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdocstringRelated to docstring linting or formatting

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions