TypeCheck/warnachinka
TypeCheck/sver
f1import typesf1import types
22
33
4def TypeCheck(argtypes, res):4def TypeCheck(argtypes, res):
5    if isinstance(argtypes, types.GeneratorType):5    if isinstance(argtypes, types.GeneratorType):
6        argtypes = list(argtypes)6        argtypes = list(argtypes)
77
8    def decorator(fun):8    def decorator(fun):
9        def newfun(*args, **kwargs):9        def newfun(*args, **kwargs):
n10            if len(kwargs) + len(args) != len(argtypes):n10            if len(args) + len(kwargs) != len(argtypes):
11                if not fun.__defaults__ or len(11                if not fun.__defaults__ or len(
t12                        fun.__defaults__) + len(kwargs) + len(args) != len(argtypes):t12                        fun.__defaults__) + len(args) + len(kwargs) != len(argtypes):
13                    raise TypeError('Function ' +13                    raise TypeError('Function ' +
14                                    fun.__code__.co_name +14                                    fun.__code__.co_name +
15                                    ' must have ' +15                                    ' must have ' +
16                                    str(len(argtypes)) +16                                    str(len(argtypes)) +
17                                    ' arguments')17                                    ' arguments')
1818
19            for index, arg in enumerate(args):19            for index, arg in enumerate(args):
20                if not isinstance(arg, argtypes[index]):20                if not isinstance(arg, argtypes[index]):
21                    raise TypeError(21                    raise TypeError(
22                        'Type of argument ' + str(index + 1) + ' is not ' + str(argtypes[index]))22                        'Type of argument ' + str(index + 1) + ' is not ' + str(argtypes[index]))
2323
24            named = 024            named = 0
25            for kw, val in kwargs.items():25            for kw, val in kwargs.items():
26                if kw in fun.__code__.co_varnames and not isinstance(26                if kw in fun.__code__.co_varnames and not isinstance(
27                        val, argtypes[fun.__code__.co_varnames.index(kw)]):27                        val, argtypes[fun.__code__.co_varnames.index(kw)]):
28                    named += 128                    named += 1
29                    raise TypeError('Type of argument \'' +29                    raise TypeError('Type of argument \'' +
30                                    kw +30                                    kw +
31                                    '\' is not ' +31                                    '\' is not ' +
32                                    str(argtypes[fun.__code__.co_varnames.index(kw)]))32                                    str(argtypes[fun.__code__.co_varnames.index(kw)]))
3333
34            result = fun(*args, **kwargs)34            result = fun(*args, **kwargs)
35            if not isinstance(result, res):35            if not isinstance(result, res):
36                raise TypeError('Type of result is not ' + str(res))36                raise TypeError('Type of result is not ' + str(res))
37            return result37            return result
3838
39        return newfun39        return newfun
4040
41    return decorator41    return decorator
4242
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op