Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
dg-pb wants to merge18 commits intopython:main
base:main
Choose a base branch
Loading
fromdg-pb:gh-124652-partialmethod
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
18 commits
Select commitHold shift + click to select a range
9262323
fix
dg-pbSep 30, 2024
3c4edd8
test fixes
dg-pbSep 30, 2024
2a843bc
merge
dg-pbSep 30, 2024
f43b69e
minor edits
dg-pbSep 30, 2024
90dc0fd
trailing placeholder restriction removed
dg-pbSep 30, 2024
10ee972
rm commented code
dg-pbSep 30, 2024
b76ffee
put back accidental removal
dg-pbSep 30, 2024
8fa0ec5
test_trailing_placeholders and bug fix
dg-pbSep 30, 2024
f323dbd
full backwards compatibility
dg-pbOct 2, 2024
d217592
small edits
dg-pbOct 2, 2024
88422c4
📜🤖 Added by blurb_it.
blurb-it[bot]Oct 17, 2024
79e9cff
Merge remote-tracking branch 'upstream/main' into gh-124652-partialme…
dg-pbOct 17, 2024
27493b3
restore previous _unwrap_partial
dg-pbOct 27, 2024
7b24727
is tuple rollback
dg-pbJan 4, 2025
4f33459
leading trailing placeholder lift factored out
dg-pbJan 8, 2025
c3df6e0
remove extra blank line
dg-pbJan 8, 2025
fca3d7d
rollback unnecessary changes
dg-pbJan 8, 2025
ec64137
Merge remote-tracking branch 'upstream/main' into gh-124652-partialme…
dg-pbMay 8, 2025
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
218 changes: 127 additions & 91 deletionsLib/functools.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,12 +14,12 @@
'partial', 'partialmethod', 'singledispatch', 'singledispatchmethod',
'cached_property', 'Placeholder']

from abc import get_cache_token
from abc importabstractmethod,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 importFunctionType,GenericAlias, MethodType, MappingProxyType, UnionType
from _thread import RLock

################################################################################
Expand DownExpand Up@@ -310,52 +310,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")
for value in keywords.values():
if value is Placeholder:
raise TypeError("Placeholder cannot be passed as a keyword argument")
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 +328,44 @@ 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:
raise TypeError("trailing Placeholders are not allowed")
for value in keywords.values():
if value is Placeholder:
raise TypeError("Placeholder cannot be passed as a keyword argument")
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 DownExpand Up@@ -439,6 +430,10 @@ def __setstate__(self, state):
except ImportError:
pass

_NULL = object()
_UNKNOWN_DESCRIPTOR = object()
_STD_METHOD_TYPES = (staticmethod, classmethod, FunctionType, partial)

# Descriptor version
class partialmethod:
"""Method descriptor with partial application of the given arguments
Expand All@@ -447,54 +442,94 @@ class partialmethod:
Supports wrapping existing descriptors and handles non-descriptor
callables as instance methods.
"""
__new__ = _partial_new

__slots__ = ("func", "args", "keywords", "wrapper",
"__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

if isinstance(func, _STD_METHOD_TYPES):
self.method = None
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')
self.method = None
else:
# Unknown descriptor
self.method = _UNKNOWN_DESCRIPTOR

def _set_func_attrs(self, func):
func.__partialmethod__ = self
if self.__isabstractmethod__:
func = abstractmethod(func)
return func

def _make_method(self):
args = self.args
func = self.func

# 4 cases
if isinstance(func, staticmethod):
deco = staticmethod
method = partial(func.__wrapped__, *args, **self.keywords)
elif isinstance(func, classmethod):
deco = classmethod
ph_args = (Placeholder,) if args else ()
method = partial(func.__wrapped__, *ph_args, *args, **self.keywords)
else:
# instance method. 2 cases:
# a) FunctionType | partial
# b) callable object without __get__
deco = None
ph_args = (Placeholder,) if args else ()
method = partial(func, *ph_args, *args, **self.keywords)

method.__partialmethod__ = self
if self.__isabstractmethod__:
method = abstractmethod(method)
if deco is not None:
method = deco(method)
return method

def __get__(self, obj, cls=None):
get =getattr(self.func, "__get__", None)
result = None
if get is not None:
new_func =get(obj, cls)
ifnew_funcis notself.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)
returnresult
method = self.method
if method is _UNKNOWN_DESCRIPTOR:
# Unknown descriptor == unknown binding
# Need toget callable at runtime and apply partial on top
new_func=self.func.__get__(obj, cls)
result = partial(new_func, *self.args, **self.keywords)
result.__partialmethod__ = self
ifself.__isabstractmethod__:
result = abstractmethod(result)
__self__ =getattr(new_func, '__self__', _NULL)
if __self__ is not _NULL:
result.__self__ = __self__
return result
if method is None:
#Cache method
self.method =method =self._make_method()
returnmethod.__get__(obj, cls)

@property
def __isabstractmethod__(self):
return getattr(self.func, "__isabstractmethod__", False)

__class_getitem__ = classmethod(GenericAlias)


# Helper functions

def _unwrap_partial(func):
Expand All@@ -506,13 +541,14 @@ 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)
__partialmethod__ =getattr(func, "__partialmethod__", None)
if isinstance(__partialmethod__, partialmethod):
func = __partialmethod__.func
if isinstance(func, (partial, partialmethod)):
func = func.func
return func


################################################################################
### LRU Cache function decorator
################################################################################
Expand Down
33 changes: 0 additions & 33 deletionsLib/inspect.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2461,39 +2461,6 @@ def _signature_from_callable(obj, *,
'attribute'.format(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
8 changes: 5 additions & 3 deletionsLib/test/test_inspect/test_inspect.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3728,8 +3728,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 DownExpand Up@@ -3768,7 +3770,7 @@ 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):
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
:func:`functools.partialmethod` is simplified by making use of new :func:`functools.partial` placeholder functionality.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp