question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NotFoundLookupError when using dispatch.multi in class method

See original GitHub issue

master 7e9b8657

from plum import dispatch

@dispatch.multi((int, str), (int, int))
def f(x: int, y: {int, str}):
    print(f'{x}, {y}')

class C():
    @dispatch
    def g(self, x: int, y: int):
        print(f'{x}, {y}')

    @dispatch
    def g(self, x: int, y: str):
        print(f'{x}, {y}')

    @dispatch.multi((int, str), (int, int))
    def h(self, x: int, y: {int, str}):
        print(f'{x}, {y}')

f(3, 4) # OK

c = C()
c.g(3, 4) # OK
c.g(3, 'cat') # OK

c.h(3, 4) # NotFoundLookupError
c.h(3, 'cat') # NotFoundLookupError

I did not yet try this on the latest released branch.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jlapeyrecommented, Feb 26, 2021

Thanks. Your last example has solved my problem.

1reaction
wesselbcommented, Feb 26, 2021

Ah, sorry, I should have said that this should also be used when classes are used, even if you don’t make use of inheritance. The problem is that vanilla functions and methods of classes behave slightly differently.

Plum should play nicely with ABC. Here’s an example:

import abc

from plum import Dispatcher, Referentiable, Self

class A(metaclass=Referentiable(abc.ABCMeta)):
    @abc.abstractmethod
    def f(self, x):
        pass

class B(A):
    _dispatch = Dispatcher(in_class=Self)

    @_dispatch
    def f(self, x: int):
        return x
>>> A()
TypeError: Can't instantiate abstract class A with abstract methods f

>>> b = B()

>>> b.f(1)
1

>>> b.f("1")
NotFoundLookupError: For function "f" of <class '__main__.B'>, signature (builtins.str) could not be resolved.
Read more comments on GitHub >

github_iconTop Results From Across the Web

plum-dispatch - PyPI
Multiple dispatch allows you to implement multiple methods for the same function, where the methods specify the types of their arguments:
Read more >
Where to dispatch multiple actions in redux? - Stack Overflow
The recommended way as per the documentation is in the action creator, like so: function actionCreator(payload) { return dispatch ...
Read more >
Using @singledispatchmethod for Multiple Class Constructors
Starting with Python 3.8, you can use this @singledispatch or @singledispatchmethod decorator to turn a function or method, respectively, into a single-dispatch ......
Read more >
Open Multi-Methods for C++ - Bjarne Stroustrup's Homepage
multi -methods generalize multiple dispatch towards open-class ex- ... care was taken to integrate open multi-methods with existing C++.
Read more >
object oriented - Multiple Dispatch and CLOS
In CLOS methods are not in a class namespace. Thus to have shorter access to variables and accessors, CLOS uses macros WITH-SLOTS and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found