Skip to content

[rejected AI] do not append fill_with in slice when slice count evenly divides iterable - #2260

Closed
pylrn wants to merge 1 commit into
pallets:mainfrom
pylrn:fix/slice-filter-divisor-fill-with
Closed

[rejected AI] do not append fill_with in slice when slice count evenly divides iterable#2260
pylrn wants to merge 1 commit into
pallets:mainfrom
pylrn:fix/slice-filter-divisor-fill-with

Conversation

@pylrn

@pylrn pylrn commented Sep 1, 2026

Copy link
Copy Markdown

Fixes #2118

Problem

When slices evenly divides len(seq) (e.g. [1, 2, 3, 4]|slice(4, 'foo')), slices_with_extra = length % slices is 0.
Because the check was slice_number >= slices_with_extra, every slice satisfied slice_number >= 0 and had fill_with appended, adding an extra unnecessary element to every sub-list even though all slices were already full and of equal length.

Fix

  • Updated the fill_with check in sync_do_slice in src/jinja2/filters.py to require slices_with_extra > 0 in addition to slice_number >= slices_with_extra.
  • Added test assertions to tests/test_filters.py and tests/test_async_filters.py.

@davidism

davidism commented Sep 1, 2026

Copy link
Copy Markdown
Member

@davidism davidism closed this Sep 1, 2026
@davidism davidism changed the title fix(filters): do not append fill_with in slice when slice count evenly divides iterable [rejected AI] do not append fill_with in slice when slice count evenly divides iterable Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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