Skip to content

Commit bad8ae6

Browse files
authored
feat: add dispatch to bound functions (#200)
Signed-off-by: nstarman <[email protected]>
1 parent 3534796 commit bad8ae6

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

plum/function.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from copy import copy
44
from functools import wraps
55
from types import MethodType
6-
from typing import Any, Callable, List, Optional, Tuple, TypeVar, Union
6+
from typing import Any, Callable, List, Optional, Protocol, Tuple, TypeVar, Union
77

88
from .method import Method
99
from .resolver import AmbiguousLookupError, NotFoundLookupError, Resolver
@@ -473,6 +473,14 @@ def _generate_qualname(f: Callable) -> str:
473473
return qualname
474474

475475

476+
class _DispatchFunction(Protocol):
477+
"""Protocol for the `dispatch` method of a function."""
478+
479+
def __call__(
480+
self, method: Optional[Callable], precedence: int
481+
) -> Union[Self, Callable[[Callable], Self]]: ...
482+
483+
476484
class _BoundFunction:
477485
"""A bound instance of `.function.Function`.
478486
@@ -520,3 +528,8 @@ def wrapped_method(*args, **kw_args):
520528
def methods(self) -> List[Signature]:
521529
"""list[:class:`.signature.Signature`]: All available methods."""
522530
return self._f.methods
531+
532+
@property
533+
def dispatch(self) -> _DispatchFunction:
534+
"""See :meth:`.Function.dispatch`."""
535+
return self._f.dispatch

tests/test_dispatcher.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,14 @@ class A:
4949
def f(self, x: int):
5050
pass
5151

52+
a = A()
53+
5254
# The 'methods' should point to those of the underlying object.
53-
assert A().f.methods is A.f.methods
54-
assert A().f.methods is A().f._f.methods
55+
assert a.f.methods is A.f.methods
56+
assert a.f.methods is a.f._f.methods
57+
58+
# The 'dispatcher' should point to the dispatcher.
59+
assert hash(a.f.dispatch) == hash(a.f._f.dispatch)
5560

5661

5762
def test_dispatch_multi():

0 commit comments

Comments
 (0)