Python type annotations enable static type checking, but most code remains untyped because manual annotation is time-consuming and tedious. Past approaches to automatic type inference fall short: static methods struggle with dynamic features and infer overly broad types; AI-based methods are unsound and miss rare types; and dynamic methods impose extreme overheads (up to 270x), lack important language support such as inferring variable types, or produce annotations that cause runtime errors. This paper presents RightTyper, a novel hybrid approach for Python that produces accurate and precise type annotations grounded in actual program behavior. RightTyper grounds inference in types observed during actual program execution and combines these observations with static analysis and name resolution to produce substantially higher-quality type annotations than prior approaches. Through principled, statistically guided adaptive sampling, RightTyper balances runtime overhead with the need to observe sufficient execution behavior to infer high-quality type annotations. We evaluate RightTyper against static, dynamic, and AI-based systems on both synthetic benchmarks and real-world code, and find that it consistently achieves higher semantic similarity to ground-truth and developer-written annotations, respectively, while incurring only approximately 27% runtime overhead.
翻译:Python的类型标注支持静态类型检查,但大多数代码仍处于无类型状态,因为手动标注耗时且繁琐。以往的自动类型推断方法存在不足:静态方法难以处理动态特性且推断出的类型过于宽泛;基于人工智能的方法缺乏可靠性且会遗漏罕见类型;动态方法则会产生极高的开销(高达270倍),缺乏对关键语言特性的支持(例如推断变量类型),或产生导致运行时错误的标注。本文提出RightTyper,一种用于Python的新型混合方法,能够基于程序的实际行为生成准确且精确的类型标注。RightTyper将推断建立在程序实际执行过程中观察到的类型之上,并将这些观察结果与静态分析及名称解析相结合,从而生成比先前方法质量显著更高的类型标注。通过基于统计原理的自适应采样,RightTyper在运行时开销与观察足够执行行为以推断高质量类型标注的需求之间取得了平衡。我们在合成基准测试和实际代码上,将RightTyper与静态、动态及基于人工智能的系统进行了评估,发现其分别在语义相似度上更接近真实标注和开发者手写标注,同时仅产生约27%的运行时开销。