From 8b143efabf8ee91fe5d868fb920e81a00bfc2d99 Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Sat, 22 Mar 2025 05:03:15 +0530 Subject: [PATCH 1/2] Handle `# fmt: skip` followed by a comment --- src/black/comments.py | 2 +- tests/data/cases/fmtskip11.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 tests/data/cases/fmtskip11.py diff --git a/src/black/comments.py b/src/black/comments.py index 1054e7ae8a2..81d3cfd4a35 100644 --- a/src/black/comments.py +++ b/src/black/comments.py @@ -321,7 +321,7 @@ def _generate_ignored_nodes_from_fmt_skip( if not comments or comment.value != comments[0].value: return if prev_sibling is not None: - leaf.prefix = "" + leaf.prefix = leaf.prefix[comment.consumed :] if Preview.fix_fmt_skip_in_one_liners not in mode: siblings = [prev_sibling] diff --git a/tests/data/cases/fmtskip11.py b/tests/data/cases/fmtskip11.py new file mode 100644 index 00000000000..5d3f7874e55 --- /dev/null +++ b/tests/data/cases/fmtskip11.py @@ -0,0 +1,6 @@ +def foo(): + pass + + +# comment 1 # fmt: skip +# comment 2 From 363a0a82a92bfc68d7174eab73703c35bf3548be Mon Sep 17 00:00:00 2001 From: Tushar Sadhwani Date: Sat, 22 Mar 2025 05:06:19 +0530 Subject: [PATCH 2/2] Add changelog entry --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 4cbc3d8bd95..65bcab1c2cc 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ - Fix crash while formatting a long `del` statement containing tuples (#4628) - Fix crash while formatting expressions using the walrus operator in complex `with` statements (#4630) +- Handle `# fmt: skip` followed by a comment at the end of file (#4635) ### Preview style