coady / multimethod

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

Return type in mypy with typevars annotated args #42

Closed adam-urbanczyk closed 3 years ago

adam-urbanczyk commented 3 years ago

Any clue how difficult it would be to fix the mypy reported return type in the case of TypeVar annotations? See this link for more details (or the example below): https://github.com/CadQuery/cadquery/issues/379#issuecomment-830873519

from typing import TypeVar
from multimethod import multimethod

T0 = TypeVar('T0', bound='Shape')
T1 = TypeVar('T1', bound='MMShape')

class Shape:
    def set_scale(self: T0, scale: float) -> T0:
        self.scale = scale
        return self

class MMShape:
    @multimethod
    def set_scale(self: T1, scale: float) -> T1:
        self.scale = scale
        return self

s0 = Shape().set_scale(1.0)
reveal_type(s0)

s1 = MMShape().set_scale(1.0)
reveal_type(s1)
> mypy chain_test.py
chain_test.py:22: note: Revealed type is 'chain_test.Shape'
chain_test.py:25: note: Revealed type is 'Any'
coady commented 3 years ago

Yes, that was contributed recently actually: #37. Already works for functions, and just needs to annotate __get__ to support methods.

It's implemented on multidispatch only, because it (like singledispatch) requires a base implementation.

adam-urbanczyk commented 3 years ago

Thanks!