Although the dynamic type system of Python facilitates the developers in writing Python programs, it also brings type errors at run-time. There exist rule-based approaches for automatically repairing Python type errors. The approaches can generate accurate patches but they require domain experts to design patch synthesis rules and suffer from low template coverage of real-world type errors. Learning-based approaches alleviate the manual efforts in designing patch synthesis rules. Among the learning-based approaches, the prompt-based approach which leverages the knowledge base of code pre-trained models via pre-defined prompts, obtains state-of-the-art performance in general program repair tasks. However, such prompts are manually defined and do not involve any specific clues for repairing Python type errors, resulting in limited effectiveness. How to automatically improve prompts with the domain knowledge for type error repair is challenging yet under-explored. In this paper, we present TypeFix, a novel prompt-based approach with fix templates incorporated for repairing Python type errors. TypeFix first mines generalized fix templates via a novel hierarchical clustering algorithm. The identified fix templates indicate the common edit patterns and contexts of existing type error fixes. TypeFix then generates code prompts for code pre-trained models by employing the generalized fix templates as domain knowledge, in which the masks are adaptively located for each type error instead of being pre-determined. Experiments on two benchmarks, including BugsInPy and TypeBugs, show that TypeFix successfully repairs 26 and 55 type errors, outperforming the best baseline approach by 9 and 14, respectively. Besides, the proposed fix template mining approach can cover 75% of developers' patches in both benchmarks, increasing the best rule-based approach PyTER by more than 30%.
翻译:尽管Python的动态类型系统便于开发者编写Python程序,但它也在运行时带来类型错误。现有基于规则的方法可自动修复Python类型错误,这些方法能生成精确的补丁,但需要领域专家设计补丁合成规则,且存在对真实世界类型错误的模板覆盖率低的问题。基于学习的方法减轻了设计补丁合成规则的人工开销。在基于学习的方法中,利用预定义提示调用代码预训练模型知识库的提示方法,在通用程序修复任务中取得了最先进的性能。然而,此类提示是手动定义的,且不包含任何针对修复Python类型错误的特定线索,导致效果有限。如何利用类型错误修复领域知识自动改进提示仍是一个具有挑战性且未被充分探索的问题。本文提出TypeFix,一种新颖的基于提示且融合修复模板的Python类型错误修复方法。TypeFix首先通过一种新颖的层次聚类算法挖掘通用修复模板。这些识别出的修复模板揭示了现有类型错误修复的常见编辑模式与上下文。随后,TypeFix将通用修复模板作为领域知识,为代码预训练模型生成代码提示,其中掩码位置针对每个类型错误自适应确定,而非预先设定。在BugsInPy和TypeBugs两个基准上的实验表明,TypeFix成功修复了26个和55个类型错误,分别比最佳基线方法多修复9个和14个。此外,所提出的修复模板挖掘方法在两个基准上能覆盖75%的开发者补丁,将基于规则的最优方法PyTER提升了30%以上。