Skip to content

Preserve prefixes outside --line-ranges#5175

Open
cyphercodes wants to merge 1 commit into
psf:mainfrom
cyphercodes:fix-line-ranges-preserve-outside-prefix
Open

Preserve prefixes outside --line-ranges#5175
cyphercodes wants to merge 1 commit into
psf:mainfrom
cyphercodes:fix-line-ranges-preserve-outside-prefix

Conversation

@cyphercodes

Copy link
Copy Markdown
Contributor

Description

Preserves comments and blank lines that are outside the requested --line-ranges instead of normalizing them while unchanged code is converted to standalone-comment leaves for range formatting.

This keeps the issue repro constrained to the requested lines: comments and separation before unchanged statements are preserved when the selected range ends immediately before them.

Fixes #4430.

Checklist - did you ...

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

Verification

  • python -m black --check src/black/comments.py src/black/linegen.py src/black/lines.py src/black/ranges.py tests/test_ranges.py
  • python -m pytest tests/test_ranges.py tests/test_format.py -q
  • git diff --check

@cyphercodes cyphercodes force-pushed the fix-line-ranges-preserve-outside-prefix branch from 84afab8 to 682d265 Compare June 12, 2026 03:49
@github-actions

github-actions Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (392adbf) to main (6325332):

--preview style: no changes

--stable style: no changes


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

@cyphercodes cyphercodes force-pushed the fix-line-ranges-preserve-outside-prefix branch from 682d265 to 392adbf Compare June 12, 2026 04:05
@cobaltt7

Copy link
Copy Markdown
Collaborator

Thanks - this fixes issues where whitespace at the beginning of lines before comments is stripped when using --line-ranges. However, this doesn't address trailing whitespace on prefix lines, which I think was the main issue #4430 was implying.

As an example:

   #  format whitespace   
print( "format me" )   
      

   #  don't format whitespace   
print("don't format me"  )     
      

Output with --line-ranges 1-3:

#  format whitespace
print("format me")


   #  don't format whitespace
print("don't format me"  )     
[empty line at EOF]

Expected output:

#  format whitespace
print( "format me" )


   #  don't format whitespace   
print("don't format me"  )     
      

@cobaltt7

Copy link
Copy Markdown
Collaborator

I also found an edge case where this fix doesn't seem to be applied. Example with --line-ranges 1-2:

print( "format me" )
   #  format whitespace

print("don't format me"  )

The spaces before the comment are retained, but they should be removed here. They are removed when running Black on the whole file.

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.

--line-ranges formats lines outside of range

2 participants