coady / multimethod

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

Support for NewType #62

Closed jaredliw closed 2 years ago

jaredliw commented 2 years ago

Python 3.9, when I define such a custom type

PositiveInt = NewType('PositiveInt', int)

and use it to annotate methods, the code breaks:

Traceback (most recent call last):
  File "C:\Users\jared\Desktop\source_code\test_sdm.py", line 19, in <module>
    Hello(1, 2, 3)
  File "C:\Users\jared\AppData\Local\Programs\Python\Python39\lib\site-packages\multimethod\__init__.py", line 298, in __call__
    func = self[tuple(func(arg) for func, arg in zip(self.type_checkers, args))]
  File "C:\Users\jared\AppData\Local\Programs\Python\Python39\lib\site-packages\multimethod\__init__.py", line 284, in __missing__
    for key in self.parents(types):
  File "C:\Users\jared\AppData\Local\Programs\Python\Python39\lib\site-packages\multimethod\__init__.py", line 233, in parents
    parents = {key for key in self if isinstance(key, signature) and key < types}
  File "C:\Users\jared\AppData\Local\Programs\Python\Python39\lib\site-packages\multimethod\__init__.py", line 233, in <setcomp>
    parents = {key for key in self if isinstance(key, signature) and key < types}
  File "C:\Users\jared\AppData\Local\Programs\Python\Python39\lib\site-packages\multimethod\__init__.py", line 169, in __lt__
    return self != other and self <= other
  File "C:\Users\jared\AppData\Local\Programs\Python\Python39\lib\site-packages\multimethod\__init__.py", line 166, in __le__
    return len(self) <= len(other) and all(map(issubclass, other, self))
TypeError: issubclass() arg 2 must be a class or tuple of classes

Should we consider interpreting PositiveInt as PositiveInt.__supertype__ (which is int)?