Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. def annotated_func(args):
  2.     def func_wrapper(func):
  3.         def wrapper(*func_args):
  4.             if len(args) != len(func_args):
  5.                 raise TypeError
  6.             for i in args:
  7.                 if not isinstance(i, (type, TypedAny, TypedUnion,
  8.                                   TypedList, TypedTuple, TypedDict,
  9.                                   TypedSet, TypedCallable, TypedAnnotatedCallable)):
  10.                     raise TypeError
  11.             func(*func_args)
  12.         wrapper.__types__ = args
  13.         return wrapper
  14.     return func_wrapper
  15.  
  16.  
  17. class TypedAny:
  18.     def __instancecheck__(self, instance):
  19.         return True
  20.  
  21.  
  22. class TypedUnion:
  23.     def __init__(self, types):
  24.         self.x = types
  25.  
  26.     def __instancecheck__(self, instance):
  27.         return isinstance(instance, tuple(self.x))
  28.  
  29.  
  30. class TypedList:
  31.     def __init__(self, t):
  32.         self.x = t
  33.  
  34.     def __instancecheck__(self, instance):
  35.         if isinstance(instance, list):
  36.             for i in instance:
  37.                 if not isinstance(i, self.x):
  38.                     return False
  39.             return True
  40.         else:
  41.             return False
  42.  
  43.  
  44. class TypedTuple:
  45.     def __init__(self, t):
  46.         self.x = t
  47.  
  48.     def __instancecheck__(self, instance):
  49.         if isinstance(instance, tuple):
  50.             for i in instance:
  51.                 if not isinstance(i, self.x):
  52.                     return False
  53.             return True
  54.         else:
  55.             return False
  56.  
  57.  
  58. class TypedDict:
  59.     def __init__(self, k, v):
  60.         self.k = k
  61.         self.v = v
  62.         self.x = tuple(k, v)
  63.  
  64.     def __instancecheck__(self, instance):
  65.         if isinstance(instance, dict):
  66.             for key, value in instance.items():
  67.                 if not isinstance(key, self.k) or not isinstance(value, self.v):
  68.                     return False
  69.             return True
  70.         else:
  71.             return False
  72.  
  73.  
  74. class TypedSet:
  75.     def __init__(self, t):
  76.         self.x = t
  77.  
  78.     def __instancecheck__(self, instance):
  79.         if isinstance(instance, set):
  80.             for i in instance:
  81.                 if not isinstance(i, self.x):
  82.                     return False
  83.             return True
  84.         else:
  85.             return False
  86.  
  87.  
  88. class TypedCallable:
  89.     def __instancecheck__(self, instance):
  90.         return callable(instance)
  91.  
  92.  
  93. class TypedAnnotatedCallable:
  94.     def __init__(self, types):
  95.         self.types = types
  96.         self.x = types
  97.  
  98.     def __instancecheck__(self, instance):
  99.         if callable(instance):
  100.             if len(self.types) == len(instance.__types__):
  101.                 types = iter(self.types)
  102.                 for i in instance.__types__:
  103.                     it = next(types)
  104.                     if not check(i, it):
  105.                         return False
  106.                 return True
  107.             else:
  108.                 return False
  109.         else:
  110.             return False
  111.  
  112.  
  113. def check(i, it):
  114.     if isinstance(i, type) and (isinstance(it, type)):
  115.         if not(i is it):
  116.             return False
  117.         else:
  118.             return True
  119.     elif (type(i) == type(it) or (issubclass(type(i), type(it)))
  120.           or issubclass(type(it), type(i))):
  121.         return True
  122.     elif (type(i) != type(it) and not(issubclass(type(i), type(it)))
  123.           and not(issubclass(type(it), type(i)))):
  124.         return False
  125.     else:
  126.         return check(i.x, it.x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement