一句话:提出 DLInfer,用静态切片与双向 GRU 深度学习模型自动推断 Python 变量类型,Top-1 准确率达 98.79%。
Python 的动态类型特性虽提升开发效率,却给软件维护与质量保证带来挑战,而手动类型标注成本高且易出错。本文提出 DLInfer,一种基于深度学习的自动类型推断技术。它首先通过静态分析为变量收集切片语句,并利用 Unigram 语言模型算法将其向量化;随后设计双向门控循环单元(GRU)模型学习类型传播信息以完成推断。作者在 700 个开源项目上开展实证研究,评估对内置类型、库类型和用户自定义类型三类基本类型的推断准确率。实验表明,对于可通过静态分析和手动标注获得类型信息的变量,DLInfer 平均 Top-1 准确率达到 98.79%;对于仅能通过动态分析获得类型信息的变量,其平均推断准确率也达到 83.03%。结果说明 DLInfer 能高效推断类型,有望辅助多种 Python 软件工程任务。
Python programming language has gained enor-mous popularity in the past decades. While its flexibility signifi-cantly improves software development productivity, the dynamic typing feature challenges software maintenance and quality assurance. To facilitate programming and type error checking, the Python programming language has provided a type hint mechanism enabling developers to annotate type information for variables. However, this manual annotation process often requires plenty of resources and may introduce errors. In this paper, we propose a deep learning type inference technique, namely DLInfer, to automatically infer the type infor-mation for Python programs. DLInfer collects slice statements for variables through static analysis and then vectorizes them with the Unigram Language Model algorithm. Based on the vectorized slicing features, we designed a bi-directional gated recurrent unit model to learn the type propagation information for inference. To validate the effectiveness of DLInfer, we conduct an extensive empirical study on 700 open-source projects. We evaluate its accuracy in inferring three kinds of fundamental types, including built-in, library, and user-defined types. By training with a large-scale dataset, DLInfer achieves an average of 98.79% Top-1 accuracy for the variables that can get type information through static analysis and manual annotation. Further, DLInfer achieves 83.03% type inference accuracy on average for the variables that can only obtain the type information through dynamic analysis. The results indicate DLInfer is highly effective in inferring types. It is promising to apply it to assist in various software engineering tasks for Python programs.