Skip to content

Commit a8bd3ae

Browse files
committed
CR changes
1 parent 1c8d73e commit a8bd3ae

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed

Lib/functools.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,8 @@ def _partial_new(cls, func, /, *args, **keywords):
343343
if nargs > pto_phcount:
344344
tot_args += args[pto_phcount:]
345345
phcount, merger = _partial_prepare_merger(tot_args)
346-
elif pto_phcount: # not args and pto_phcount
346+
else: # works for both pto_phcount == 0 and != 0
347347
phcount, merger = pto_phcount, func._merger
348-
else: # not args and not pto_phcount
349-
phcount, merger = 0, None
350348
keywords = {**func.keywords, **keywords}
351349
func = func.func
352350
else:

Lib/inspect.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,7 +1933,10 @@ def _signature_get_partial(wrapped_sig, partial, extra_args=()):
19331933
# If positional-only parameter is bound by partial,
19341934
# it effectively disappears from the signature
19351935
# However, if it is a Placeholder it is not removed
1936-
if arg_value is not functools.Placeholder:
1936+
# And also looses default value
1937+
if arg_value is functools.Placeholder:
1938+
new_params[param_name] = param.replace(default=_empty)
1939+
else:
19371940
new_params.pop(param_name)
19381941
continue
19391942

@@ -1957,9 +1960,13 @@ def _signature_get_partial(wrapped_sig, partial, extra_args=()):
19571960
else:
19581961
# was passed as a positional argument
19591962
# Do not pop if it is a Placeholder
1960-
# and change kind to positional only
1963+
# also change kind to positional only
1964+
# and remove default
19611965
if arg_value is functools.Placeholder:
1962-
new_param = param.replace(kind=_POSITIONAL_ONLY)
1966+
new_param = param.replace(
1967+
kind=_POSITIONAL_ONLY,
1968+
default=_empty
1969+
)
19631970
new_params[param_name] = new_param
19641971
else:
19651972
new_params.pop(param_name)

Lib/test/test_functools.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ def test_placeholders_optimization(self):
242242
got, empty = p3(5)
243243
expected = (-1, 0, 1, 2, 3, 4, 5)
244244
self.assertTrue(expected == got and empty == {})
245+
# inner partial has placeholders and outer partial has no args case
246+
p = self.partial(capture, PH, 0)
247+
p2 = self.partial(p)
248+
expected = (PH, 0)
249+
self.assertTrue(expected == p2.args)
250+
self.assertTrue(((1, 0), {}) == p2(1))
245251

246252
def test_construct_placeholder_singleton(self):
247253
PH = self.module.Placeholder
@@ -550,7 +556,7 @@ class TestPartialPySubclass(TestPartialPy):
550556
def test_subclass_optimization(self):
551557
p = py_functools.partial(min, 2)
552558
p2 = self.partial(p, 1)
553-
assert p2.func == min
559+
assert p2.func is min
554560
assert p2(0) == 0
555561

556562

@@ -696,7 +702,7 @@ class PartialMethodSubclass(functools.partialmethod):
696702
pass
697703
p = functools.partialmethod(min, 2)
698704
p2 = PartialMethodSubclass(p, 1)
699-
assert p2.func == min
705+
assert p2.func is min
700706
assert p2.__get__(0)() == 0
701707

702708

Lib/test/test_inspect/test_inspect.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3523,16 +3523,16 @@ def foo(a=0, b=1, /, c=2, d=3):
35233523
# Positional
35243524
p = partial(foo, Placeholder, 1, c=0, d=1)
35253525
self.assertEqual(self.signature(p),
3526-
((('a', 0, ..., "positional_only"),
3526+
((('a', ..., ..., "positional_only"),
35273527
('c', 0, ..., "keyword_only"),
35283528
('d', 1, ..., "keyword_only")),
35293529
...))
35303530

35313531
# Positional or Keyword - transformed to positional
35323532
p = partial(foo, Placeholder, 1, Placeholder, 1)
35333533
self.assertEqual(self.signature(p),
3534-
((('a', 0, ..., "positional_only"),
3535-
('c', 2, ..., "positional_only")),
3534+
((('a', ..., ..., "positional_only"),
3535+
('c', ..., ..., "positional_only")),
35363536
...))
35373537

35383538
def test_signature_on_partialmethod(self):

Modules/_functoolsmodule.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -656,12 +656,6 @@ partial_setstate(partialobject *pto, PyObject *state)
656656
PyErr_SetString(PyExc_TypeError, "invalid partial state");
657657
return NULL;
658658
}
659-
Py_ssize_t state_len = PyTuple_GET_SIZE(state);
660-
if (state_len != 4) {
661-
PyErr_Format(PyExc_TypeError,
662-
"expected 4 items in state, got %zd", state_len);
663-
return NULL;
664-
}
665659
if (!PyArg_ParseTuple(state, "OOOO", &fn, &fnargs, &kw, &dict) ||
666660
!PyCallable_Check(fn) ||
667661
!PyTuple_Check(fnargs) ||

0 commit comments

Comments
 (0)