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