fix: floor/ceil/round_ used as iteratees silently pass index as precision - #248
Merged
dgilland merged 2 commits intoJul 6, 2026
Merged
Conversation
pydash.floor, pydash.ceil, and pydash.round_ each accept an optional
`precision` argument. helpers.getargcount() counts all positional
parameters, so it returns 2 for these functions. When callit() then
invokes them with (element, index, collection) it passes the collection
index as `precision`, producing wrong grouping keys.
For example:
count_by([6.1, 4.2, 6.3], floor)
returned {6.0: 1, 4.2: 1, 6.3: 1} instead of {6.0: 2, 4.0: 1}
because floor(4.2, 1) == 4.2 and floor(6.3, 2) == 6.3.
Setting _argcount = 1 on all three functions signals callit() to supply
only the element value, matching the intent of using them as iteratees.
Owner
|
Thanks for the contribution, much appreciated! 👍 |
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.
Bug
pydash.floor,pydash.ceil, andpydash.round_each accept an optionalprecisionargument.helpers.getargcount()counts all positionalparameters and therefore returns
2for these functions. Whencallit()invokes them inside
iteriteratee()it passes(element, index, collection)truncated to
argcount=2, so the collection index is forwarded asprecision.This produces silently wrong results whenever these functions are used directly
as iteratees.
Reproducer
The same bug affects
_.ceiland_.round_. Equivalent lambdas work finebecause
lambda x: _.floor(x)only has one positional parameter.Fix
Set
_argcount = 1onfloor,ceil, andround_after their definitions.This is the existing optimization mechanism already used by iteratees returned
from
utilities.iteratee()and the wrappers infunctions.py; it tellscallit()to supply only the element value.Normal two-argument use (
floor(3.75, 1)) is completely unaffected.Tests
Three new parametrize cases added to
test_count_by— one per affectedfunction — covering the previously broken behavior.