-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pure Python fallback for functools.reduce #76502
Comments
The functools module imports reduce from _functools, using a guard in case it is not present: try: 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.) |
See discussion in https://bugs.python.org/issue12428 (especially the last message) |
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 |
I just merged PR 8548. See the PR to the discussion. IMHO it's a nice enhancement to have a pure Python implementation: 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: |
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. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: