Skip to content

fix(filters): do not append fill_with when items divide evenly across slices#2154

Closed
justDance-everybody wants to merge 4 commits into
pallets:mainfrom
justDance-everybody:clawoss/fix/slice-filter-fill-with-2118
Closed

fix(filters): do not append fill_with when items divide evenly across slices#2154
justDance-everybody wants to merge 4 commits into
pallets:mainfrom
justDance-everybody:clawoss/fix/slice-filter-fill-with-2118

Conversation

@justDance-everybody

@justDance-everybody justDance-everybody commented Apr 3, 2026

Copy link
Copy Markdown

Fixes #2118 - see commit message for details

Copilot AI review requested due to automatic review settings April 3, 2026 06:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes slice filter behavior in Jinja2 so that fill_with is not appended when the input length divides evenly across the requested number of slices.

Changes:

  • Update sync_do_slice to only append fill_with when there is a remainder (slices_with_extra > 0) and the current slice is one of the shorter slices.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/jinja2/filters.py
Comment on lines +1092 to +1093
if (
fill_with is not None

Copilot AI Apr 3, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a regression test for the case where the input length divides evenly by the number of slices (so slices_with_extra == 0). With fill_with provided, this should not append any filler items (e.g., range(9)|slice(3, 'X') should yield [[0,1,2],[3,4,5],[6,7,8]]). This will ensure the bug fixed here doesn’t reappear, and should be covered in both sync and async filter tests if applicable.

Copilot uses AI. Check for mistakes.
@davidism davidism closed this Apr 3, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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