8000 small edits · python/cpython@d217592 · GitHub
[go: up one dir, main page]

Skip to content

Commit d217592

Browse files
committed
small edits
1 parent f323dbd commit d217592

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

Lib/functools.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,6 @@ class partialmethod:
451451
__repr__ = _partial_repr
452452

453453
def __init__(self, func, /, *args, **keywords):
454-
if not callable(func) and getattr(func, '__get__', None) is None:
455-
raise TypeError(f'the first argument {func!r} must be a callable '
456-
'or a descriptor')
457-
458454
if isinstance(func, partialmethod):
459455
# Subclass optimization
460456
temp = partial(lambda: None, *func.args, **func.keywords)
@@ -467,8 +463,12 @@ def __init__(self, func, /, *args, **keywords):
467463
self.args = args
468464
self.keywords = keywords
469465

470-
if (isinstance(func, _STD_METHOD_TYPES) or
471-
getattr(func, '__get__', None) is None):
466+
if isinstance(func, _STD_METHOD_TYPES):
467+
self.method = None
468+
elif getattr(func, '__get__', None) is None:
469+
if not callable(func):
470+
raise TypeError(f'the first argument {func!r} must be a callable '
471+
'or a descriptor')
472472
self.method = None
473473
else:
474474
# Unknown descriptor
@@ -486,22 +486,26 @@ def _make_method(self):
486486

487487
# 4 cases
488488
if isinstance(func, staticmethod):
489-
func = partial(func.__wrapped__, *args, **self.keywords)
490-
self._set_func_attrs(func)
491-
return staticmethod(func)
489+
deco = staticmethod
490+
method = partial(func.__wrapped__, *args, **self.keywords)
492491
elif isinstance(func, classmethod):
492+
deco = classmethod
493493
ph_args = (Placeholder,) if args else ()
494-
func = partial(func.__wrapped__, *ph_args, *args, **self.keywords)
495-
self._set_func_attrs(func)
496-
return classmethod(func)
494+
method = partial(func.__wrapped__, *ph_args, *args, **self.keywords)
497495
else:
498496
# instance method. 2 cases:
499497
# a) FunctionType | partial
500498
# b) callable object without __get__
499+
deco = None
501500
ph_args = (Placeholder,) if args else ()
502-
func = partial(func, *ph_args, *args, **self.keywords)
503-
self._set_func_attrs(func)
504-
return func
501+
method = partial(func, *ph_args, *args, **self.keywords)
502+
503+
method.__partialmethod__ = self
504+
if self.__isabstractmethod__:
505+
method = abstractmethod(method)
506+
if deco is not None:
507+
method = deco(method)
508+
return method
505509

506510
def __get__(self, obj, cls=None):
507511
method = self.method
@@ -510,7 +514,9 @@ def __get__(self, obj, cls=None):
510514
# Need to get callable at runtime and apply partial on top
511515
new_func = self.func.__get__(obj, cls)
512516
result = partial(new_func, *self.args, **self.keywords)
513-
self._set_func_attrs(func)
517+
result.__partialmethod__ = self
518+
if self.__isabstractmethod__:
519+
result = abstractmethod(result)
514520
__self__ = getattr(new_func, '__self__', _NULL)
515521
if __self__ is not _NULL:
516522
result.__self__ = __self__

0 commit comments

Comments
 (0)
0