RetypeR: Integrated Retrieval-based Automatic Program Repair for Python Type Errors
Sichong Hao, Xianjun Shi, Hongwei Liu · 2024
Python is a widely popular dynamic programming language. While Python's dynamic type system facilitates the development of Python programs, it also introduces type errors at run-time which are often challenging to repair. Large language models (LLMs) have recently demonstrated impressive performance in code understanding and generation, and have been applied to patch synthesis. However, the ability of LLMs to repair type errors is often limited by their finite input context window, which may not fully encompass the repair information necessary for fixing a specific type error, such as valuable context code and fix patterns. In this paper, we introduce RetypeR, an integrated retrieval-based framework to repair Python type errors automatically. RetypeR comprises two stages: external retrieval and internal retrieval. The external retrieval stage retrieves applicable fix patterns from external code repositories containing historical error fixes via an abstract syntax tree (AST)-based retrieval strategy. In the internal retrieval stage, a novel fix ingredient retriever is designed to extract valuable repair ingredients from local context within source files based on both lexical and semantic similarity. To integrate the information retrieved from the aforementioned external and internal stages, RetypeR constructs context-aware repair prompts for LLMs to guide them in generating correct patches for type errors. We evaluate RetypeR on two real-world benchmarks, TypeBugs and BugsInPy. The experimental results demonstrate that RetypeR achieves state-of-the-art performance, improving the best baseline by 12.73% and 11.54% on TypeBugs and BugsInPy benchmarks, respectively.