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 25% runtime overhead.
翻译:Python的类型注解支持静态类型检查,但大多数代码仍处于无类型状态,因为手动标注耗时且繁琐。以往的自动类型推断方法存在不足:静态方法难以处理动态特性且推断出的类型过于宽泛;基于人工智能的方法缺乏可靠性且会遗漏罕见类型;动态方法则带来极高的运行时开销(高达270倍),缺乏对推断变量类型等重要语言特性的支持,或产生的注解会导致运行时错误。本文提出了RightTyper,一种适用于Python的新型混合方法,能够基于程序的实际行为生成准确且精确的类型注解。RightTyper将推断过程建立在程序实际执行过程中观察到的类型之上,并将这些观察结果与静态分析及名称解析相结合,从而生成比先前方法质量显著更高的类型注解。通过采用基于统计原理的自适应采样策略,RightTyper在运行时开销与观察足够执行行为以推断高质量类型注解的需求之间取得了平衡。我们在合成基准测试和实际代码上,将RightTyper与静态、动态以及基于人工智能的系统进行了评估,发现其生成的注解在语义上分别与真实标注和开发者手写标注具有更高的一致性,同时仅产生约25%的运行时开销。