Type inference in dynamic languages like Python and JavaScript is a bit like detective work. If you see a variable x being passed into a function process_data(data: DataConfig), you can reasonably guess that x is a DataConfig. But what if that function is defined in a completely different file? Most machine learning models for code struggle with this "non-local" context, often falling back on guessing common types like str or int when they encounter something complex or user-defined.
At ICLR 2023, researchers from UT Austin presented TypeT5, a system that bridges the gap between deep learning and traditional static analysis to solve this problem. TypeT5 treats type inference as a "code infilling" task, using a pre-trained CodeT5 model enhanced by static analysis. By constructing a "usage graph" of the entire codebase and using an iterative decoding scheme, it can propagate type information across files, significantly outperforming previous models on rare and complex user-defined types.
The Problem: Blindspots and Limited Vocabularies
While Python and JavaScript have embraced "gradual typing" (allowing developers to add type hints optionally), most existing codebases remain untyped. Manual annotation is tedious and error-prone. Existing ML-based type inference tools have two major flaws:
- The Local Context Blindspot: They usually only look at the code immediately surrounding a variable. If the type information lives in a different module, the model guesses blindly.
- The Vocabulary Problem: Many models treat type prediction as a classification task over a fixed set of common types. This fails for "parametric types" (like
dict[int, list[User]]) or custom classes defined by the user.
Methods: A Three-Pronged Approach
The authors propose three key innovations to turn a standard language model into a type-inference powerhouse:
1. Type Inference as Code Infilling
Instead of classifying types from a fixed list, TypeT5 uses CodeT5 (an encoder-decoder model) to treat types as missing text. It inserts special "mask" tokens (e.g., <extra_id_0>) where type hints should be and asks the model to generate the missing string.
This approach allows the model to "invent" any type string, including complex nested types it has never seen exactly as-is during training.
2. Static Analysis-Based Context
To give the model a "global" view, the authors use static analysis to build a usage graph. For any given code element (like a function), they pull in:
- The Preamble: Imports and class headers from the current file.
- Users: Snippets of code where this function is called.
- Usees: Signatures of functions or variables that this code calls or uses.
3. Iterative Decoding
This is the "secret sauce." Since the type of A might depend on B, and B might depend on C, TypeT5 doesn't just make one pass. It performs a two-pass iterative decoding:
- Pass 1 (Usee-to-User): It predicts types for low-level dependencies first and "fills them in" the context for higher-level functions.
- Pass 2 (User-to-Usee): It goes back the other way, using information from how a function is called to refine the types inside that function.
Datasets
The researchers evaluated TypeT5 on two main datasets:
- BetterTypes4Py: A high-quality, filtered version of the existing ManyTypes4Py dataset, cleaned to remove stale projects and ensure a high density of human-provided type hints.
- InferTypes4Py: A new, "unseen" test set derived from the authors' own codebase and other recent projects to ensure the model wasn't just memorizing code it saw during pre-training.
Key Insights and Results
TypeT5 sets a new state-of-the-art for Python type inference, demonstrating that context and coherence matter just as much as raw predictive power:
- Overall Accuracy: On the BetterTypes4Py dataset, TypeT5 achieved 73.02% adjusted accuracy, compared to ~35% for Type4Py and ~42% for HiTyper.
- Rare Types: On rare/custom types, TypeT5 reached 58.56% accuracy, while baselines struggled below 26%.
- Coherence over Accuracy: The iterative approach reduced type errors (violations of logic) by 12% compared to a non-incremental version. Seeing how a function is used is often more informative than seeing how it is defined.
- Human-in-the-Loop Efficiency: Because the model is iterative, it supports a "developer tool" mode. If a user corrects one wrong prediction, the model immediately incorporates that "gold" information to improve all subsequent predictions. In simulations, a developer would only need to correct one out of every five predictions to achieve a perfectly annotated codebase.
Limitations and Future Work
While powerful, TypeT5 has a few limitations. Its static analysis currently only looks within the project itself; it doesn't "jump" into the source code of third-party libraries (like NumPy or Django), though it sees the imports. Additionally, iterative decoding is naturally slower than a single-pass model, which might be a concern for real-time IDE integration in massive monorepos. Finally, the model relies on human-provided types for training, which aren't always perfect. Future work could involve training directly on "type-correctness" as verified by a compiler.
Summary updated