coady / multimethod

Multiple argument dispatching.
https://coady.github.io/multimethod
Other
277 stars 24 forks source link

Flaky performance in version 1.6 for sequences #53

Closed ivanovmg closed 3 years ago

ivanovmg commented 3 years ago

Consider the following script.

from typing import Sequence, Union

from multimethod import multimethod

@multimethod
def func(x: Union[int, Sequence[int]]) -> Sequence[int]:
    raise NotImplementedError

@func.register
def _from_int(x: int) -> Sequence[int]:
    return [x, x]

@func.register
def _from_sequence(x: Sequence[int]) -> Sequence[int]:
    return x

func(1)
func([1, 2])

When executing python script.py I noticed weird behavior.

Traceback (most recent call last):
  File "script.py", line 22, in <module>
    func([1, 2])
  File "C:\venv\lib\site-packages\multimethod\__init__.py",
line 301, in __call__
    func = self[tuple(func(arg) for func, arg in zip(self.type_checkers, args))]

  File "C:\venv\lib\site-packages\multimethod\__init__.py",
line 295, in __missing__
    raise DispatchError(msg, types, keys)
multimethod.DispatchError: ('func: 0 methods found', (<class 'list'>,), [])

I would like to emphasize that this behavior is flaky. Sometimes this error is raised, but sometimes the code executes just fine.

$ pip list
Package     Version
----------- -------
multimethod 1.6
pip         19.2.3
setuptools  41.2.0
$ python --version
Python 3.8.3

In the previous version of multimethod there was no such issue.

coady commented 3 years ago

Not sure why it would be intermittent; it does look like a bug with new type checker.

coady commented 3 years ago

Fixed in fd95a14.