Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def annotated_func(args):
- def func_wrapper(func):
- def wrapper(*func_args):
- if len(args) == len(func_args):
- raise TypeError
- for i in args:
- if !isinstance(i, (type, TypedAny, TypedUnion,
- TypedList, TypedTuple, TypedDict,
- TypedSet, TypedCallable, TypedAnnotatedCallable)):
- raise TypeError
- func.__types__ = args
- return wrapper
- return func_wrapper
- class TypedAny:
- def __instancecheck__(self, instance):
- return True
- class TypedUnion:
- def __init__(self, types):
- self.types = types
- def __instancecheck__(self, instance):
- return isinstance(instance, tuple(self.types))
- class TypedList:
- def __init__(self, T):
- self.T = T
- def __instancecheck__(self, instance):
- if isinstance(instance, list):
- for i in instance:
- if not isinstance(i, self.T):
- return False
- return True
- else:
- return False
- class TypedTuple:
- def __init__(self, T):
- self.T = T
- def __instancecheck__(self, instance):
- if isinstance(instance, tuple):
- for i in instance:
- if not isinstance(i, self.T):
- return False
- return True
- else:
- return False
- class TypedDict:
- def __init__(self, K, V):
- self.K = K
- self.V = V
- def __instancecheck__(self, instance):
- if isinstance(instance, dict):
- for key, value in instance.items():
- if not isinstance(key, self.K) or not isinstance(value, self.V):
- return False
- return True
- else:
- return False
- class TypedSet:
- def __init__(self, T):
- self.T = T
- def __instancecheck__(self, instance):
- if isinstance(instance, set):
- for i in instance:
- if not isinstance(i, self.T):
- return False
- return True
- else:
- return False
- class TypedCallable:
- def __instancecheck__(self, instance):
- return callable(instance)
- class TypedAnnotatedCallable:
- def __init__(self, types):
- self.types = types
- def __instancecheck__(self, instance):
- if callable(instance):
- types = iter(self.types)
- for i in instance.__types__:
- if not isinstance(i, next(types)):
- return False
- return True
- else:
- return False
- def type_check(func):
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement