Skip to content

Commit

Permalink
small edits
Browse files Browse the repository at this point in the history
  • Loading branch information
dg-pb committed Oct 2, 2024
1 parent f323dbd commit d217592
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,6 @@ class partialmethod:
__repr__ = _partial_repr

def __init__(self, func, /, *args, **keywords):
if not callable(func) and getattr(func, '__get__', None) is None:
raise TypeError(f'the first argument {func!r} must be a callable '
'or a descriptor')

if isinstance(func, partialmethod):
# Subclass optimization
temp = partial(lambda: None, *func.args, **func.keywords)
Expand All @@ -467,8 +463,12 @@ def __init__(self, func, /, *args, **keywords):
self.args = args
self.keywords = keywords

if (isinstance(func, _STD_METHOD_TYPES) or
getattr(func, '__get__', None) is None):
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
Expand All @@ -486,22 +486,26 @@ def _make_method(self):

# 4 cases
if isinstance(func, staticmethod):
func = partial(func.__wrapped__, *args, **self.keywords)
self._set_func_attrs(func)
return staticmethod(func)
deco = staticmethod
method = partial(func.__wrapped__, *args, **self.keywords)
elif isinstance(func, classmethod):
deco = classmethod
ph_args = (Placeholder,) if args else ()
func = partial(func.__wrapped__, *ph_args, *args, **self.keywords)
self._set_func_attrs(func)
return classmethod(func)
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 ()
func = partial(func, *ph_args, *args, **self.keywords)
self._set_func_attrs(func)
return func
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):
method = self.method
Expand All @@ -510,7 +514,9 @@ def __get__(self, obj, cls=None):
# Need to get callable at runtime and apply partial on top
new_func = self.func.__get__(obj, cls)
result = partial(new_func, *self.args, **self.keywords)
self._set_func_attrs(func)
result.__partialmethod__ = self
if self.__isabstractmethod__:
result = abstractmethod(result)
__self__ = getattr(new_func, '__self__', _NULL)
if __self__ is not _NULL:
result.__self__ = __self__
Expand Down

0 comments on commit d217592

Please sign in to comment.