TypeCheck/abrivan
TypeCheck/Stephan
f1import typesf1import types
22
33
n4def TypeCheck(arg, r):n4def TypeCheck(argtypes, res):
5    if isinstance(arg, types.GeneratorType):5    if isinstance(argtypes, types.GeneratorType):
6        arg = list(arg)6        argtypes = list(argtypes)
77
n8    def de(f):n8    def decorator(fun):
9        def n_f(*args, **kwargs):9        def newfun(*args, **kwargs):
10            if len(kwargs)+len(args) != len(arg):10            if len(args) + len(kwargs) != len(argtypes):
11                if not f.__defaults__ or len(11                if not fun.__defaults__ or len(
12                        f.__defaults__) + len(kwargs) + len(args) != len(arg):12                        fun.__defaults__) + len(args) + len(kwargs) != len(argtypes):
13                    raise TypeError('Function ' +13                    raise TypeError('Function ' +
n14                                    f.__code__.co_name +n14                                    fun.__code__.co_name +
15                                    ' must have ' +15                                    ' must have ' +
n16                                    str(len(arg)) +n16                                    str(len(argtypes)) +
17                                    ' arguments')17                                    ' arguments')
n18            for index, argss in enumerate(args):n18            for index, arg in enumerate(args):
19                if not isinstance(argss, arg[index]):19                if not isinstance(arg, argtypes[index]):
20                    raise TypeError('Type of argument ' +20                    raise TypeError(
21                                    str(index + 1) + ' is not ' + str(arg[index]))21                        'Type of argument ' + str(index + 1) + ' is not ' + str(argtypes[index]))
22            n = 022            named = 0
23            for k, v in kwargs.items():23            for kw, val in kwargs.items():
24                if k in f.__code__.co_varnames and not isinstance(24                if kw in fun.__code__.co_varnames and not isinstance(
25                        v, arg[f.__code__.co_varnames.index(k)]):25                        val, argtypes[fun.__code__.co_varnames.index(kw)]):
26                    n += 126                    named += 1
27                    raise TypeError('Type of argument \'' +27                    raise TypeError('Type of argument \'' +
n28                                    k +n28                                    kw +
29                                    '\' is not ' +29                                    '\' is not ' +
t30                                    str(arg[f.__code__.co_varnames.index(k)]))t30                                    str(argtypes[fun.__code__.co_varnames.index(kw)]))
31            otv = f(*args, **kwargs)31            result = fun(*args, **kwargs)
32            if not isinstance(otv, r):32            if not isinstance(result, res):
33                raise TypeError('Type of result is not ' + str(r))33                raise TypeError('Type of result is not ' + str(res))
34            return otv34            return result
35        return n_f35        return newfun
36    return de36    return decorator
3737
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op