From 4c2505be5f8d566777232f717d6454887efa032b Mon Sep 17 00:00:00 2001 From: titagass <269501527+titagass@users.noreply.github.com> Date: Fri, 3 Apr 2026 06:29:56 +0000 Subject: [PATCH 1/4] fix(filters): do not append fill_with when items divide evenly across slices --- src/jinja2/filters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py index c46e20c10..2b62cc743 100644 --- a/src/jinja2/filters.py +++ b/src/jinja2/filters.py @@ -1089,7 +1089,7 @@ def sync_do_slice( end = offset + (slice_number + 1) * items_per_slice tmp = seq[start:end] - if fill_with is not None and slice_number >= slices_with_extra: + if fill_with is not None and slices_with_extra > 0 and slice_number >= slices_with_extra: tmp.append(fill_with) yield tmp From 9e2e474aac6e86649654c98790e10c216b0f8774 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Fri, 3 Apr 2026 06:32:58 +0000 Subject: [PATCH 2/4] [pre-commit.ci lite] apply automatic fixes --- src/jinja2/filters.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/jinja2/filters.py b/src/jinja2/filters.py index 2b62cc743..27e5f0589 100644 --- a/src/jinja2/filters.py +++ b/src/jinja2/filters.py @@ -1089,7 +1089,11 @@ def sync_do_slice( end = offset + (slice_number + 1) * items_per_slice tmp = seq[start:end] - if fill_with is not None and slices_with_extra > 0 and slice_number >= slices_with_extra: + if ( + fill_with is not None + and slices_with_extra > 0 + and slice_number >= slices_with_extra + ): tmp.append(fill_with) yield tmp From 38df06c8a40f7dde4b855f8c1ee05eafb2e72309 Mon Sep 17 00:00:00 2001 From: titagass <269501527+titagass@users.noreply.github.com> Date: Fri, 3 Apr 2026 06:51:11 +0000 Subject: [PATCH 3/4] test(filters): add regression tests for slice filter fill_with behavior --- tests/test_filters.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_filters.py b/tests/test_filters.py index 4601469a6..1663124b2 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -881,3 +881,18 @@ def test_filter_undefined_in_condexpr(self, env): with pytest.raises(TemplateRuntimeError, match="No filter named 'f'"): t2.render(x=42) + + def test_slice_no_fill_with_even_division(self, env): + """fill_with must not be appended when items divide evenly.""" + t = env.from_string("{{ items|slice(4, 'X')|list }}") + assert t.render(items=[1, 2, 3, 4]) == "[[1], [2], [3], [4]]" + assert t.render(items=list(range(9))) == ( + "[[0, 1, 2], [3, 4, 5], [6, 7, 8]]" + ) + + def test_slice_fill_with_remainder(self, env): + """fill_with should only be appended to shorter slices.""" + t = env.from_string("{{ items|slice(3, 'X')|list }}") + assert t.render(items=list(range(10))) == ( + "[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 'X']]" + ) From 84cd3a7cd5182ec88355dc5e1410a909e2dcaa4d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Fri, 3 Apr 2026 06:52:26 +0000 Subject: [PATCH 4/4] [pre-commit.ci lite] apply automatic fixes --- tests/test_filters.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_filters.py b/tests/test_filters.py index 1663124b2..afab3b655 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -886,9 +886,7 @@ def test_slice_no_fill_with_even_division(self, env): """fill_with must not be appended when items divide evenly.""" t = env.from_string("{{ items|slice(4, 'X')|list }}") assert t.render(items=[1, 2, 3, 4]) == "[[1], [2], [3], [4]]" - assert t.render(items=list(range(9))) == ( - "[[0, 1, 2], [3, 4, 5], [6, 7, 8]]" - ) + assert t.render(items=list(range(9))) == ("[[0, 1, 2], [3, 4, 5], [6, 7, 8]]") def test_slice_fill_with_remainder(self, env): """fill_with should only be appended to shorter slices."""