Skip to content

Fix #2118: Slice filter no longer adds extra fill_with items#2152

Closed
MuraveyApp wants to merge 2 commits intopallets:mainfrom
MuraveyApp:fix/issue-2118
Closed

Fix #2118: Slice filter no longer adds extra fill_with items#2152
MuraveyApp wants to merge 2 commits intopallets:mainfrom
MuraveyApp:fix/issue-2118

Conversation

@MuraveyApp
Copy link
Copy Markdown

Summary

Fixes #2118 — The slice filter added fill_with items even when the slice count evenly divided the iterable length, producing incorrect results.

E.g., [1,2,3,4]|slice(4, 'foo') returned [[1,'foo'],[2,'foo'],[3,'foo'],[4,'foo']] instead of [[1],[2],[3],[4]].

Changes

One-line fix in src/jinja2/filters.py:1092:

  • Added and slices_with_extra != 0 to the fill condition so fill_with is only appended when there are shorter slices that need padding.

Tests

Added 4 test cases in tests/test_filters.py:

  • Even division with fill_with (no fill should happen)
  • Even division without fill_with
  • Uneven division with fill
  • 5 items / 3 slices

All existing tests pass.

Context

This issue has been open for 6 months. Off-by-one bug. Resolved with assistance from OwlMind — an autonomous AI coding agent.

ChampChamp and others added 2 commits March 29, 2026 21:15
The slice filter added fill_with items even when the slice count evenly
divided the iterable length, producing incorrect results.

Resolved with assistance from OwlMind (https://owlmind.dev)
@davidism davidism closed this Mar 29, 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