该模块提供动态类型检查机制。

typechecker的Python项目详细描述


该模块提供动态类型检查机制。

正常类型检查:

>>> from typechecker import typeinfo, list_, tuple_, has_attrs
>>> @typeinfo(int, x=int, y=int)
... def add(x, y):
...     return x + y
>>> add(10, 20)
30
>>> add('aa', 'bb')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "typechecker/typeinfo.py", line 113, in call
    _check_arg(f, assigned, types)
  File "typechecker/typeinfo.py", line 84, in _check_arg
    f.__name__))
TypeError: In function add, argument "y" required "int". "str" found.

元组类型检查:

>>> @typeinfo(int, val=tuple_(int, str))
... def test(val):
...     return len(val)
...
>>> test((19, 'aaa'))
2
>>> test('aaa')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "typechecker/typeinfo.py", line 113, in call
    _check_arg(f, assigned, types)
  File "typechecker/typeinfo.py", line 84, in _check_arg
    f.__name__))
TypeError: In function test, argument "val" required "tuple[int, str]". "str" found.

列表类型检查:

>>> @typeinfo(int, val=list_(int))
... def test(val):
...     return 10
...
>>> test([1,2,3])
10
>>> test([1,2,'aaa'])
Traceback (most recent call last):
  File "typechecker/typeinfo.py", line 113, in call
    _check_arg(f, assigned, types)
  File "typechecker/typeinfo.py", line 84, in _check_arg
    f.__name__))
TypeError: In function test, argument "val" required "list[int]". "list" found.

复杂类型检查:

>>> @typeinfo(int, val=tuple_(list_(int)))
... def test(val):
...     return 10
...
>>> test(([1],))
10
>>> test((['aa'],))
Traceback (most recent call last):
  File "/usr/local/python2.7/lib/python2.7/doctest.py", line 1254, in __run
    compileflags, 1) in test.globs
  File "<doctest typechecker[12]>", line 1, in <module>
    test((['aa'],))
  File "typechecker/typeinfo.py", line 113, in call
    _check_arg(f, assigned, types)
  File "typechecker/typeinfo.py", line 84, in _check_arg
    f.__name__))
TypeError: In function test, argument "val" required "tuple[list[int]]". "tuple" found.

结构子类型:

>>> @typeinfo(int, f=Callable)
... def test(f):
...     return 10
...
>>> test(map)
10
>>> test(1)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "typechecker/typeinfo.py", line 113, in call
    _check_arg(f, assigned, types)
  File "typechecker/typeinfo.py", line 84, in _check_arg
    f.__name__))
TypeError: In function test, argument "f" required "hasattr[__call__]". "int" found.

然后检查:

>>> @typeinfo(int, f=and_(Callable, ContextManager))
... def test(f):
...     return 10
...
>>> test(file)
10
>>> test(map)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "typechecker/typeinfo.py", line 113, in call
    _check_arg(f, assigned, types)
  File "typechecker/typeinfo.py", line 84, in _check_arg
    f.__name__))
TypeError: In function test, argument "f" required "And[hasattr[__call__], hasattr[__enter__, __exit__]]". "builtin_function_or_method" found.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何通过InjectMocks注释注入真实对象   多线程如何减少Java parallelStream中的#个线程?   java检测所有安卓摄像头   java中多个构造函数的参数   Spark SQL load json抛出错误java。lang.NoClassDefFoundError:scala/collection/GenTraversableOnce$class   java如何在ServerSocket中获得挂起的请求数或接受请求而不阻塞?   java JaxWsPortProxyFactoryBean查询超时   java Spring MVC LightAdmin配置   java类类型列表不一致性   安卓自定义视图,以在Java中动态插入到布局中   如果我使用安卓,java如何使用replace()方法。支持v4。应用程序。碎片   为什么Java中的数组不能使用类型擦除?   基于java JBoss EJB的Web服务日期格式   如何在java中实现负载均衡器   java OSGI OBR存储库托管?