Skip to content

Pass the accumulator and value to builtin reducers - #390

Merged
dgilland merged 1 commit into
dgilland:developfrom
nk-batman21:fix-builtin-reducers
Oct 30, 2026
Merged

dgilland merged 1 commit into
dgilland:developfrom
nk-batman21:fix-builtin-reducers

Conversation

@nk-batman21

@nk-batman21 nk-batman21 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

reduce_ and reductions call a builtin iteratee with only the accumulator, because getargcount() gives every builtin an argument count of 1. That rule keeps map_(values, int) from passing the index as base, but a reducer always needs the accumulator and the value, so the usual functools.reduce-style calls fail:

>>> reduce_([1, 2, 3, 4], operator.mul)
TypeError: mul expected 2 arguments, got 1
>>> reduce_([3, 9, 2], max)
TypeError: 'int' object is not iterable
>>> reduce_([12, 18], math.gcd)
12

reduce_right, reductions_right, zip_with([1, 2], [10, 20], operator.add) and unzip_with fail the same way because they go through reduce_ or reductions. The same calls with a lambda or pydash.add work.

This picks the argument count of a builtin function iteratee in reduce_ and reductions from 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 through getargcount, and builtins used as map_ 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.

  • Ran: full unit/doctest suite on Python 3.14 after rebasing on develop, 3,050 passed with 100% coverage, and Ruff format/lint.
  • Ran on the first version: Python 3.10, mypy, the chaining-types stub comparison, the strict Sphinx build and the package build.
  • Checked: the 9 binary cases fail on develop, and the 6 abs cases pass on develop and with this change; 1 to 3 argument lambdas, pydash.add, partial(operator.add), dict input and map_(values, int) give the same results as before.

@coveralls

coveralls commented Oct 28, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — nk-batman21:fix-builtin-reducers into dgilland:develop

@Ekko82 Ekko82 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/pydash/collections.py Resolved
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.

@Ekko82 Ekko82 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dgilland
dgilland merged commit 6fae887 into dgilland:develop Oct 30, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants