gh-149083: Convert _initial_missing for pure py reduce to sentinel#149536
Open
sobolevn wants to merge 1 commit intopython:mainfrom
Open
gh-149083: Convert _initial_missing for pure py reduce to sentinel#149536sobolevn wants to merge 1 commit intopython:mainfrom
_initial_missing for pure py reduce to sentinel#149536sobolevn wants to merge 1 commit intopython:mainfrom
Conversation
JelleZijlstra
approved these changes
May 8, 2026
Member
JelleZijlstra
left a comment
There was a problem hiding this comment.
Maybe ask Hugo how he feels about applying a few more of these in 3.15? It seems fairly low risk when replacing bare object().
Member
Author
|
I agree. It feels like the change should happen together with the addition of |
Member
|
It would be nice to fix the C version (it has broken signature now), but it seems that that's not backed yet by the AC. Or did I miss something? |
Member
Author
|
Yes, AFAIK |
Member
Attached patch "works for me": >>> import inspect, functools
>>> inspect.signature(functools.reduce)
<Signature (function, iterable, /, initial=_initial_missing)>Details(Don't forgot to run AC.) diff --git a/Lib/inspect.py b/Lib/inspect.py
index a96b3dc954e..dc5a6e3be88 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2200,7 +2200,8 @@ def wrap_value(s):
except NameError:
raise ValueError
- if isinstance(value, (str, int, float, bytes, bool, type(None))):
+ if isinstance(value, (str, int, float, bytes, bool, type(None),
+ sentinel)):
return ast.Constant(value)
raise ValueError
diff --git a/Modules/_functoolsmodule.c b/Modules/_functoolsmodule.c
index 19bdf3d47c2..13da7944b61 100644
--- a/Modules/_functoolsmodule.c
+++ b/Modules/_functoolsmodule.c
@@ -9,6 +9,7 @@
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()
+PyObject *_initial_missing;
#include "clinic/_functoolsmodule.c.h"
/*[clinic input]
@@ -1066,7 +1067,7 @@ _functools.reduce
function as func: object
iterable as seq: object
/
- initial as result: object = NULL
+ initial as result: object(c_default="NULL") = _functools._initial_missing
Apply a function of two arguments cumulatively to the items of an iterable, from left to right.
@@ -1081,7 +1082,7 @@ calculates ((((1 + 2) + 3) + 4) + 5).
static PyObject *
_functools_reduce_impl(PyObject *module, PyObject *func, PyObject *seq,
PyObject *result)
-/*[clinic end generated code: output=30d898fe1267c79d input=4ccfb74548ce5170]*/
+/*[clinic end generated code: output=30d898fe1267c79d input=7e5eaeb4f8a7a78d]*/
{
PyObject *args, *it;
@@ -1982,6 +1983,14 @@ _functools_exec(PyObject *module)
// lru_list_elem is used only in _lru_cache_wrapper.
// So we don't expose it in module namespace.
+ _initial_missing = PySentinel_New("_initial_missing", "_functools");
+ if (_initial_missing == NULL) {
+ return -1;
+ }
+ if (PyModule_Add(module, "_initial_missing", _initial_missing) < 0) {
+ Py_DECREF(_initial_missing);
+ return -1;
+ }
return 0;
}
|
skirpichev
reviewed
May 8, 2026
|
|
||
| def reduce(function, sequence, initial=_initial_missing): | ||
| """ | ||
| reduce(function, iterable, /[, initial]) -> value |
Member
There was a problem hiding this comment.
This signature line now looks redundant.
Member
Author
There was a problem hiding this comment.
I change the signature in another PR :)
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.
This is not covered in #149084
Difference is not big, but noticable. Before:

Notice the default parameter repr, which is rather unpretty.
After:

skip news, because there should be no user facing changes.