Skip to content

Empty new line added after docstring when using --line-ranges #5285

Description

@ddeepwell

Describe the bug

Based on the discussion in this issue, Black has no opinion on whether a blank line should or should not exist after a docstring. Both are permitted. However, Black will add an empty new line after a docstring when using --line-ranges when the lines are within the lines containing the docstring.

To Reproduce

For example, take this code in "example.py":

def function():
    """This is a docstring."""
    return "Hi There!"

And run it with these arguments:

$ black --line-ranges 2-2 example.py

"example.py" is changed to:

def function():
    """This is a docstring."""

    return "Hi There!"

Expected behavior

I expect no change to the original file.

Environment

  • Black's version: black, 26.5.2.dev73+gc7801d9e2
  • OS and Python version: MacOS/Python 3.14.6

Additional context

This unexpected behavior was introduced in version 26.1.0 (and earlier in preview mode, but I didn't check when that occured).

I notice this error because the VS Code Black Formatter Extension often used the --line-ranges option. I notice it a lot when pasting text into a docstring because I have configured VS Code to format-on-paste.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions