不带批注的运行时类型检查
pytypeutils的Python项目详细描述
pytypeutils
一个运行时类型检查库,它不基于注释并且具有 支持numpy和scipy。
功能
它有文档、所有api函数的单元测试和可读错误 信息。这检测到NUMPY和火炬,如果它们存在,则提供 每个的附加功能。
安装
pip install pytypeutils
用法
作为先决条件测试:
importpytypeutilsastusimporttypingdeffoo(a:int,b:typing.Union[int,float]):tus.check(a=(a,int),b=(b,(int,float)))return7foo(3,3)# no error!foo(3,3.0)# no error!foo(3.0,3)# ValueError: expected a is <class 'int'> but got 3.0 (type(a)=<class 'float'>)
内部单元测试:
importunittestimportpytypeutilsastusdeffoo(a:int):return2*aclassMyTester(unittest.TestCase):deftest_foo_giveint_returnsint(self):res=foo(2)tus.check(res=(res,int))
这将检测是否安装了numpy并添加pytypeutils.check_ndarrays
if
它是:
importpytypeutilsastusimporttypingimportnumpyasnpdeffeature_means(pts:np.ndarray):"""Returns the mean of the features for the given points, where pts is in the shape (num_samples, num_features)"""tus.check_ndarrays(pts=(pts,('num_samples','num_features'),('float32','float64')))res=pts.mean(0)tus.check_ndarrays(res=(res,[('num_features',pts.shape[1])],pts.dtype))returnresfeature_means(np.random.uniform(0,1,(10,4)))# worksfeature_means(np.random.uniform(0,1,(10,4,2)))# ValueError: expected pts.shape is (num_samples=any, num_features=any)# but has shape (10, 4, 2)
这将检测是否安装了手电筒并添加pytypeutils.check_tensors
if
它是:
importpytypeutilsastusimporttypingimporttorchdeffeature_means(pts:torch.tensor):'''Returns the mean of the features for the given points, where pts is in the shape (num_samples, num_features)'''tus.check_tensors(pts=(pts,('num_samples','num_features'),('float32','float64')))res=pts.mean(0)tus.check_tensors(res=(res,[('num_features',pts.shape[1])],pts.dtype))returnresfeature_means(torch.randn(10,4))# worksfeature_means(torch.randn(10,4,2))# ValueError: expected pts.shape is (num_samples=any, num_features=any)# but has shape (10, 4, 2)
为什么是这个类型库?
注释类型库,如pytypes和 typeguard如果合理的话可能会很棒 用键入提示来表达事物。然而,这往往是乏味的和 它不能很好地转换为单元测试、快速片段或快速调试 函数调用中的命令。这个库允许您获得合理的类型 可以快速写入和删除的错误,而无需执行此操作 对整个函数参数进行类型检查。此外,内置扩展 对于numpy和pytorch,如果使用 那些图书馆,需要一些健全的检查。
API
>>> help(pytypeutils) Help on package pytypeutils: NAME pytypeutils - Various generic functions and imports for pytypeutils. PACKAGE CONTENTS np_checks torch_checks utils FUNCTIONS check(**kwargs) Each keyword argument corresponds to an argument whose type will be checked. The key is used for printing out error messages and the value is a tuple describing the object to be checked and how it is to be checked. Example: ``` import pytypeutils as tus import typing def foo(a: int, b: typing.Union[int, float]): tus.check(a=(a, int), b=(b, (int, float))) ``` check_callable(**kwargs) Verifies that every value is callable. If not, raises an error check_listlike(**kwargs) Verifies that the given list-like objects have contents of a particular type. The keys are used for error messages, the values should be a tuple of the form (list, content types, optional length or (minl, maxl)). If there are more than 100 elements in any of the lists, 100 elements are sampled at random. Example: import pytypeutils as tus import typing def foo(a: typing.List[str], b: typing.List[typing.Union[int, float]]): tus.check(a=(a, (tuple, list)), b=(b, (tuple, list))) tus.check_listlike(a=(a, str), b=(b, (int, float), (0, 5))) # all elements of a are str # b has 0-5 elements inclusive, each of which is an int or float
>>> help(pytypeutils.check_ndarrays)
Help on function check_ndarrays in module pytypeutils.np_checks:
check_ndarrays(**kwargs)
Checks to verify the given arguments are numpy arrays with the given
specifications. The keys are used for error messages and the values are
tuples of the form (arr, expected shape, expected dtype). The expected
shape may be None not to check shape information, or a tuple of dimension
descriptions which can be None (for an any-size dimension), a str (for
an any-size dimension with a name for error messages), an int (for a
dimension of a particular size), or a tuple (str, int) where the str is
the name of the dimension and the int is the size of the dimension. The
dtype may be a tuple of numpy datatypes as strings or types, or None for
any dtype.
Example:
import pytypeutils as tus
import typing
import numpy as np
def feature_means(pts: np.ndarray):
'''Returns the mean of the features for the given points,
where pts is in the shape (num_samples, num_features)'''
tus.check_ndarrays(
pts=(pts, ('num_samples', 'num_features'), ('float32', 'float64')))
res = pts.mean(0)
tus.check_ndarrays(
res=(res, [('num_features', pts.shape[1])], pts.dtype)
)
return res
>>> help(pytypeutils.check_tensors)
Help on function check_tensors in module pytypeutils.torch_checks:
check_tensors(**kwargs)
Checks to verify the given arguments are torch tensors with the given
specifications. The keys are used for error messages and the values are
tuples of the form (arr, expected shape, expected dtype). The expected
shape may be None not to check shape information, or a tuple of dimension
descriptions which can be None (for an any-size dimension), a str (for
an any-size dimension with a name for error messages), an int (for a
dimension of a particular size), or a tuple (str, int) where the str is
the name of the dimension and the int is the size of the dimension. The
dtype may be a tuple of numpy datatypes as strings or types, or None for
any dtype.
Example:
import pytypeutils as tus
import typing
import torch
def feature_means(pts: torch.tensor):
'''Returns the mean of the features for the given points,
where pts is in the shape (num_samples, num_features)'''
tus.check_tensors(
pts=(pts, ('num_samples', 'num_features'), ('float32', 'float64')))
res = pts.mean(0)
tus.check_tensors(
res=(res, [('num_features', pts.shape[1])], pts.dtype)
)
return res