plum: f(**dict) does not work

How do I call an overloaded constructor if I have a dict with the call args ?

If the constructor is not overloaded like this it works fine using **

class B:
  def __init__(self, x):
    self.b=x

B(1) <main.B object at 0x7faebc3e7880> B(**{‘x’:1}) <main.B object at 0x7faebc3d04f0>

but when the constructor is overloaded this doesn’t. The error message does not make any sense either.

class B:
  @dispatch
  def __init__(self, x):
    self.b=x

B(1) <main.B object at 0x7faebc3e7be0> B(**{‘x’:1})

Traceback (most recent call last): File “/documents/Utilities/pyson/venv/lib/python3.8/site-packages/plum/function.py”, line 421, in _resolve_method_with_cache return self._cache[types] KeyError: (<class ‘main.B’>,)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “/documents/Utilities/pyson/venv/lib/python3.8/site-packages/plum/function.py”, line 341, in resolve_method signature = self._resolver.resolve(target) File “/documents/Utilities/pyson/venv/lib/python3.8/site-packages/plum/resolver.py”, line 168, in resolve raise NotFoundLookupError(f"{target} could not be resolved.") plum.resolver.NotFoundLookupError: (<__main__.B object at 0x7faebc3e7820>,) could not be resolved.

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “/documents/Utilities/pyson/venv/lib/python3.8/site-packages/plum/function.py”, line 489, in call return self._f(self._instance, *args, **kw_args) File “/documents/Utilities/pyson/venv/lib/python3.8/site-packages/plum/function.py”, line 398, in call method, return_type = self._resolve_method_with_cache(args=args) File “/documents/Utilities/pyson/venv/lib/python3.8/site-packages/plum/function.py”, line 427, in _resolve_method_with_cache method, return_type = self.resolve_method(args) File “/documents/Utilities/pyson/venv/lib/python3.8/site-packages/plum/function.py”, line 350, in resolve_method method, return_type = self._handle_not_found_lookup_error(e) File “/documents/Utilities/pyson/venv/lib/python3.8/site-packages/plum/function.py”, line 394, in _handle_not_found_lookup_error raise ex plum.resolver.NotFoundLookupError: For function __init__ of __main__.B, (<__main__.B object at 0x7faebc3e7820>,) could not be resolved.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 20 (10 by maintainers)

Most upvoted comments

@Moosems Thanks! Good suggestion. I’ve added this to the docs.

Hmm, one possible solution would to not pass the description as a dictionary but as plain arguments, and pass these to the class:

from plum import dispatch


def instantiate(cls, *args, **kw_args):
    return cls(*args, **kw_args)


class MyClass:
    @dispatch
    def __init__(self, x: int):
        self.x = x


a = instantiate(MyClass, 1)

Would something like this be acceptable?