Table of Contents
Fetching ...

PyTy: Repairing Static Type Errors in Python

Yiu Wai Chow, Luca Di Grazia, Michael Pradel

TL;DR

PyTy tackles the problem of repairing static type errors in Python arising from gradual typing. It combines an empirical preliminary study, a large-scale dataset PyTyDefects created via delta debugging and gradual type checking, and a cross-language transfer learning approach based on the TFix APR system to repair type errors. On a held-out set, PyTy achieves 85.4% error removal and 54.4% exact developer-match fixes, outperforming state-of-the-art LLMs and showing practical impact with 20 of 30 PRs merged. The work demonstrates a data-driven pathway to accelerate adoption of typing in Python and provides publicly available resources, including the PyTyDefects dataset. This approach highlights the value of targeted, checker-validated repairs for real-world software engineering workflows.

Abstract

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.

PyTy: Repairing Static Type Errors in Python

TL;DR

PyTy tackles the problem of repairing static type errors in Python arising from gradual typing. It combines an empirical preliminary study, a large-scale dataset PyTyDefects created via delta debugging and gradual type checking, and a cross-language transfer learning approach based on the TFix APR system to repair type errors. On a held-out set, PyTy achieves 85.4% error removal and 54.4% exact developer-match fixes, outperforming state-of-the-art LLMs and showing practical impact with 20 of 30 PRs merged. The work demonstrates a data-driven pathway to accelerate adoption of typing in Python and provides publicly available resources, including the PyTyDefects dataset. This approach highlights the value of targeted, checker-validated repairs for real-world software engineering workflows.

Abstract

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.
Paper Structure (55 sections, 11 figures, 2 tables, 1 algorithm)

This paper contains 55 sections, 11 figures, 2 tables, 1 algorithm.

Figures (11)

  • Figure 1: Examples of type errors fixed by PyTy.
  • Figure 2: Type errors (left) and related fix patterns (right), based on 125 type error fixes collected in the preliminary study.
  • Figure 3: Examples of fixing type errors based on error messages.
  • Figure 4: Fix locations and usefulness of error messages.
  • Figure 5: Overview of the approach.
  • ...and 6 more figures