Skip to content

AI spam#2177

Closed
thakoreh wants to merge 2 commits into
pallets:mainfrom
thakoreh:fix/slice-fill-with-divisor
Closed

AI spam#2177
thakoreh wants to merge 2 commits into
pallets:mainfrom
thakoreh:fix/slice-fill-with-divisor

Conversation

@thakoreh

Copy link
Copy Markdown

Problem

The slice filter incorrectly appends the fill_with value to every slice when the iterable length is evenly divisible by the slice count.

Before (buggy):

{% set a = [1, 2, 3, 4] %}
{{ a|slice(4, "foo")|list }}

Output: [[1, "foo"], [2, "foo"], [3, "foo"], [4, "foo"]]

After (fixed):
Output: [[1], [2], [3], [4]]

Root Cause

When length % slices == 0, slices_with_extra is 0. The fill condition slice_number >= slices_with_extra becomes slice_number >= 0, which is always true, so every slice gets the fill value appended.

Fix

Added slices_with_extra > 0 guard to the fill condition, so fill values are only applied when there is actually a remainder to distribute:

# Before
if fill_with is not None and slice_number >= slices_with_extra:
# After
if fill_with is not None and slices_with_extra > 0 and slice_number >= slices_with_extra:

Tests

10 new tests in tests/test_slice_fill_fix.py:

  • 5 tests for even division (4/4, 6/3, 8/4, 1/1, 10/5)
  • 3 tests for remainder cases (5/3, 7/3, 3/4)
  • 2 tests for no-fill variants

Fixes #2118

Hiren and others added 2 commits May 27, 2026 06:17
… divisible

When iterable length is a multiple of slice count, slices_with_extra is 0.
The condition `slice_number >= slices_with_extra` becomes `>= 0` which
is always true, causing every slice to receive the fill value.

Fix: only apply fill when slices_with_extra > 0 (i.e., there is actually a
remainder to distribute).

10 new tests covering even division, remainder, and no-fill cases.

Fixes pallets#2118
@davidism davidism closed this May 27, 2026
@davidism davidism changed the title Fix: slice filter incorrectly applies fill_with when length is evenly divisible by slice count AI spam May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Slice returns one extra item when slice count is a divisor of iterable length and fill_with not none

2 participants