Domain Knowledge Matters: Improving Prompts with Fix Templates for Repairing Python Type Errors

As a dynamic programming language, Python has become increasingly popular in recent years. Although the dynamic type system of Python facilitates the developers in writing Python programs, it also brings type errors at runtime which are prevalent yet not easy to fix. There exist rule-based approache...

Full description

Saved in:
Bibliographic Details
Published inProceedings / International Conference on Software Engineering pp. 12 - 24
Main Authors Peng, Yun, Gao, Shuzheng, Gao, Cuiyun, Huo, Yintong, Lyu, Michael R.
Format Conference Proceeding
LanguageEnglish
Published ACM 14.04.2024
Subjects
Online AccessGet full text
ISSN1558-1225
DOI10.1145/3597503.3608132

Cover

More Information
Summary:As a dynamic programming language, Python has become increasingly popular in recent years. Although the dynamic type system of Python facilitates the developers in writing Python programs, it also brings type errors at runtime which are prevalent yet not easy to fix. There exist rule-based approaches for automatically repairing Python type errors. The approaches can generate accurate patches for the type errors covered by manually defined templates, 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 and have become prevalent due to the recent advances in deep learning. Among the learning-based approaches, the prompt-based approach which leverages the knowledge base of code pretrained models via predefined 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 pretrained 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%.
ISSN:1558-1225
DOI:10.1145/3597503.3608132