-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-32380 Fix singledispatch interaction with methods #4987
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9351f1c
a69278b
f19e7a3
6ac7fea
042734d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -744,7 +744,7 @@ def _find_impl(cls, registry): | |
match = t | ||
return registry.get(match) | ||
|
||
def singledispatch(func): | ||
def singledispatch(func=None, arg=0): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name (Although this will become a moot point if you switch to a separate |
||
"""Single-dispatch generic function decorator. | ||
|
||
Transforms a function into a generic function, which can have different | ||
|
@@ -753,76 +753,78 @@ def singledispatch(func): | |
implementations can be registered using the register() attribute of the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docstring needs to be updated for the new parameter |
||
generic function. | ||
""" | ||
# There are many programs that use functools without singledispatch, so we | ||
# trade-off making singledispatch marginally slower for the benefit of | ||
# making start-up of such applications slightly faster. | ||
import types, weakref | ||
|
||
registry = {} | ||
dispatch_cache = weakref.WeakKeyDictionary() | ||
cache_token = None | ||
|
||
def dispatch(cls): | ||
"""generic_func.dispatch(cls) -> <function implementation> | ||
|
||
Runs the dispatch algorithm to return the best available implementation | ||
for the given *cls* registered on *generic_func*. | ||
|
||
""" | ||
nonlocal cache_token | ||
if cache_token is not None: | ||
current_token = get_cache_token() | ||
if cache_token != current_token: | ||
dispatch_cache.clear() | ||
cache_token = current_token | ||
try: | ||
impl = dispatch_cache[cls] | ||
except KeyError: | ||
if func is None: | ||
return partial(singledispatch, arg=arg) | ||
else: | ||
# There are many programs that use functools without singledispatch, so we | ||
# trade-off making singledispatch marginally slower for the benefit of | ||
# making start-up of such applications slightly faster. | ||
import types, weakref | ||
registry = {} | ||
dispatch_cache = weakref.WeakKeyDictionary() | ||
cache_token = None | ||
|
||
def dispatch(cls): | ||
"""generic_func.dispatch(cls) -> <function implementation> | ||
|
||
Runs the dispatch algorithm to return the best available implementation | ||
for the given *cls* registered on *generic_func*. | ||
|
||
""" | ||
nonlocal cache_token | ||
if cache_token is not None: | ||
current_token = get_cache_token() | ||
if cache_token != current_token: | ||
dispatch_cache.clear() | ||
cache_token = current_token | ||
try: | ||
impl = registry[cls] | ||
impl = dispatch_cache[cls] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the diff being weird, I did not change this. The block and main part of singledispatch was indented. |
||
except KeyError: | ||
impl = _find_impl(cls, registry) | ||
dispatch_cache[cls] = impl | ||
return impl | ||
|
||
def register(cls, func=None): | ||
"""generic_func.register(cls, func) -> func | ||
|
||
Registers a new implementation for the given *cls* on a *generic_func*. | ||
|
||
""" | ||
nonlocal cache_token | ||
if func is None: | ||
if isinstance(cls, type): | ||
return lambda f: register(cls, f) | ||
ann = getattr(cls, '__annotations__', {}) | ||
if not ann: | ||
raise TypeError( | ||
f"Invalid first argument to `register()`: {cls!r}. " | ||
f"Use either `@register(some_class)` or plain `@register` " | ||
f"on an annotated function." | ||
try: | ||
impl = registry[cls] | ||
except KeyError: | ||
impl = _find_impl(cls, registry) | ||
dispatch_cache[cls] = impl | ||
return impl | ||
|
||
def register(cls, func=None): | ||
"""generic_func.register(cls, func) -> func | ||
|
||
Registers a new implementation for the given *cls* on a *generic_func*. | ||
|
||
""" | ||
nonlocal cache_token | ||
if func is None: | ||
if isinstance(cls, type): | ||
return lambda f: register(cls, f) | ||
ann = getattr(cls, '__annotations__', {}) | ||
if not ann: | ||
raise TypeError( | ||
f"Invalid first argument to `register()`: {cls!r}. " | ||
f"Use either `@register(some_class)` or plain `@register` " | ||
f"on an annotated function." | ||
) | ||
func = cls | ||
|
||
# only import typing if annotation parsing is necessary | ||
from typing import get_type_hints | ||
argname, cls = next(iter(get_type_hints(func).items())) | ||
assert isinstance(cls, type), ( | ||
f"Invalid annotation for {argname!r}. {cls!r} is not a class." | ||
) | ||
func = cls | ||
|
||
# only import typing if annotation parsing is necessary | ||
from typing import get_type_hints | ||
argname, cls = next(iter(get_type_hints(func).items())) | ||
assert isinstance(cls, type), ( | ||
f"Invalid annotation for {argname!r}. {cls!r} is not a class." | ||
) | ||
registry[cls] = func | ||
if cache_token is None and hasattr(cls, '__abstractmethods__'): | ||
cache_token = get_cache_token() | ||
dispatch_cache.clear() | ||
return func | ||
|
||
def wrapper(*args, **kw): | ||
return dispatch(args[0].__class__)(*args, **kw) | ||
|
||
registry[object] = func | ||
wrapper.register = register | ||
wrapper.dispatch = dispatch | ||
wrapper.registry = types.MappingProxyType(registry) | ||
wrapper._clear_cache = dispatch_cache.clear | ||
update_wrapper(wrapper, func) | ||
return wrapper | ||
registry[cls] = func | ||
if cache_token is None and hasattr(cls, '__abstractmethods__'): | ||
cache_token = get_cache_token() | ||
dispatch_cache.clear() | ||
return func | ||
|
||
def wrapper(*args, **kw): | ||
return dispatch(args[arg].__class__)(*args, **kw) | ||
|
||
registry[object] = func | ||
wrapper.register = register | ||
wrapper.dispatch = dispatch | ||
wrapper.registry = types.MappingProxyType(registry) | ||
wrapper._clear_cache = dispatch_cache.clear | ||
update_wrapper(wrapper, func) | ||
return wrapper |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2147,6 +2147,43 @@ def __eq__(self, other): | |
return self.arg == other | ||
self.assertEqual(i("str"), "str") | ||
|
||
def test_method_register(self): | ||
class A: | ||
@functools.singledispatch(arg=1) | ||
def t(self, arg): | ||
return "base" | ||
@t.register(int) | ||
def _(self, arg): | ||
return "int" | ||
@t.register(str) | ||
def _(self, arg): | ||
return "str" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As @ilevkivskyi noted, additional tests are needed to cover the interaction with other method decorators, especially I think you will find that adding a parameter to the existing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @ncoghlan and @ethanhs, I'd been lurking the cpython PRs and had some time today, so I made a quick gist of a possible descriptor-aware implementation of Nice to haves (for my implementation, at least), would be a mechanism which wraps up any methods Anyway, here's the gist. Heavily inspired by the |
||
a = A() | ||
|
||
self.assertEqual(a.t(0), "int") | ||
self.assertEqual(a.t(''), "str") | ||
self.assertEqual(a.t(0.0), "base") | ||
|
||
self.assertEqual(A.t(a,0), "int") | ||
self.assertEqual(A.t(a,''), "str") | ||
self.assertEqual(A.t(a,0.0), "base") | ||
|
||
def test_method_register_noargs(self): | ||
msg = "tuple index out of range" | ||
class B: | ||
@functools.singledispatch(arg=1) | ||
def fun(): | ||
return 'base' | ||
@fun.register(str) | ||
def _(): | ||
return "str" | ||
@fun.register(float) | ||
def _(): | ||
return "float" | ||
with self.assertRaises(IndexError) as exc: | ||
B.fun() | ||
self.assertEqual(str(exc.exception), msg) | ||
|
||
def test_invalid_registrations(self): | ||
msg_prefix = "Invalid first argument to `register()`: " | ||
msg_suffix = ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
``functools.singledispatch`` now supports decorating and registering with | ||
methods. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/singeldispatch/singledispatch/ (typo fix)