From 880ff0ddf7442f7e783c225851ad3de9b414d107 Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Fri, 20 Feb 2026 05:56:41 +0300 Subject: [PATCH 1/2] Mark first block before matching content as changed in line ranges mapping --- src/black/ranges.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/black/ranges.py b/src/black/ranges.py index d7e003db83f..f8378523106 100644 --- a/src/black/ranges.py +++ b/src/black/ranges.py @@ -492,7 +492,7 @@ def _calculate_lines_mappings( original_end=block.a, modified_start=1, modified_end=block.b, - is_changed_block=False, + is_changed_block=True, ) ) else: From 219ff2db56409df2da23f4d3531c34b559547023 Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Fri, 20 Feb 2026 10:32:41 +0300 Subject: [PATCH 2/2] Add unit test and changelog entry for line ranges first block fix --- CHANGES.md | 2 ++ tests/test_ranges.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index eaa962f918d..203b054fb91 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,8 @@ - Don't double-decode input, causing non-UTF-8 files to be corrupted (#4964) +- Fix `--line-ranges` not marking the first block as changed when it contains + modifications before the first matching content (#4997) ### Preview style diff --git a/tests/test_ranges.py b/tests/test_ranges.py index 0ed0e989123..1ddc8991e89 100644 --- a/tests/test_ranges.py +++ b/tests/test_ranges.py @@ -183,6 +183,23 @@ def test_diffs(lines: list[tuple[int, int]], adjusted: list[tuple[int, int]]) -> assert adjusted == adjusted_lines(lines, original_source, modified_source) +def test_first_block_changed() -> None: + """Changes in the first block (before any matching content) should be included.""" + original_source = """\ +x = 1 +y = 2 +z = 3 +""" + modified_source = """\ +x = 10 +y = 2 +z = 3 +""" + # Line 1 was changed, so range (1, 1) should map to (1, 1) in the output. + result = adjusted_lines([(1, 1)], original_source, modified_source) + assert result == [(1, 1)] + + @pytest.mark.parametrize( "lines,sanitized", [