Commits

coady committed 3179d8f

Dispatch on python 3 annotations.

Comments (0)

Files changed (2)

         def decorator(func):
             if isinstance(func, cls):
                 self, func = func, func.last
-            elif func.__name__ in namespace:
-                self = namespace[func.__name__]
             else:
-                self = cls.new(func.__name__)
+                self = namespace.get(func.__name__, cls.new(func.__name__))
             self[types] = self.last = func
             return self
+        if len(types) == 1 and hasattr(types[0], '__annotations__'):
+            func, = types
+            types = tuple(map(func.__annotations__.__getitem__, func.__code__.co_varnames[:len(func.__annotations__)]))
+            return decorator(func)
         return decorator
+    def __init__(self, *types):
+        dict.__init__(self)
     def parents(self, types):
         "Find immediate parents of potential key."
         parents, ancestors = set(), set()
         assert func(0, None) is func(None, 0) is None
         self.assertRaises(DispatchError, func, 0, 0)
 
+    def testAnnotations(self):
+        self.assertRaises(DispatchError, annotated, 0, 0)
+        assert annotated(1, 2.0) == 2
+
+try:
+    exec('@multimethod\ndef annotated(x:int, y:float, z=None): return x * y')
+except SyntaxError:
+    del TestCase.testAnnotations
+
 if __name__ == '__main__':
     unittest.main()