Skip to content

Respect --skip-source-first-line when formatting stdin - #5580

Merged
cobaltt7 merged 5 commits into
psf:mainfrom
Enesuygurs:fix-stdin-skip-source-first-line
Oct 22, 2026
Merged

cobaltt7 merged 5 commits into
psf:mainfrom
Enesuygurs:fix-stdin-skip-source-first-line

Conversation

@Enesuygurs

Copy link
Copy Markdown
Contributor

Description

--skip-source-first-line (-x) is honoured when formatting files by path, but it is silently ignored when the code is passed through stdin (black -x -). Instead, the first line is formatted along with the rest. When that line isn't valid Python, which is the case the option exists for (e.g. a Windows batch wrapper), Black fails to parse it:

$ printf '@echo off && python -x "%%~f0" %%* && goto :eof\nx=1\n' | black -x -
error: cannot parse: -:1:6

The same file formatted by path works. With --diff/--check on stdin, an unformatted first line also shows up as a change and makes the check fail.

format_file_in_place() splits the header off before decoding and prepends it again afterwards, but format_stdin_to_stdout() never looked at mode.skip_source_first_line. It now does the same. This includes the existing CRLF handling for headers, so the output doesn't end up with \r\r\n. The header is kept for write-back and for --diff/--color-diff output.

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the stability policy? (no style change, CLI/IO fix only)
  • Add an entry in CHANGES.md if necessary? (under "Output")
  • Add / update tests if necessary? (test_skip_source_first_line_via_stdin, LF and CRLF, write-back and --diff; fails without the fix)
  • Add new / update outdated documentation? (no documentation change needed)

@github-actions

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (e6b36b4) to main (6112e64):

--preview style: no changes

--stable style: no changes


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

@cobaltt7
cobaltt7 merged commit 1190eeb into psf:main Oct 22, 2026
58 checks passed
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.

2 participants