Repository navigation
Pass the accumulator and value to builtin reducers - #390
Conversation
Ekko82
left a comment
There was a problem hiding this comment.
On Windows with Python 3.13.9, 14 targeted public-API checks give 10 passes and 4 failures at base 2b85153, versus 8 passes and 6 failures at this head. Four binary builtin cases are fixed, while six previously passing unary cases regress as detailed inline. Two unary-lambda controls, map(int) and default reduce_ still pass. I did not run the full suite, type checks, lint or build.
getargcount() gives every builtin an argcount of 1, so reduce_ and reductions called builtin reducers with only the accumulator: reduce_(values, operator.mul) raised TypeError, reduce_(values, max) tried to iterate the accumulator, and reduce_([12, 18], math.gcd) returned 12. zip_with and unzip_with failed the same way through reduce_. Pass two arguments to builtin reducers there; other iteratees and map-style builtin callbacks are unchanged.
cd914e1 to
e4562f4
Compare
Ekko82
left a comment
There was a problem hiding this comment.
Rechecked e4562f4 on Windows with Python 3.13.9 using the same 14 public-API probes from my earlier review. All 14 pass: the six abs paths now work, the four binary builtin cases still pass, and the four controls are unchanged. This resolves the unary regression I reported.
I ran these targeted probes; I did not run the full suite, type checks, lint or build.
reduce_andreductionscall a builtin iteratee with only the accumulator, becausegetargcount()gives every builtin an argument count of 1. That rule keepsmap_(values, int)from passing the index asbase, but a reducer always needs the accumulator and the value, so the usualfunctools.reduce-style calls fail:reduce_right,reductions_right,zip_with([1, 2], [10, 20], operator.add)andunzip_withfail the same way because they go throughreduce_orreductions. The same calls with a lambda orpydash.addwork.This picks the argument count of a builtin function iteratee in
reduce_andreductionsfrom its signature: two when it has two required positional parameters or*args(operator.mul,math.gcd), one when it has one (abs), and two when it has no signature (max,min). Builtin types and other iteratees still go throughgetargcount, and builtins used asmap_and other callbacks still get one argument. I added cases to the existing reduce, reductions, zip_with and unzip_with tests and an unreleased changelog entry.develop, 3,050 passed with 100% coverage, and Ruff format/lint.develop, and the 6abscases pass ondevelopand with this change; 1 to 3 argument lambdas,pydash.add,partial(operator.add), dict input andmap_(values, int)give the same results as before.