Skip to content

Conversation

@finswimmer
Copy link
Contributor

@finswimmer finswimmer commented Nov 26, 2025

This PR fixes a couple of regressions regarding handling line breaks and empty line.

  • If the docstring e.g. for a class is the last thing in the file, don't add any empty lines after it
  • If a class or method only contains a docstring, correctly insert empty lines after it
  • Treat ... as code lines, so that empty line handling works correct
  • Introduce a workaround to detect f-strings in Python < 3.12
  • Fixed an issue that arises when line-endings with \r\n are used

Closes: #328

@adamtheturtle
Copy link

Thank you @finswimmer , with --black this removes most inconsistencies with black formatting in my project.

One issue remains in my project - when a class is preceded by a comment, black (and docformatter 1.7.7) thinks that there should be two newlines before that comment, but with this PR, docformatter places only one newline before that comment.

Demonstration script:

#!/usr/bin/env bash
set -euo pipefail

DOCFORMATTER_STABLE="docformatter==1.7.7"
DOCFORMATTER_FINSWIMMER_REF="git+https://github.com/finswimmer/docformatter@fea4116e01435bfcce31de936f8549b1b64ca025"

ORIGINAL_CONTENT=$'class A:\n    """Hello."""\n\n\n# Some comment\nclass B:\n    """Hello."""\n'

echo "Initial content:"
printf '%s' "$ORIGINAL_CONTENT"
echo "------"

echo
echo "docformatter stable (${DOCFORMATTER_STABLE}):"
printf '%s' "$ORIGINAL_CONTENT" | uvx --python=3.13 --from "${DOCFORMATTER_STABLE}" docformatter --black -
echo "------"

echo
echo "docformatter finswimmer (${DOCFORMATTER_FINSWIMMER_REF}):"
printf '%s' "$ORIGINAL_CONTENT" | uvx --python=3.13 --from "${DOCFORMATTER_FINSWIMMER_REF}" docformatter --black -
echo "------"

Output:

Initial content:
class A:
    """Hello."""


# Some comment
class B:
    """Hello."""
------

docformatter stable (docformatter==1.7.7):
class A:
    """Hello."""


# Some comment
class B:
    """Hello."""
------

docformatter finswimmer (git+https://github.com/finswimmer/docformatter@fea4116e01435bfcce31de936f8549b1b64ca025):
class A:
    """Hello."""

# Some comment
class B:
    """Hello."""
------

@finswimmer
Copy link
Contributor Author

Thanks a lot for the feedback @adamtheturtle Can you try again please?

@adamtheturtle
Copy link

Thank you @finswimmer . The next issues I've hit are demonstrated here.

Failure with CRLF

This errors on this branch.

#!/usr/bin/env bash
set -euo pipefail

DOCFORMATTER_STABLE="docformatter==1.7.7"
DOCFORMATTER_FINSWIMMER_REF="git+https://github.com/finswimmer/docformatter@948c28a5d43106bad3ec796fdf26f87cbdaee085"

ORIGINAL_CONTENT=$'"""\nA\n"""\n\npass\n\npass\n'
CRLF_CONTENT=$(printf '%s' "$ORIGINAL_CONTENT" | sed 's/$/\r/')

echo "Initial content (with CRLF):"
printf '%s' "$CRLF_CONTENT"
echo "------"

echo
echo "docformatter stable (${DOCFORMATTER_STABLE}):"
printf '%s' "$CRLF_CONTENT" | uvx --python=3.13 --from "${DOCFORMATTER_STABLE}" docformatter --black -
echo "------"

echo
echo "docformatter finswimmer (${DOCFORMATTER_FINSWIMMER_REF}):"
printf '%s' "$CRLF_CONTENT" | uvx --python=3.13 --from "${DOCFORMATTER_FINSWIMMER_REF}" docformatter --black -
echo "------"

Incompatibility with black

#!/usr/bin/env bash
set -euo pipefail

DOCFORMATTER_STABLE="docformatter==1.7.7"
DOCFORMATTER_FINSWIMMER_REF="git+https://github.com/finswimmer/docformatter@948c28a5d43106bad3ec796fdf26f87cbdaee085"

ORIGINAL_CONTENT=$'"""\nA\n"""\n\npass\n'


echo "Initial content:"
printf '%s' "$ORIGINAL_CONTENT"
echo "------"

echo
echo "docformatter stable (${DOCFORMATTER_STABLE}):"
printf '%s' "$ORIGINAL_CONTENT" | uvx --python=3.13 --from "${DOCFORMATTER_STABLE}" docformatter --black -
echo "------"

echo
echo "docformatter finswimmer (${DOCFORMATTER_FINSWIMMER_REF}):"
printf '%s' "$ORIGINAL_CONTENT" | uvx --python=3.13 --from "${DOCFORMATTER_FINSWIMMER_REF}" docformatter --black -
echo "------"

Output:

Initial content:
"""
A
"""

pass
------

docformatter stable (docformatter==1.7.7):
"""
A
"""

pass
------

docformatter finswimmer (git+https://github.com/finswimmer/docformatter@948c28a5d43106bad3ec796fdf26f87cbdaee085):
"""
A
"""


pass

This demonstrates that a newline is added. This newline is something that black gets rid of.

@finswimmer
Copy link
Contributor Author

The CRLF issue should be fixed.

I'm not sure how to handle the conflict with black. The code clearly says, that it will ensure 2 empty line follow the module docstring. I couldn't find enough information what the current behavior of black should. There are some older issue that state that 2 lines are intended, e.g. psf/black#4027 So can you please open a separate issue for this and hopefully @weibullguy can clarify how they like to proceed with this?

fin swimmer

@finswimmer finswimmer marked this pull request as ready for review November 27, 2025 17:53
@finswimmer finswimmer changed the title Fix new lines fix several empty line regressions Nov 27, 2025
@finswimmer finswimmer changed the title fix several empty line regressions fix: resolve several empty line regressions Nov 27, 2025
@adamtheturtle
Copy link

Thank you @finswimmer . I have created #331.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

On master - file left with two blank lines at end

2 participants