fix: tail(0, seq) should return empty instead of the whole sequence#629
Open
kaizeenn wants to merge 1 commit into
Open
fix: tail(0, seq) should return empty instead of the whole sequence#629kaizeenn wants to merge 1 commit into
kaizeenn wants to merge 1 commit into
Conversation
`tail(0, seq)` is implemented as `seq[-n:]` which for `n == 0` becomes `seq[-0:]` = `seq[0:]` — returning the entire sequence instead of the last zero elements. This also makes `tail` inconsistent across iterable types: sliceable inputs return the whole sequence, while non-sliceable iterables hit the `deque(seq, 0)` fallback and correctly return empty. Fix by handling `n == 0` as a special case, returning `seq[:0]` for sliceable types and `()` for non-sliceable iterables. Fixes pytoolz#626. Signed-off-by: kaizeenn <[email protected]>
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.
Summary
Fixes #626.
tail(0, seq)returns the whole sequence for sliceable inputs and empty for iterables — inconsistent and wrong.Root cause
tail(n, seq)is implemented asseq[-n:]. Whenn == 0,seq[-0:]isseq[0:], returning the entire sequence. Non-sliceable iterables usedeque(seq, 0)which correctly returns empty, making the behavior inconsistent across input types.Fix
Handle
n == 0as a special case before the main logic:seq[:0](empty of the same type)()(empty tuple, consistent withdequefallback)Test evidence
(The only failure is
test_has_versionwhich requires the package to be installed.)