From b03a69e5bc1cda4fd225fb965b387dd8d4a609bd Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 31 Oct 2026 15:23:32 -0500 Subject: [PATCH 1/2] Simplify Placeholder with the new sentinel() class --- Lib/functools.py | 24 +----------------------- Lib/test/test_functools.py | 7 ------- 2 files changed, 1 insertion(+), 30 deletions(-) diff --git a/Lib/functools.py b/Lib/functools.py index 2c08791d4d2651b..1679b67b1300721 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -275,29 +275,7 @@ def reduce(function, sequence, /, initial=_initial_missing): ################################################################################ -class _PlaceholderType: - """The type of the Placeholder singleton. - - Used as a placeholder for partial arguments. - """ - __instance = None - __slots__ = () - - def __init_subclass__(cls, *args, **kwargs): - raise TypeError(f"type '{cls.__name__}' is not an acceptable base type") - - def __new__(cls): - if cls.__instance is None: - cls.__instance = object.__new__(cls) - return cls.__instance - - def __repr__(self): - return 'Placeholder' - - def __reduce__(self): - return 'Placeholder' - -Placeholder = _PlaceholderType() +Placeholder = sentinel('Placeholder') # _PlaceholderType() def _partial_prepare_merger(args): if not args: diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 6aa7916adc0bb86..0eb5efcb6e18e26 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -279,13 +279,6 @@ def test_placeholders_kw_restriction(self): self.assertEqual(len(actual_kwds), 1) self.assertIs(actual_kwds['a'], ALWAYS_EQ) - def test_construct_placeholder_singleton(self): - PH = self.module.Placeholder - tp = type(PH) - self.assertIs(tp(), PH) - self.assertRaises(TypeError, tp, 1, 2) - self.assertRaises(TypeError, tp, a=1, b=2) - def test_repr(self): args = (object(), object()) args_repr = ', '.join(repr(a) for a in args) From 4f77a8c9859f5daddba37e14eaedba87080a5c48 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 31 Oct 2026 15:32:43 -0500 Subject: [PATCH 2/2] Remove unused import --- Lib/functools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/functools.py b/Lib/functools.py index 1679b67b1300721..05993163506ac52 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -418,7 +418,7 @@ def __setstate__(self, state): try: - from _functools import partial, Placeholder, _PlaceholderType + from _functools import partial, Placeholder except ImportError: pass