Skip to content

Commit f0097c4

Browse files
committed
Final optional refinements
1 parent 62ea333 commit f0097c4

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

Lib/functools.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -893,21 +893,21 @@ def _find_impl(cls, registry):
893893
match = t
894894
return registry.get(match)
895895

896-
def _get_singledispatch_annotated_param(func, *, role):
896+
def _get_singledispatch_annotated_param(func, functype):
897897
"""Find the first positional and user-specified parameter in a callable
898898
or descriptor.
899899
900900
Used by singledispatch for registration by type annotation of the parameter.
901901
"""
902902
if isinstance(func, staticmethod):
903-
idx = 0 # Take the very first parameter.
903+
idx = 0
904904
func = func.__func__
905905
elif isinstance(func, (classmethod, MethodType)):
906-
idx = 1 # Skip *cls* or *self*.
906+
idx = 1 # Skip parameter bound by instance-level access.
907907
func = func.__func__
908908
else:
909-
# Skip *self* when called from `singledispatchmethod.register`.
910-
idx = 0 if role == "function" else 1
909+
# Assume and skip bound parameter when singledispatchmethod is used.
910+
idx = 1 if functype == "method" else 0
911911
# Fast path: emulate `inspect._signature_from_function` if possible.
912912
if isinstance(func, FunctionType) and not hasattr(func, "__wrapped__"):
913913
func_code = func.__code__
@@ -923,7 +923,7 @@ def _get_singledispatch_annotated_param(func, *, role):
923923
except IndexError:
924924
pass
925925
else:
926-
# Allow variadic positional "(*args)" parameters for backward compatibility.
926+
# Allow variadic positional '(*args)' parameters for backward compatibility.
927927
if param.kind not in (inspect.Parameter.KEYWORD_ONLY, inspect.Parameter.VAR_KEYWORD):
928928
return param.name
929929
raise TypeError(
@@ -978,7 +978,7 @@ def _is_valid_dispatch_type(cls):
978978
return (isinstance(cls, UnionType) and
979979
all(isinstance(arg, type) for arg in cls.__args__))
980980

981-
def register(cls, func=None, __role__="function"):
981+
def register(cls, func=None, __functype__="function"):
982982
"""generic_func.register(cls, func) -> func
983983
984984
Registers a new implementation for the given *cls* on a *generic_func*.
@@ -1003,7 +1003,7 @@ def register(cls, func=None, __role__="function"):
10031003
)
10041004
func = cls
10051005

1006-
argname = _get_singledispatch_annotated_param(func, role=__role__)
1006+
argname = _get_singledispatch_annotated_param(func, functype=__functype__)
10071007

10081008
# only import typing if annotation parsing is necessary
10091009
from typing import get_type_hints
@@ -1015,7 +1015,7 @@ def register(cls, func=None, __role__="function"):
10151015
except KeyError:
10161016
raise TypeError(
10171017
f"Invalid first argument to `register()`: {func!r}. "
1018-
"Use either `@register(some_class)` or add a type "
1018+
f"Use either `@register(some_class)` or add a type "
10191019
f"annotation to parameter {argname!r} of your callable."
10201020
) from None
10211021

@@ -1055,7 +1055,7 @@ def wrapper(*args, **kw):
10551055
funcname = getattr(func, '__name__', 'singledispatch function')
10561056
registry[object] = func
10571057
wrapper.register = register
1058-
wrapper.register.__text_signature__ = "(cls, func)" # Hide private parameters from help().
1058+
wrapper.register.__text_signature__ = "(cls, func)" # Hide __functype__ from help().
10591059
wrapper.dispatch = dispatch
10601060
wrapper.registry = MappingProxyType(registry)
10611061
wrapper._clear_cache = dispatch_cache.clear
@@ -1083,7 +1083,7 @@ def register(self, cls, method=None):
10831083
10841084
Registers a new implementation for the given *cls* on a *generic_method*.
10851085
"""
1086-
return self.dispatcher.register(cls, func=method, __role__="method")
1086+
return self.dispatcher.register(cls, func=method, __functype__="method")
10871087

10881088
def __get__(self, obj, cls=None):
10891089
return _singledispatchmethod_get(self, obj, cls)

0 commit comments

Comments
 (0)