TypeWriter:基于搜索验证的神经类型预测
TypeWriter: Neural Type Prediction with Search-based Validation
摘要:
由于缺少类型注释,维护以动态类型语言(如JavaScript或Python)编写的大型代码库可能具有挑战性:简单的数据兼容性错误激增,IDE支持有限,API难以理解。最近的工作试图通过静态类型推断或概率类型预测来解决这些问题。不幸的是,动态语言的静态类型推断是固有的限制,而概率方法遭受不精确。本文介绍了TypeWriter,第一次结合概率类型预测与基于搜索的细化预测类型。TypeWriter的预测器通过将代码的自然语言属性与程序设计语言级信息相结合,学习从部分注释的代码库中推断函数的返回和参数类型。为了验证预测的类型,TypeWriter使用预测类型的不同组合调用渐进的类型检查器,同时以反馈导向的方式导航可能的类型组合的空间。我们为Python实现了TypeWriter方法,并在两个代码语料库上进行了评估:Facebook的数百万行代码库和1,137个流行开源项目的集合。我们发现,TypeWriter的类型预测器在返回类型的前1(前5)预测中达到了0.64(0.79)的F1得分,参数类型达到了0.57(0.80),这明显优于以前的类型预测模型。通过将预测与基于搜索的验证相结合,TypeWriter可以在随机选择的语料库中完全注释14%到44%的文件,同时确保类型正确性。与静态类型推断工具的比较表明,TypeWriter添加了更多的非平凡类型。TypeWriter目前向Facebook的开发人员推荐类型,并且已经接受了数千种类型,只需进行最小的更改。
英文摘要:
Maintaining large code bases written in dynamically typed languages, such as JavaScript or Python, can be challenging due to the absence of type annotations: simple data compatibility errors proliferate, IDE support is limited, and APIs are hard to comprehend. Recent work attempts to address those issues through either static type inference or probabilistic type prediction. Unfortunately, static type inference for dynamic languages is inherently limited, while probabilistic approaches suffer from imprecision. This paper presents TypeWriter, the first combination of probabilistic type prediction with search-based refinement of predicted types. TypeWriter's predictor learns to infer the return and argument types for functions from partially annotated code bases by combining the natural language properties of code with programming language-level information. To validate predicted types, TypeWriter invokes a gradual type checker with different combinations of the predicted types, while navigating the space of possible type combinations in a feedback-directed manner. We implement the TypeWriter approach for Python and evaluate it on two code corpora: a multi-million line code base at Facebook and a collection of 1,137 popular open-source projects. We show that TypeWriter's type predictor achieves an F1 score of 0.64 (0.79) in the top-1 (top-5) predictions for return types, and 0.57 (0.80) for argument types, which clearly outperforms prior type prediction models. By combining predictions with search-based validation, TypeWriter can fully annotate between 14% to 44% of the files in a randomly selected corpus, while ensuring type correctness. A comparison with a static type inference tool shows that TypeWriter adds many more non-trivial types. TypeWriter currently suggests types to developers at Facebook and several thousands of types have already been accepted with minimal changes.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。