Conversation
… 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
slicefilter incorrectly appends thefill_withvalue to every slice when the iterable length is evenly divisible by the slice count.Before (buggy):
Output:
[[1, "foo"], [2, "foo"], [3, "foo"], [4, "foo"]]After (fixed):
Output:
[[1], [2], [3], [4]]Root Cause
When
length % slices == 0,slices_with_extrais0. The fill conditionslice_number >= slices_with_extrabecomesslice_number >= 0, which is always true, so every slice gets the fill value appended.Fix
Added
slices_with_extra > 0guard to the fill condition, so fill values are only applied when there is actually a remainder to distribute:Tests
10 new tests in
tests/test_slice_fill_fix.py:Fixes #2118