NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

PYInfer: Deep Learning Semantic Type Inference for Python Variables

Siwei Cui, Gang Zhao, Zeyu Dai, Luochao Wang, Ruihong Huang, Jeff HuangarXiv 2021

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.

Introduction

Python’s dynamic nature is a double-edged sword: it offers immense flexibility but makes codebases prone to type-related errors that static languages catch at compile-time. While Python introduced type hints (PEP 484), manually annotating millions of lines of legacy code is prohibitively expensive. Existing automated tools generally fall into two categories: conservative static analyzers that suffer from low coverage, and probabilistic models that often focus only on function signatures while ignoring the rich semantic clues hidden within local variable usage patterns. PYInfer fills this gap by treating variable-level type inference as a classification problem, leveraging the "natural language" semantics of source code.

Methodology

PYInfer utilizes a multi-step pipeline to transform raw source code into precise type predictions.

Bootstrapping Ground Truth

Because high-quality, human-labeled datasets for variable types are scarce at scale, the system uses an existing static analyzer (PySonar2) to label millions of variables across thousands of popular GitHub repositories, creating a robust training foundation.

Contextual Margins and Semantics

The core insight of PYInfer is that a variable's type is often revealed by its immediate context. For every variable, the model analyzes a "margin"—a window of tokens surrounding the variable's usage. This captures how the variable is actually utilized, such as whether it is being passed to a mathematical function or indexed like a list.

Contextual margins used to capture variable usage semantics

Neural Architecture

To handle the "Out-Of-Vocabulary" problem common in source code (where developers invent new names), PYInfer employs Byte Pair Encoding (BPE) to break names into sub-word units. The core model is a Gated Recurrent Unit (GRU) with an Attention mechanism. The GRU processes the sequence of contextual tokens, while the attention layer helps the model focus on the most relevant clues, such as specific method calls, within the margin. The final layer is a Softmax classifier that outputs probabilities for the top 500 most common types.

The PYInfer neural architecture featuring BPE embeddings and GRU with Attention

Datasets

The research introduced typePY, currently the largest dataset of its kind for Python, collected from 4,577 "top-star" GitHub repositories. It contains over 54 million lines of code and 42.5 million valid type annotations. The model was also benchmarked against probPY and validated against typeshed, a human-labeled dataset of high-quality annotations.

Key Results

PYInfer demonstrates that deep learning can be both broader and faster than traditional logic-based analysis:

  • Accuracy: It achieved 91.2% accuracy on the 11 most basic Python types and 81.2% accuracy across the 500 most common types.
  • Precision: By setting a confidence threshold of 0.9, the model reaches 97.6% precision.
  • Efficiency: The model can annotate a variable in roughly 1 millisecond, making it 187x faster than previous state-of-the-art probabilistic tools.
  • Coverage: PYInfer achieved 5.2x more code coverage than the previous state-of-the-art probabilistic tool.

Conclusion

While PYInfer is probabilistic rather than "sound" like formal static analysis, its ability to leverage contextual semantics allows it to significantly outperform existing tools in both speed and coverage. Future work includes extending this approach to other dynamic languages like Ruby or PHP and integrating these models directly into IDEs for real-time developer support.

Summary updated

BibTeX
@misc{cui2021pyinfer,
  author = {Siwei Cui and Gang Zhao and Zeyu Dai and Luochao Wang and Ruihong Huang and Jeff Huang},
  title = {{PYInfer: Deep Learning Semantic Type Inference for Python Variables}},
  year = {2021},
  eprint = {2106.14316},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2106.14316},
}
Figure