You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add pure Python fallback for functools.reduce #76502
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/ncoghlan'closed_at=<Date2018-10-25.14:10:34.470>created_at=<Date2017-12-14.13:26:34.576>labels= ['easy', '3.8', 'library']
title='Add pure Python fallback for functools.reduce'updated_at=<Date2018-10-25.15:01:16.818>user='https://github.com/stevendaprano'
The functools module imports reduce from _functools, using a guard in case it is not present:
try:
from _functools import reduce
except ImportError:
pass
However, the documentation says nothing about reduce being optional, and it is unconditionally included in the module __all__.
If reduce is guaranteed to be implemented in _functools, then the guard is redundant and should be removed. Otherwise, a pure python fallback should be added.
(The docs for reduce include a pure Python equivalent which might be sufficient.)
Reviewing the code and the CI test failures on the PR, the trick here is that functools isn't actually *using* functools.reduce, it's just re-exporting it if it's defined.
So if you block importing of "_functools" (which the test suite does in order to test the pure Python fallbacks), then the *only* consequence is that "functools.reduce" will be missing - the module will otherwise be fine.
This isn't at all clear when reading the code though, so I think the simplest resolution here would be to add a comment to the fallback path that says "If _functools.reduce is missing, then functools.reduce will also be missing, but the module will otherwise work".
I just merged PR 8548. See the PR to the discussion. IMHO it's a nice enhancement to have a pure Python implementation: #8548 (comment)
(and the PR has been approved by 2 other core devs ;-))
But I'm against adding it to Python 3.7 as well, I rejected the backport: PR 10092.
I proposed the author to write another PR to refactor test_functools.py, but I don't think that it's worth it to keep this issue open just for that: #8548 (comment)
changed the title [-]functools.reduce has a redundant guard or needs a pure Python fallback[/-][+]Add pure Python fallback for functools.reduce[/+]on Oct 25, 2018
However, the documentation says nothing about reduce being optional, and it is unconditionally included in the module __all__.
Oh, about this specific issue: maybe test___all__ should be fixed to test functools.py with _functools blocked? As done by test_functools.py? But this is a wider change and I'm not sure that it's doable :-( (especially in a generic way!) The PEP-399 requires the same API for the C and the Python implementations.
Activity
stevendaprano commentedon Dec 14, 2017
The functools module imports reduce from _functools, using a guard in case it is not present:
try:
from _functools import reduce
except ImportError:
pass
However, the documentation says nothing about reduce being optional, and it is unconditionally included in the module __all__.
If reduce is guaranteed to be implemented in _functools, then the guard is redundant and should be removed. Otherwise, a pure python fallback should be added.
(The docs for reduce include a pure Python equivalent which might be sufficient.)
asvetlov commentedon Dec 21, 2017
See discussion in https://bugs.python.org/issue12428 (especially the last message)
ncoghlan commentedon Jan 28, 2018
Reviewing the code and the CI test failures on the PR, the trick here is that functools isn't actually *using* functools.reduce, it's just re-exporting it if it's defined.
So if you block importing of "_functools" (which the test suite does in order to test the pure Python fallbacks), then the *only* consequence is that "functools.reduce" will be missing - the module will otherwise be fine.
This isn't at all clear when reading the code though, so I think the simplest resolution here would be to add a comment to the fallback path that says "If _functools.reduce is missing, then functools.reduce will also be missing, but the module will otherwise work".
Alternatively, we could add a fallback implementation based on the recipe in the docs, and adjust the test suite to actually run the reduce tests against the py_functools variant: https://docs.python.org/3/library/functools.html#functools.reduce
vstinner commentedon Oct 25, 2018
New changeset e25d5fc by Victor Stinner (madman-bob) in branch 'master':
bpo-32321: Add pure Python fallback for functools.reduce (GH-8548)
e25d5fc
vstinner commentedon Oct 25, 2018
I just merged PR 8548. See the PR to the discussion. IMHO it's a nice enhancement to have a pure Python implementation:
#8548 (comment)
(and the PR has been approved by 2 other core devs ;-))
But I'm against adding it to Python 3.7 as well, I rejected the backport: PR 10092.
I proposed the author to write another PR to refactor test_functools.py, but I don't think that it's worth it to keep this issue open just for that:
#8548 (comment)
[-]functools.reduce has a redundant guard or needs a pure Python fallback[/-][+]Add pure Python fallback for functools.reduce[/+]vstinner commentedon Oct 25, 2018
Oh, about this specific issue: maybe test___all__ should be fixed to test functools.py with _functools blocked? As done by test_functools.py? But this is a wider change and I'm not sure that it's doable :-( (especially in a generic way!) The PEP-399 requires the same API for the C and the Python implementations.