Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
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
Next Next commit
Dispatch on the second argument if generic method is instance-bindable
  • Loading branch information
johnslavik committed Feb 8, 2026
commit 5479e0f0a4470518811463a99b726abb25fa53e9
15 changes: 13 additions & 2 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# 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 @@ -1060,6 +1060,12 @@ def __init__(self, unbound, obj, cls):
# Set instance attributes which cannot be handled in __getattr__()
# because they conflict with type descriptors.
func = unbound.func
# Dispatch on the second argument if a generic method turns into
# a bound method on instance-level access.
if obj is None and isinstance(func, FunctionType):
self._skip_bound_arg = True
else:
self._skip_bound_arg = False
Comment thread
johnslavik marked this conversation as resolved.
Outdated
try:
self.__module__ = func.__module__
except AttributeError:
Expand Down Expand Up @@ -1088,7 +1094,12 @@ def __call__(self, /, *args, **kwargs):
'singledispatchmethod method')
raise TypeError(f'{funcname} requires at least '
'1 positional argument')
method = self._dispatch(args[0].__class__)
if self._skip_bound_arg:
method = self._dispatch(args[1].__class__)
if not isinstance(method, FunctionType):
args = args[1:]
else:
method = self._dispatch(args[0].__class__)
if hasattr(method, "__get__"):
method = method.__get__(self._obj, self._cls)
return method(*args, **kwargs)
Expand Down
Loading