coady / multimethod

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

DispatchError on the first call, subsequent calls work fine #43

Closed adam-urbanczyk closed 3 years ago

adam-urbanczyk commented 3 years ago

As stated in the title, I get an error on the first invocation. Subsequent invocations do work. I'm using your master branch and py3.8. Any pointers will be welcome.

Here is a MWE :

from typing import Optional, Tuple
from multimethod import multimethod

Point = Tuple[float, float]

class A(object):

    @multimethod
    def segment(self, p1: Point, p2: Point) -> "A":

        return self

a = A()
try:
    a.segment((0.,1.),(0.,1.))
except Exception as e:
    print(e) #why do I even get to this point?

a.segment((0.,1.),(0.,1.)) #2nd call works as expected
coady commented 3 years ago

Yeah, looks like there's a bug with using forward references and subscripts together. I'll work on a fix, but there's a workaround in the meantime. Call evaluate once on methods that using forward references.

A.segment.evaluate()
a = A()
...
coady commented 3 years ago

It's still not fully fixed. An annotation could have forward references and a subscript. And with PEP 563's from __future__ import annotations string annotations are even more common.