diff --git a/CHANGES.md b/CHANGES.md index 6062ac205c1..1d444369026 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -21,17 +21,14 @@ magic trailing commas and intentional multiline formatting (#4865) - 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 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 7068e4f4cba..09fce3a193d 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