Skip to content
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

gh-124652: partialmethod simplifications #124788

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
190 changes: 101 additions & 89 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
'partial', 'partialmethod', 'singledispatch', 'singledispatchmethod',
'cached_property', 'Placeholder']

from abc import get_cache_token
from abc import abstractmethod, get_cache_token
from collections import namedtuple
# import weakref # Deferred to single_dispatch()
from operator import itemgetter
from reprlib import recursive_repr
from types import GenericAlias, MethodType, MappingProxyType, UnionType
from types import FunctionType, GenericAlias, MethodType, MappingProxyType, UnionType
from _thread import RLock

################################################################################
Expand Down Expand Up @@ -313,49 +313,6 @@ def _partial_prepare_merger(args):
merger = itemgetter(*order) if phcount else None
return phcount, merger

def _partial_new(cls, func, /, *args, **keywords):
if issubclass(cls, partial):
base_cls = partial
if not callable(func):
raise TypeError("the first argument must be callable")
else:
base_cls = partialmethod
# func could be a descriptor like classmethod which isn't callable
if not callable(func) and not hasattr(func, "__get__"):
raise TypeError(f"the first argument {func!r} must be a callable "
"or a descriptor")
if args and args[-1] is Placeholder:
raise TypeError("trailing Placeholders are not allowed")
if isinstance(func, base_cls):
pto_phcount = func._phcount
tot_args = func.args
if args:
tot_args += args
if pto_phcount:
# merge args with args of `func` which is `partial`
nargs = len(args)
if nargs < pto_phcount:
tot_args += (Placeholder,) * (pto_phcount - nargs)
tot_args = func._merger(tot_args)
if nargs > pto_phcount:
tot_args += args[pto_phcount:]
phcount, merger = _partial_prepare_merger(tot_args)
else: # works for both pto_phcount == 0 and != 0
phcount, merger = pto_phcount, func._merger
keywords = {**func.keywords, **keywords}
func = func.func
else:
tot_args = args
phcount, merger = _partial_prepare_merger(tot_args)

self = object.__new__(cls)
self.func = func
self.args = tot_args
self.keywords = keywords
self._phcount = phcount
self._merger = merger
return self

def _partial_repr(self):
cls = type(self)
module = cls.__module__
Expand All @@ -374,7 +331,49 @@ class partial:
__slots__ = ("func", "args", "keywords", "_phcount", "_merger",
"__dict__", "__weakref__")

__new__ = _partial_new
def __new__(cls, func, /, *args, **keywords):
if not callable(func):
raise TypeError("the first argument must be callable")
if args and args[-1] is Placeholder:
# Trim trailing placeholders
j = len(args) - 1
if not j:
args = ()
else:
while (j := j - 1) >= 0:
if args[j] is not Placeholder:
break
args = args[:j + 1]
dg-pb marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(func, partial):
pto_phcount = func._phcount
tot_args = func.args
if args:
tot_args += args
if pto_phcount:
# merge args with args of `func` which is `partial`
nargs = len(args)
if nargs < pto_phcount:
tot_args += (Placeholder,) * (pto_phcount - nargs)
tot_args = func._merger(tot_args)
if nargs > pto_phcount:
tot_args += args[pto_phcount:]
phcount, merger = _partial_prepare_merger(tot_args)
else: # works for both pto_phcount == 0 and != 0
phcount, merger = pto_phcount, func._merger
keywords = {**func.keywords, **keywords}
func = func.func
else:
tot_args = args
phcount, merger = _partial_prepare_merger(tot_args)

self = object.__new__(cls)
self.func = func
self.args = tot_args
self.keywords = keywords
self._phcount = phcount
self._merger = merger
return self

__repr__ = recursive_repr()(_partial_repr)

def __call__(self, /, *args, **keywords):
Expand Down Expand Up @@ -413,7 +412,7 @@ def __setstate__(self, state):
raise TypeError("invalid partial state")

if args and args[-1] is Placeholder:
raise TypeError("trailing Placeholders are not allowed")
raise TypeError("unexpected trailing Placeholders")
phcount, merger = _partial_prepare_merger(args)

