Toward a Corpus Study of the Dynamic Gradual Type

Dibri Nsofor, Ben Greenman · 2025

Read Toward a Corpus Study of the Dynamic Gradual Type by Dibri Nsofor, Ben Greenman (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2503-08928.txt.

Introduction

Dibri Nsofor and Ben Greenman reported early results in 2025 from a corpus study of the dynamic type in Python. Gradual typing lets programmers mark values as dynamic, trading safety for easy adoption. How that escape hatch is used in real code shapes future type system design.

The study targets two hundred twenty-one GitHub projects that use the mypy checker. Eight patterns of dynamic type use emerged from the code. Each pattern carries lessons: some point to gaps that better type features could close, others to habits that tooling could steer toward precise annotations. The authors frame the catalogue as a guide for refining mypy and its support tools.

Catalog record

CREATORDibri Nsofor, Ben Greenman
YEAR2025
COLLECTIONPAPERS
FORMATTXT
FILE SIZE26 KB
VERSION1.0
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2503-08928

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

Toward a Corpus Study of the Dynamic Gradual Type Dibri Nsofor, Ben Greenman Abstract . Gradually-typed languages feature a dynamic type that supports implicit coercions, greatly weakening the type system but making types easier to adopt. Understanding how developers use this dynamic type is a critical question for the design of useful and usable type systems. This paper reports on an in-progress corpus study of the dynamic type in Python, targeting 221 GitHub projects that use the mypy type checker. The study reveals eight patterns-of-use for the dynamic type, which have implications for future refinements of the mypy type system and for tool support to encourage precise type annotations. 1. Introduction Programming languages have traditionally forced developers to choose between the “flexibility and simplicity” of untyped code ( Hoare 1989 ) and the maintenance benefits of static types (e.g. ( Hejlsberg 2023 ) ). Gradual typing offers a compromise through a flexible dynamic type that can be used in any context without a type error ( Siek and Taha 2006 ; Siek et al. 2015 ) . For example, a function that expects an argument of the dynamic type (named Any in Python) can treat the argument as though it has any precise type; the code below assumes its dynamic argument is a class, adds a method to it, and returns the updated class: ⬇ import types ; from typing import Any # the dynamic type in Python def addPrice ( cls : Any ) -> Any : # mixin , add method to class cls . price = types . MethodType ( lambda self : 99, cls ) return cls A caller can send any value to the addPrice function without raising a compile time type error. Incorrect calls, such as addPrice(11) result in a runtime error when executed. The dynamic type adds a degree of optimism to the typechecker, fundamentally changing the nature of type analysis. Without the dynamic type, a well-typed program is certifiably made of parts that fit together (it cannot “go wrong” ( Milner 1978 ) ). With the dynamic type, a well-typed program is code that may fit together provided that every occurrence of the dynamic type receives well-behaved values at runtime—which, as noted in work on static blame ( Su et al. 2024 ) , may be impossible. Moreover, the dynamic type renders type analysis unsound (unless coercions are backed by runtime checks, see section 2 ): any value can inhabit any type after coercion. The dynamic type also pokes loopholes into other traditional properties. For example, the simply-typed lambda calculus is strongly normalizing, but loses this property with the addition of a dynamic type ( Matthews and Findler 2009 ) . Given the perils of the dynamic type, it ought to be used sparingly: either as a temporary measure in prototype software, or as a last resort when precise types do not exist. And yet, its use is widespread. In our collection of 221 projects that use the mypy typechecker ( section 3 ), there are 28,478 occurrences of the dynamic Any type. Understanding why these Any s appear in widely-used code is a critical question for the adoption of types. If more-precise alternatives exist, that motivates tool support for writing types. If the Any types cover up limitations of the typechecker, that motivates research on type system design. If the rationale for certain Any types is unclear, that motivates interviews and developer surveys. This paper is a progress report of our journey to discover the precise reason behind Any types across a large corpus of Python projects. Based on a combination of manual and script-driven analysis on a small number of sample projects, it presents: • the design of a corpus study to study use of the dynamic type ( section 3 ), • eight usage patterns ( section 4 ) and discussions of how to identify the patterns automatically. A major goal of this paper is to solicit early-stage feedback on the design of our corpus study experiment. Automated software analysis is a powerful but dangerous tool, as flaws in the protocol can lead to questionable conclusions ( Ray et al. 2014 ; Berger et al. 2019a ; Berger et al. 2019b ; Ray et al. 2019 ) . In particular, our methods for detecting usage patterns must guard against false positives before we apply them at scale. The paper concludes with future work ( section 5 ), related work ( section 6 ), and a discussion that positions this study in the broader context of gradual languages ( section 7 ). 2. Background: Optional Typing and Mypy Gradually-typed languages come in many varieties because the question of how to enforce static types in the midst of untyped code exposes a complex design space. Full enforcement via behavioral contracts is a compelling vision ( Tobin-Hochstadt and Felleisen 2006 ; Findler and Felleisen 2002 ) , but leads to high run-time overhead ( Greenman et al. 2019 ) without custom runtime support ( Feltey et al. 2018 ; Bauman et al. 2017 ; Kuhlenschmidt et al. 2019 ) . One alternative is to enforce only the top-level shape of types, but this can still lead to overheads of 2x or more ( Vitousek et al. 2017 ; Greenman 2022 ; Greenman and Migeed 2018 ) . A second alternative is to forbid untyped data structures from entering typed code ( Wrigstad et al. 2010 ; Lu et al. 2023a ; Muehlboeck and Tate 2017 ) . A third alternative is to forgo enforcement (and type soundness) altogether. This third option is the most popular choice ( Greenman et al. 2023 ) , and is known as optional typing ( Bracha 2004 ) . Mypy is an optional typing system for Python ( Mypy Team 2024b ) . It equips the PEP 484 syntax for types ( van Rossum et al. 2024 ) with static checks that detect inconsistencies between types and code. Mypy can efficiently analyze millions of lines of code ( Lehtosalo 2019 ) and has been widely adopted. Mypy supports annotations on variable, class, and function declarations. Developers can annotate as many or as few of these positions as they choose. By default, unannotated positions receive the dynamic Any type, though developers can change this default through configuration options that fine-tune mypy or by writing a comment of the form #type:ignore[ERR] with ERR replaced by a specific mypy error code ( Mypy Team 2024a ) . For programs that depend on untyped libraries, developers can write interfaces, called type stubs , that declare types for library exports. The (untyped) definitions of these exports are not typechecked, but their uses (in typed code) are typechecked. In summary, there are several ways that the Any type can enter a mypy codebase: explicitly through developer-provided Any annotations, implicitly through configuration options, and explicitly through lines with #type:ignore comments. All three sources are important for a corpus study. But whereas configuration options and comments come with metadata to describe how they systematically incorporate an Any type, developer-provided annotations require manual analysis. The main focus of this paper is on the manual analysis. 3. Corpus Study Design Our goal is to learn why developers

Read the complete text