Python's dynamic typing mechanism, while promoting flexibility, is a significant source of runtime type errors that plague large-scale software, which inspires the automatic type inference techniques. Existing type inference tools have achieved advances in type inference within isolated code snippets. However, repository-level type inference remains a significant challenge, primarily due to the complex inter-procedural dependencies that are difficult to model and resolve. To fill this gap, we present \methodName, a novel approach based on LLMs that achieves repository-level type inference through the co-evolution of types and dependencies. \methodName~constructs an Entity Dependency Graph (EDG) to model the objects and type dependencies across the repository. During the inference process, it iteratively refines types and dependencies in EDG for accurate type inference. Our key innovations are: (1) an EDG model designed to capture repository-level type dependencies; (2) an iterative type inference approach where types and dependencies co-evolve in each iteration; and (3) a type-checker-in-the-loop strategy that validates and corrects inferences on-the-fly, thereby reducing error propagation. When evaluated on 12 complex Python repositories, \methodName~significantly outperformed prior works, achieving a \textit{TypeSim} score of 0.89 and a \textit{TypeExact} score of 0.84, representing a 27\% and 40\% relative improvement over the strongest baseline. More importantly, \methodName~removed new type errors introduced by the tool by 92.7\%. This demonstrates a significant leap towards automated, reliable type annotation for real-world Python development.
翻译:Python的动态类型机制在提升灵活性的同时,也是引发大规模软件运行时类型错误的主要根源,这推动了自动类型推断技术的发展。现有的类型推断工具已在孤立代码片段内的类型推断方面取得了进展。然而,仓库级类型推断仍然是一个重大挑战,这主要源于难以建模和解析的复杂过程间依赖关系。为填补这一空白,我们提出了\methodName,一种基于LLM的新方法,通过类型与依赖的协同演化实现仓库级类型推断。\methodName~构建了一个实体依赖图(EDG)来建模整个仓库中的对象和类型依赖关系。在推断过程中,它迭代地优化EDG中的类型和依赖关系,以实现精确的类型推断。我们的核心创新在于:(1)设计了用于捕获仓库级类型依赖关系的EDG模型;(2)提出了一种迭代式类型推断方法,其中类型与依赖关系在每次迭代中协同演化;(3)采用循环类型检查器策略,实时验证并修正推断结果,从而减少错误传播。在12个复杂Python仓库上的评估表明,\methodName~显著优于现有方法,获得了0.89的\textit{TypeSim}分数和0.84的\textit{TypeExact}分数,分别比最强基线相对提升了27\%和40\%。更重要的是,\methodName~将工具引入的新类型错误减少了92.7\%。这标志着面向实际Python开发的自动化、可靠类型标注实现了重大飞跃。