Skip to main content
QUICK REVIEW

[论文解读] PYInfer: Deep Learning Semantic Type Inference for Python Variables

Siwei Cui, Gang Zhao|arXiv (Cornell University)|Jun 27, 2021
Software Engineering Research参考文献 36被引用 11
一句话总结

PYInfer 是一个基于深度学习的框架,通过利用上下文代码语义来推断 Python 变量类型,其神经网络在来自 PySonar2 的大规模自动构建数据集上进行训练。在 11 种基本类型上达到 91.2% 的准确率,在 500 种常见类型上达到 81.2% 的准确率,其速度比最先进的工具快 187 倍,代码覆盖率高出 5.2 倍。

ABSTRACT

Python type inference is challenging in practice. Due to its dynamic properties and extensive dependencies on third-party libraries without type annotations, the performance of traditional static analysis techniques is limited. Although semantics in source code can help manifest intended usage for variables (thus help infer types), they are usually ignored by existing tools. In this paper, we propose PYInfer, an end-to-end learning-based type inference tool that automatically generates type annotations for Python variables. The key insight is that contextual code semantics is critical in inferring the type for a variable. For each use of a variable, we collect a few tokens within its contextual scope, and design a neural network to predict its type. One challenge is that it is difficult to collect a high-quality human-labeled training dataset for this purpose. To address this issue, we apply an existing static analyzer to generate the ground truth for variables in source code. Our main contribution is a novel approach to statically infer variable types effectively and efficiently. Formulating the type inference as a classification problem, we can handle user-defined types and predict type probabilities for each variable. Our model achieves 91.2% accuracy on classifying 11 basic types in Python and 81.2% accuracy on classifying 500 most common types. Our results substantially outperform the state-of-the-art type annotators. Moreover, PYInfer achieves 5.2X more code coverage and is 187X faster than a state-of-the-art learning-based tool. With similar time consumption, our model annotates 5X more variables than a state-of-the-art static analysis tool. Our model also outperforms a learning-based function-level annotator on annotating types for variables and function arguments. All our tools and datasets are publicly available to facilitate future research in this direction.

研究动机与目标

  • 解决 Python 变量类型推断领域缺乏大规模、高质量人工标注数据集的问题。
  • 通过利用上下文代码语义,提升动态类型 Python 代码中类型推断的准确率与效率。
  • 实现对用户自定义类型的高效推断,这些类型在 Python 中很常见,但常被现有工具忽略。
  • 开发一个可扩展的、端到端的基于学习的系统,在覆盖率和速度方面超越静态分析与基于学习的基线方法。
  • 提供公开可用的数据集与工具,以支持未来在 Python 类型推断领域的研究。

提出的方法

  • 从 4,577 个获星数最高的 GitHub 仓库中收集源代码,共包含 5490 万行代码,分布在 320,402 个文件中。
  • 使用 PySonar2 作为静态分析器,自动为变量生成初始类型注解,构成训练数据集。
  • 应用数据清洗技术,提升自动生成注解的质量。
  • 设计一个深度神经网络,通过编码变量使用周围的代码片段(标记)来预测变量类型,实现分类。
  • 将类型推断建模为多分类问题,支持内置类型与用户自定义类型。
  • 使用子词分词(BPE)处理未登录词,提升对罕见或自定义类型名称的泛化能力。

实验结果

研究问题

  • RQ1与传统静态分析相比,上下文代码语义是否能显著提升 Python 变量类型推断的准确率?
  • RQ2在静态分析器自动生成的注解上训练的深度学习模型,在变量类型预测方面是否有效?
  • RQ3所提出的方法能否泛化到训练过程中未见过的用户自定义类型与罕见类型?
  • RQ4在准确率、速度与代码覆盖率方面,PYInfer 与最先进的工具相比表现如何?
  • RQ5大规模、自动整理的数据集在多大程度上能支持高准确率的 Python 类型推断?

主要发现

  • PYInfer 在 11 种基本 Python 类型的分类中达到 91.2% 的准确率,显著优于现有工具。
  • 在更复杂的 500 种常见类型基准测试中,PYInfer 达到 81.2% 的准确率,展现出对多样化类型的强泛化能力。
  • 该模型比最先进的基于学习的工具快 187 倍,使其在大规模代码库中具有极高的效率。
  • PYInfer 的代码覆盖率比最佳现有基于学习的工具高出 5.2 倍,表明其适用范围更广。
  • 在相似运行时间下,PYInfer 标注的变量数量是 PySonar2(领先的静态分析工具)的 5 倍。
  • PYInfer 在预测变量与函数参数类型方面均优于 TypeWriter,凸显其在变量级别推断上的优越性。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。