TypeWriter: neural type prediction with search-based validation
Michael Pradel, Georgios Gousios, Jason Liu, Satish Chandra
摘要
Maintaining large code bases written in dynamically typed languages, such as JavaScript or Python, can be challenging due to the absence of type annotations: simple data compatibility errors proliferate, IDE support is limited, and APIs are hard to comprehend. Recent work attempts to address those issues through either static type inference or probabilistic type prediction. Unfortunately, static type inference for dynamic languages is inherently limited, while probabilistic approaches suffer from imprecision. This paper presents TypeWriter, the first combination of probabilistic type prediction with search-based refinement of predicted types. Type-Writer's predictor learns to infer the return and argument types for functions from partially annotated code bases by combining the natural language properties of code with programming languagelevel information. To validate predicted types, TypeWriter invokes a gradual type checker with different combinations of the predicted types, while navigating the space of possible type combinations in a feedback-directed manner.
We implement the TypeWriter approach for Python and evaluate it on two code corpora: a multi-million line code base at Facebook and a collection of 1,137 popular open-source projects. We show that TypeWriter's type predictor achieves an F1 score of 0.64 (0.79) in the top-1 (top-5) predictions for return types, and 0.57 (0.80) for argument types, which clearly outperforms prior type prediction models. By combining predictions with search-based validation, TypeWriter can fully annotate between 14% to 44% of the files in a randomly selected corpus, while ensuring type correctness. A comparison with a static type inference tool shows that TypeWriter adds many more non-trivial types. TypeWriter currently suggests types to developers at Facebook and several thousands of types have already been accepted with minimal changes.
问 Lune
几分钟读透这篇 Paper,不用再花一下午。
Lune 能读完全文,也能读引用它的顶会 Paper,讲清它的结论、证据和局限,每一点都注明出自哪篇 Paper。不用硬啃,也不靠猜。
已有 45 篇顶会 Paper 引用了它。别等审稿人问起,先弄清它们发现了什么。
脉络
- Generative Type Inference for Python 引用了 InCoder: A Generative Model for Code Infilling and Synthesis
- Generative Type Inference for Python 引用了 Type4Py: Practical Deep Similarity Learning-Based Type Inference for Python
- Generative Type Inference for Python 引用了 Static Inference Meets Deep learning: A Hybrid Type Inference Approach for Python
引用它的顶会 Paper45
- InCoder: A Generative Model for Code Infilling and SynthesisDaniel Fried, Armen Aghajanyan, Jessy Lin, Sida Wang 等ICLR 2023 · 140 次引用
- Type4Py: Practical Deep Similarity Learning-Based Type Inference for PythonAmir M. Mir, Evaldas Latoskinas, Sebastian Proksch, Georgios GousiosICSE 2022 · 59 次引用
- StateFormer: fine-grained type recovery from binaries using generative state modelingKexin Pei, Jonas Guan, Matthew Broughton, Zhongtian Chen 等FSE 2021 · 53 次引用
- Static Inference Meets Deep learning: A Hybrid Type Inference Approach for PythonYun Peng, Cuiyun Gao, Zongjie Li, Bowei Gao 等ICSE 2022 · 48 次引用
- Generative Type Inference for PythonYun Peng, Chaozheng Wang, Wenxuan Wang, Cuiyun Gao 等ASE 2023 · 29 次引用
它引用的 Paper2
相关 Paper
- TypePro: Boosting LLM-Based Type Inference via Inter-Procedural SlicingTeyu Lin, Minghao Fan, Huaxun Huang, Zhirong Shen 等FSE 2026
- TypeT5: Seq2seq Type Inference using Static AnalysisJiayi Wei, Greg Durrett, Isil DilligICLR 2023 · 4 次引用
- TypeCare: Boosting Python Type Inference Models via Context-Aware Re-Ranking and AugmentationWonseok Oh, Hakjoo OhICSE 2026
- Static Type Recommendation for PythonKe Sun, Yifan Zhao, Dan Hao, Lu ZhangASE 2022 · 6 次引用
- The evolution of type annotations in python: an empirical studyLuca Di Grazia, Michael PradelFSE 2022 · 29 次引用
