plum: `TypeError: Test.__call__() got an unexpected keyword argument 'a'`

Hello!

With the following code:

from rich.traceback import install

install(show_locals=True)

from plum import dispatch


class Test:
    @dispatch
    def __call__(self):
        pass

    @dispatch
    def __call__(self, *args, **kwargs):
        pass


test = Test()
test(a=1)

I get the following traceback:

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /mnt/wsl/sylvorg/sylvorg/sylveon/siluam/oreo/./test15.py:20 in <module>                          │
│                                                                                                  │
│   17 │   │   pass                                                                                │
│   18                                                                                             │
│   19 test = Test()                                                                               │
│ ❱ 20 test(a=1)                                                                                   │
│   21                                                                                             │
│                                                                                                  │
│ ╭───────────────────────────── locals ─────────────────────────────╮                             │
│ │ dispatch = <plum.dispatcher.Dispatcher object at 0x7f4713206350> │                             │
│ │  install = <function install at 0x7f4713d0bce0>                  │                             │
│ │     Test = <class '__main__.Test'>                               │                             │
│ │     test = <__main__.Test object at 0x7f47134f7950>              │                             │
│ ╰──────────────────────────────────────────────────────────────────╯                             │
│                                                                                                  │
│ /nix/store/0rqp0565hq7xhg1n0ld36icv3031f87s-python3-3.11.8-env/lib/python3.11/site-packages/plum │
│ /function.py:484 in __call__                                                                     │
│                                                                                                  │
│   481 │   │   pass                                                                               │
│   482 │                                                                                          │
│   483 │   def __call__(self, _, *args, **kw_args):                                               │
│ ❱ 484 │   │   return self._f(self._instance, *args, **kw_args)                                   │
│   485 │                                                                                          │
│   486 │   def invoke(self, *types):                                                              │
│   487 │   │   """See :meth:`.Function.invoke`."""                                                │
│                                                                                                  │
│ ╭───────────────────────────── locals ──────────────────────────────╮                            │
│ │       _ = <__main__.Test object at 0x7f47134f7950>                │                            │
│ │    args = ()                                                      │                            │
│ │ kw_args = {'a': 1}                                                │                            │
│ │    self = <plum.function._BoundFunction object at 0x7f47132bc0d0> │                            │
│ ╰───────────────────────────────────────────────────────────────────╯                            │
│                                                                                                  │
│ /nix/store/0rqp0565hq7xhg1n0ld36icv3031f87s-python3-3.11.8-env/lib/python3.11/site-packages/plum │
│ /function.py:368 in __call__                                                                     │
│                                                                                                  │
│   365 │   def __call__(self, *args, **kw_args):                                                  │
│   366 │   │   __tracebackhide__ = True                                                           │
│   367 │   │   method, return_type = self._resolve_method_with_cache(args=args)                   │
│ ❱ 368 │   │   return _convert(method(*args, **kw_args), return_type)                             │
│   369 │                                                                                          │
│   370 │   def _resolve_method_with_cache(                                                        │
│   371 │   │   self,                                                                              │
│                                                                                                  │
│ ╭─────────────────────────────────────────── locals ───────────────────────────────────────────╮ │
│ │        args = (<__main__.Test object at 0x7f47134f7950>,)                                    │ │
│ │     kw_args = {'a': 1}                                                                       │ │
│ │      method = <function Test.__call__ at 0x7f471383c540>                                     │ │
│ │ return_type = typing.Any                                                                     │ │
│ │        self = <multiple-dispatch function Test.__call__ (with 2 registered and 0 pending     │ │
│ │               method(s))>                                                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────╯ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
TypeError: Test.__call__() got an unexpected keyword argument 'a'

It doesn’t seem to be happening with the following blocks either:

from rich.traceback import install

install(show_locals=True)

from plum import dispatch


class Test:
    @dispatch
    def __call__(self, *args):
        pass

    @dispatch
    def __call__(self, *args, **kwargs):
        pass


test = Test()
test(a=1)
from rich.traceback import install

install(show_locals=True)

from plum import dispatch


class Test:
    @dispatch
    def __call__(self):
        pass

    @dispatch
    def __call__(self, **kwargs):
        pass


test = Test()
test(a=1)

Thank you kindly for the help!

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Since keyword arguments are ignored in the dispatch, you won’t be able to create versions of methods for specific keyword arguments. However, you could emulate this behaviour in a way like this:

from plum import dispatch


@dispatch
def f(x, **kw_args):
    if kw_args:
        return f.invoke(object, object)(x, **kw_args)
    print("Only one argument and no keywords!")


@dispatch
def f(*args, **kw_args):
    print("Fallback:", args, kw_args)

Actually, could you help me create a patch for my own version of plum that would redirect any unknown keyword arguments to a function with **kwargs?

I think this might be considerably hard to get right. If I simple solution like the above suffices, my advice would be to go with that.