Skip to main content
QUICK REVIEW

[Paper Review] PYInfer: Deep Learning Semantic Type Inference for Python Variables

Siwei Cui, Gang Zhao|arXiv (Cornell University)|Jun 27, 2021
Software Engineering ResearchComputer Science36 references11 citations
TL;DR

PYInfer is a deep learning-based framework that infers Python variable types by leveraging contextual code semantics, using a neural network trained on a large-scale, automatically generated dataset from PySonar2. It achieves 91.2% accuracy on 11 basic types and 81.2% on 500 common types, outperforming state-of-the-art tools in speed (187X faster) and code coverage (5.2X higher).

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.

Motivation & Objective

  • Address the lack of large-scale, high-quality human-labeled datasets for Python variable type inference.
  • Improve the accuracy and efficiency of type inference in dynamically-typed Python code by leveraging contextual code semantics.
  • Enable effective inference of user-defined types, which are common in Python but often overlooked by existing tools.
  • Develop a scalable, end-to-end learning-based system that outperforms both static analysis and learning-based baselines in coverage and speed.
  • Provide a publicly available dataset and tool to support future research in Python type inference.

Proposed method

  • Collect source code from 4,577 top-starred GitHub repositories, totaling 54.9 million lines of code across 320,402 files.
  • Use PySonar2 as a static analyzer to automatically generate initial type annotations for variables, forming the training dataset.
  • Apply data cleaning techniques to improve the quality of the automatically generated annotations.
  • Design a deep neural network that encodes contextual code snippets (tokens around variable usage) to predict variable types via classification.
  • Frame type inference as a multi-class classification problem, enabling support for both built-in and user-defined types.
  • Utilize subword tokenization (BPE) to handle out-of-vocabulary tokens and improve generalization to rare or custom type names.

Experimental results

Research questions

  • RQ1Can contextual code semantics significantly improve the accuracy of Python variable type inference compared to traditional static analysis?
  • RQ2How effective is a deep learning model trained on automatically generated annotations from a static analyzer for variable type prediction?
  • RQ3Can the proposed method generalize to user-defined types and rare types not seen during training?
  • RQ4How does the performance of PYInfer compare to state-of-the-art tools in terms of accuracy, speed, and code coverage?
  • RQ5To what extent can a large-scale, automatically curated dataset support high-accuracy type inference in Python?

Key findings

  • PYInfer achieves 91.2% accuracy in classifying 11 basic Python types, significantly outperforming existing tools.
  • On a more complex benchmark of 500 common types, PYInfer attains 81.2% accuracy, demonstrating strong generalization to diverse types.
  • The model is 187X faster than the state-of-the-art learning-based tool, making it highly efficient for large-scale codebases.
  • PYInfer achieves 5.2X higher code coverage than the best existing learning-based tool, indicating broader applicability.
  • With similar runtime, PYInfer annotates 5X more variables than PySonar2, a leading static analysis tool.
  • PYInfer outperforms TypeWriter in predicting types for both variables and function arguments, highlighting its superiority in variable-level inference.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.