Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 2 additions & 24 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you want to retain pickability but I'm not sure that way works (I think you need to add the "functools." part for the module).

cc @JelleZijlstra

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Something like this? Placeholder = sentinel('functools.Placeholder', repr='Placeholder')

My understanding from PEP 661 is that this is already handled by the sentinel's __module__ attribute.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe this should be fine, though you can always add a new test.

I made this same change in #149088, along with the corresponding change to the C implementation. I didn't push forward on it because of pushback around hypothetical code relying on type(Placeholder).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm going to revive Jelle's PR which also handles the C implementation.


def _partial_prepare_merger(args):
if not args:
Expand Down Expand Up @@ -440,7 +418,7 @@ def __setstate__(self, state):


try:
from _functools import partial, Placeholder, _PlaceholderType
from _functools import partial, Placeholder
except ImportError:
pass

Expand Down
7 changes: 0 additions & 7 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading