一句话:提出 PyTy,首个基于学习的 Python 静态类型错误自动修复技术,利用跨语言迁移学习并在真实错误上达 85.4% 修复率。
渐进类型让 Python 开发者可自选类型标注,但静态检查器报告的大量类型错误仍需手工修复。本文提出 PyTy,面向 Python 静态类型错误的自动程序修复。作者先实证研究开发者修复模式,发现多种策略与重复模式;再自动抽取错误-修复对,构建来自 176 个 GitHub 仓库、含 2,766 对样本的 PyTyDefects。核心是受数据稀缺启发、采用跨语言迁移学习的神经修复模型。评估显示,PyTy 覆盖十类常见错误,在 281 个真实错误上修复 85.4%,优于先进大语言模型 2.1 倍,可与运行时错误修复互补;30 个 PR 中 20 个被合并。
Gradual typing enables developers to annotate types of their own choosing, offering a flexible middle ground between no type annotations and a fully statically typed language. As more and more code bases get type-annotated, static type checkers detect an increasingly large number of type errors. Unfortunately, fixing these errors requires manual effort, hampering the adoption of gradual typing in practice. This paper presents PyTy, an automated program repair approach targeted at statically detectable type errors in Python. The problem of repairing type errors deserves specific attention because it exposes particular repair patterns, offers a warning message with hints about where and how to apply a fix, and because gradual type checking serves as an automatic way to validate fixes. We addresses this problem through three contributions: (i) an empirical study that investigates how developers fix Python type errors, showing a diverse set of fixing strategies with some recurring patterns; (ii) an approach to automatically extract type error fixes, which enables us to create a dataset of 2,766 error-fix pairs from 176 GitHub repositories, named PyTyDefects; (iii) the first learning-based repair technique for fixing type errors in Python. Motivated by the relative data scarcity of the problem, the neural model at the core of PyTy is trained via cross-lingual transfer learning. Our evaluation shows that PyTy offers fixes for ten frequent categories of type errors, successfully addressing 85.4% of 281 real-world errors. This effectiveness outperforms state-of-the-art large language models asked to repair type errors (by 2.1x) and complements a previous technique aimed at type errors that manifest at runtime. Finally, 20 out of 30 pull requests with PyTy-suggested fixes have been merged by developers, showing the usefulness of PyTy in practice.