Skip to content

AI junk - #2238

Closed
Swastikbhat-lab wants to merge 1 commit into
pallets:mainfrom
Swastikbhat-lab:fix/slice-fill-divisor
Closed

AI junk#2238
Swastikbhat-lab wants to merge 1 commit into
pallets:mainfrom
Swastikbhat-lab:fix/slice-fill-divisor

Conversation

@Swastikbhat-lab

Copy link
Copy Markdown

Fixes #2118.

Bug

When the slice count divides the iterable length evenly, the slice filter still pads every chunk with fill_with:

{{ [1, 2, 3, 4]|slice(4, 'foo')|list }}

Actual: [[1, 'foo'], [2, 'foo'], [3, 'foo'], [4, 'foo']]
Expected: [[1], [2], [3], [4]]

Cause

In sync_do_slice, the padding condition is slice_number >= slices_with_extra. When length % slices == 0, slices_with_extra is 0, so the condition is true for every chunk and each one gets fill_with appended — even though no chunk is short.

Fix

Only pad when there are short chunks to pad, i.e. when slices_with_extra is non-zero. The non-divisor behavior (padding the chunks after the extras, e.g. range(10)|slice(3, 'X')[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]) is unchanged.

Tests

Added a regression test for the divisor case. Full suite:

912 passed in 2.49s

The `slice` filter appends `fill_with` to every chunk when the slice
count divides the iterable length exactly, because the padding condition
`slice_number >= slices_with_extra` is true for all chunks when
`slices_with_extra` is 0. Only pad chunks when there are short chunks to
pad, i.e. when the length is not evenly divisible by the slice count.

Add a regression test for the divisor case.

Closes pallets#2118.
@davidism

Copy link
Copy Markdown
Member

@davidism davidism closed this Aug 15, 2026
@davidism davidism changed the title Fix slice filter padding fill_with when slices divide the length evenly AI junk Aug 15, 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

3 participants