8000 gh-124652: partialmethod simplifications by dg-pb · Pull Request #124788 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-124652: partialmethod simplifications #124788

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test_trailing_placeholders and bug fix
  • Loading branch information
dg-pb committed Sep 30, 2024
commit 8fa0ec50e50dfd373e32d66ba474b06a61256972
13 changes: 11 additions & 2 deletions Lib/functools.py
< 8000 /tr>
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,15 @@ def _partial_prepare_merger(args):
else:
order.append(i)
phcount = j - nargs
merger = itemgetter(*order) if phcount else None
if phcount:
if nargs == 1:
i = order[0]
def merger(all_args):
return (all_args[i],)
else:
merger = itemgetter(*order)
else:
merger = None
return phcount, merger

def _partial_repr(self):
Expand Down Expand Up @@ -403,7 +411,8 @@ def __setstate__(self, state):

phcount, merger = _partial_prepare_merger(args)

args = tuple(args) # just in case it's a subclass
if type(args) is not tuple:
args = tuple(args) # just in case it's a subclass
if kwds is None:
kwds = {}
elif type(kwds) is not dict: # XXX does it need to be *exactly* dict?
Expand Down
26 changes: 10 additions & 16 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,16 @@ def foo(bar):
p2.new_attr = 'spam'
self.assertEqual(p2.new_attr, 'spam')

# def test_placeholders_trailing_trim(self):
# PH = self.module.Placeholder
# for args, call_args, expected_args in [
# [(PH,), (), ()],
# [(0, PH), (), (0,)],
# [(0, PH, 1, PH, PH, PH), (2,), (0, 2, 1)]
# ]:
# actual_args, actual_kwds = self.partial(capture, *args)(*call_args)
# self.assertEqual(actual_args, expected_args)
# self.assertEqual(actual_kwds, {})
def test_trailing_placeholders(self):
PH = self.module.Placeholder
for args, call_args, expected_args in [
[(PH,), (1,), (1,)],
[(0, PH), (1,), (0, 1)],
[(0, PH, 2, PH, PH), (1, 3, 4), (0, 1, 2, 3, 4)]
]:
actual_args, actual_kwds = self.partial(capture, *args)(*call_args)
self.assertEqual(actual_args, expected_args)
self.assertEqual(actual_kwds, {})

def test_placeholders(self):
PH = self.module.Placeholder
Expand Down Expand Up @@ -373,12 +373,6 @@ def test_setstate(self):
f()
self.assertEqual(f(2), ((2, 1), dict(a=10)))

# # Trailing Placeholder error
# f = self.partial(signature)
# msg_regex = re.escape("unexpected trailing Placeholders")
# with self.assertRaisesRegex(TypeError, f'^{msg_regex}$') as cm:
# f.__setstate__((capture, (1, PH), dict(a=10), dict(attr=[])))

def test_setstate_errors(self):
f = self.partial(signature)
self.assertRaises(TypeError, f.__setstate__, (capture, (), {}))
Expand Down
Loading
0