astypes
0.2.6
python库以静态检测AST节点的类型。
一个好的用例是只需对特定类型运行一些规则的衬里。例如,仅当something具有类型str时,检查something.format(a=b) 。
python3 -m pip install astypesAstypes使用星体来推断节点的定义。因此,如果您的代码与AST节点一起使用,则需要首先将它们转换为Astroid:
import astroid
import astypes
module = astroid . parse ( source_code )
node = astypes . find_node ( module , ast_node )当您有一个星体节点时,您可以得到它的类型:
node_type = astype . get_node ( node )
print ( node_type . annotation )例子:
import astroid
import astypes
node = astroid . extract_node ( '1 + 2.3' )
t = astypes . get_type ( node )
print ( t . annotation ) # 'float'对于真实的用法示例,请查看sublytypes。这是一个CLI工具,可以使用ASTYPES自动将类型注释添加到Python代码中。
您可以在astypes/_handlers.py中找到大多数逻辑。简而言之:
13始终是int 。list(x)返回类型list 。如果您与其他内容列出了阴影list ,那可能是不正确的。