From 1a8565e76d4a9592ea239155544af4bae3b39d62 Mon Sep 17 00:00:00 2001 From: cobalt <61329810+cobaltt7@users.noreply.github.com> Date: Sun, 7 Dec 2025 18:21:39 -0600 Subject: [PATCH] Fix new lines being added after imports with `# fmt: skip` on them Signed-off-by: cobalt <61329810+cobaltt7@users.noreply.github.com> --- CHANGES.md | 5 +---- src/black/lines.py | 1 + tests/data/cases/preview_fmtpass_imports.py | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 tests/data/cases/preview_fmtpass_imports.py diff --git a/CHANGES.md b/CHANGES.md index 76593423944..5e38b367095 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -22,17 +22,14 @@ - Fix `fix_fmt_skip_in_one_liners` crashing on `with` statements (#4853) - Fix `fix_fmt_skip_in_one_liners` crashing on annotated parameters (#4854) - Fix `# fmt: skip` behavior for deeply nested expressions (#4883) +- Fix new lines being added after imports with `# fmt: skip` on them (#4894) ### Packaging - - - Releases now include arm64 Windows binaries and wheels (#4814) ### Integrations - - - Add `output-file` input to GitHub Action `psf/black` to write formatter output to a file for artifact capture and log cleanliness (#4824) diff --git a/src/black/lines.py b/src/black/lines.py index fffd9f7120f..2997884de1a 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -695,6 +695,7 @@ def _maybe_empty_lines(self, current_line: Line) -> tuple[int, int]: and self.previous_line.depth == 0 and current_line.depth == 0 and not current_line.is_import + and not current_line.is_fmt_pass_converted(first_leaf_matches=is_import) and Preview.always_one_newline_after_import in self.mode ): return 1, 0 diff --git a/tests/data/cases/preview_fmtpass_imports.py b/tests/data/cases/preview_fmtpass_imports.py new file mode 100644 index 00000000000..d7ea3883aa4 --- /dev/null +++ b/tests/data/cases/preview_fmtpass_imports.py @@ -0,0 +1,21 @@ +# flags: --preview + +# Regression test for https://github.com/psf/black/issues/3438 + +import ast +import collections # fmt: skip +import dataclasses +# fmt: off +import os +# fmt: on +import pathlib + +import re # fmt: skip +import secrets + +# fmt: off +import sys +# fmt: on + +import tempfile +import zoneinfo