Skip to content

Preserve function docstring spacing with line ranges - #5286

Draft
happykawayigt wants to merge 2 commits into
psf:mainfrom
happykawayigt:fix/line-range-docstring-blank
Draft

Preserve function docstring spacing with line ranges#5286
happykawayigt wants to merge 2 commits into
psf:mainfrom
happykawayigt:fix/line-range-docstring-blank

Conversation

@happykawayigt

@happykawayigt happykawayigt commented Jul 31, 2026

Copy link
Copy Markdown

Description

Fixes #5285.

When line-range formatting replaces out-of-range code with fmt: pass placeholders, those placeholders are represented as comment lines. The blank-line tracker could therefore scan past the enclosing function boundary and misclassify its docstring as a module docstring, adding a blank line that was outside the requested formatting range.

Treating a converted fmt: pass line as a scope boundary preserves the existing spacing. The regression fixture exercises every single-line range across a formatted function whose docstring has no following blank line.

Validation:

  • pytest tests/test_format.py -q (230 passed)
  • pre-commit on changed files: isort, flake8, mypy, prettier, EOF, trailing whitespace passed
  • git diff --check

AI assistance was used during implementation. I independently reviewed the line-range conversion path and final diff, reproduced the regression through the line-by-line fixture, and ran the validation above.

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the stability policy? (No style change.)
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation? (No documentation change is needed.)

@happykawayigt
happykawayigt force-pushed the fix/line-range-docstring-blank branch from 38f5d7b to 8542127 Compare August 3, 2026 12:13
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (8542127) to main (5706d79):

--preview style: no changes

--stable style: no changes


What is this? | Workflow run | diff-shades documentation

@cobaltt7

cobaltt7 commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Is there a reason this is a draft PR? It all looks good to me!

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.

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

2 participants