args = tuple(args) # just in case it's a subclass
Expand Down Expand Up @@ -444,50 +443,65 @@ class partialmethod:
Supports wrapping existing descriptors and handles non-descriptor
callables as instance methods.
"""
__new__ = _partial_new

__slots__ = ("func", "args", "keywords", "wrapper",
"__isabstractmethod__", "__dict__", "__weakref__")
__repr__ = _partial_repr

def _make_unbound_method(self):
def _method(cls_or_self, /, *args, **keywords):
phcount = self._phcount
if phcount:
try:
pto_args = self._merger(self.args + args)
args = args[phcount:]
except IndexError:
raise TypeError("missing positional arguments "
"in 'partialmethod' call; expected "
f"at least {phcount}, got {len(args)}")
else:
pto_args = self.args
keywords = {**self.keywords, **keywords}
return self.func(cls_or_self, *pto_args, *args, **keywords)
_method.__isabstractmethod__ = self.__isabstractmethod__
_method.__partialmethod__ = self
return _method
def __init__(self, func, /, *args, **keywords):
if isinstance(func, partialmethod):
# Subclass optimization
temp = partial(lambda: None, *func.args, **func.keywords)
temp = partial(temp, *args, **keywords)
func = func.func
args = temp.args
keywords = temp.keywords
self.func = func
self.args = args
self.keywords = keywords
self.__isabstractmethod__ = getattr(func, "__isabstractmethod__", False)

# 5 cases
rewrap = None
if isinstance(func, staticmethod):
self.wrapper = partial(func.__wrapped__, *args, **keywords)
rewrap = staticmethod
elif isinstance(func, classmethod):
self.wrapper = partial(func.__wrapped__, Placeholder, *args, **keywords)
rewrap = classmethod
elif isinstance(func, (FunctionType, partial)):
# instance method
self.wrapper = partial(func, Placeholder, *args, **keywords)
elif getattr(func, '__get__', None) is None:
if not callable(func):
raise TypeError(f"the first argument {func!r} must be a callable "
"or a descriptor")
# callable object without __get__
# treat this like an instance method
self.wrapper = partial(func, Placeholder, *args, **keywords)
else:
# Unknown descriptor
self.wrapper = None

def __get__(self, obj, cls=None):
get = getattr(self.func, "__get__", None)
result = None
if get is not None:
new_func = get(obj, cls)
if new_func is not self.func:
# Assume __get__ returning something new indicates the
# creation of an appropriate callable
result = partial(new_func, *self.args, **self.keywords)
try:
result.__self__ = new_func.__self__
except AttributeError:
pass
if result is None:
# If the underlying descriptor didn't do anything, treat this
# like an instance method
result = self._make_unbound_method().__get__(obj, cls)
return result
# Adjust for abstract and rewrap if needed
if self.wrapper is not None:
if self.__isabstractmethod__:
self.wrapper = abstractmethod(self.wrapper)
if rewrap is not None:
self.wrapper = rewrap(self.wrapper)

@property
def __isabstractmethod__(self):
return getattr(self.func, "__isabstractmethod__", False)
def __get__(self, obj, cls=None):
if self.wrapper is not None:
return self.wrapper.__get__(obj, cls)
else:
# Unknown descriptor
new_func = getattr(self.func, '__get__')(obj, cls)
result = partial(new_func, *self.args, **self.keywords)
try:
result.__self__ = new_func.__self__
except AttributeError:
pass
return result

__class_getitem__ = classmethod(GenericAlias)

Expand All @@ -503,8 +517,6 @@ def _unwrap_partialmethod(func):
prev = None
while func is not prev:
prev = func
while isinstance(getattr(func, "__partialmethod__", None), partialmethod):
func = func.__partialmethod__
while isinstance(func, partialmethod):
func = getattr(func, 'func')
func = _unwrap_partial(func)
Expand Down
33 changes: 0 additions & 33 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2438,39 +2438,6 @@ def _signature_from_callable(obj, *,
'attribute'.format(o_sig))
return sig

try:
partialmethod = obj.__partialmethod__
except AttributeError:
pass
else:
if isinstance(partialmethod, functools.partialmethod):
# Unbound partialmethod (see functools.partialmethod)
# This means, that we need to calculate the signature
# as if it's a regular partial object, but taking into
# account that the first positional argument
# (usually `self`, or `cls`) will not be passed
# automatically (as for boundmethods)

wrapped_sig = _get_signature_of(partialmethod.func)

sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
if first_wrapped_param.kind is Parameter.VAR_POSITIONAL:
# First argument of the wrapped callable is `*args`, as in
# `partialmethod(lambda *args)`.
return sig
else:
sig_params = tuple(sig.parameters.values())
assert (not sig_params or
first_wrapped_param is not sig_params[0])
# If there were placeholders set,
# first param is transformed to positional only
if partialmethod.args.count(functools.Placeholder):
first_wrapped_param = first_wrapped_param.replace(
kind=Parameter.POSITIONAL_ONLY)
new_params = (first_wrapped_param,) + sig_params
return sig.replace(parameters=new_params)

if isinstance(obj, functools.partial):
wrapped_sig = _get_signature_of(obj.func)
return _signature_get_partial(wrapped_sig, obj)
Expand Down
15 changes: 10 additions & 5 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,16 @@ def foo(bar):
p2.new_attr = 'spam'
self.assertEqual(p2.new_attr, 'spam')

def test_placeholders_trailing_raise(self):
def test_placeholders_trailing_trim(self):
PH = self.module.Placeholder
for args in [(PH,), (0, PH), (0, PH, 1, PH, PH, PH)]:
with self.assertRaises(TypeError):
self.partial(capture, *args)
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_placeholders(self):
PH = self.module.Placeholder
Expand Down Expand Up @@ -370,7 +375,7 @@ def test_setstate(self):

# Trailing Placeholder error
f = self.partial(signature)
msg_regex = re.escape("trailing Placeholders are not allowed")
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=[])))

Expand Down
13 changes: 5 additions & 8 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -3668,8 +3668,10 @@ def test():
pass
ham = partialmethod(test)

with self.assertRaisesRegex(ValueError, "has incorrect arguments"):
inspect.signature(Spam.ham)
self.assertEqual(self.signature(Spam.ham, eval_str=False),
((), Ellipsis))
with self.assertRaisesRegex(ValueError, "invalid method signature"):
inspect.signature(Spam().ham)

class Spam:
def test(it, a, b, *, c) -> 'spam':
Expand Down Expand Up @@ -3708,14 +3710,9 @@ def test(self: 'anno', x):
g = partialmethod(test, 1)

self.assertEqual(self.signature(Spam.g, eval_str=False),
((('self', ..., 'anno', 'positional_or_keyword'),),
((('self', ..., 'anno', 'positional_only'),),
...))

def test_signature_on_fake_partialmethod(self):
def foo(a): pass
foo.__partialmethod__ = 'spam'
self.assertEqual(str(inspect.signature(foo)), '(a)')

def test_signature_on_decorated(self):
def decorator(func):
@functools.wraps(func)
Expand Down
Loading
Loading