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%的运行时开销。

0
下载
关闭预览

相关内容

掌握使用Python的大型语言模型
专知会员服务
63+阅读 · 2024年5月22日
专知会员服务
93+阅读 · 2020年12月26日
【书籍推荐】简洁的Python编程(Clean Python),附274页pdf
专知会员服务
183+阅读 · 2020年1月1日
一文看懂怎么用 Python 做数据分析
大数据技术
24+阅读 · 2019年5月5日
一个牛逼的 Python 调试工具
机器学习算法与Python学习
15+阅读 · 2019年4月30日
Python用法速查网站
Python程序员
17+阅读 · 2018年12月16日
【干货】Python无监督学习的4大聚类算法
新智元
14+阅读 · 2018年5月26日
在Python中使用SpaCy进行文本分类
专知
24+阅读 · 2018年5月8日
Tensorflow 文本分类-Python深度学习
Python程序员
12+阅读 · 2017年11月22日
各种相似性度量及Python实现
机器学习算法与Python学习
11+阅读 · 2017年7月6日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
3+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
12+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
7+阅读 · 2015年12月31日
国家自然科学基金
14+阅读 · 2008年12月31日
VIP会员
相关资讯
一文看懂怎么用 Python 做数据分析
大数据技术
24+阅读 · 2019年5月5日
一个牛逼的 Python 调试工具
机器学习算法与Python学习
15+阅读 · 2019年4月30日
Python用法速查网站
Python程序员
17+阅读 · 2018年12月16日
【干货】Python无监督学习的4大聚类算法
新智元
14+阅读 · 2018年5月26日
在Python中使用SpaCy进行文本分类
专知
24+阅读 · 2018年5月8日
Tensorflow 文本分类-Python深度学习
Python程序员
12+阅读 · 2017年11月22日
各种相似性度量及Python实现
机器学习算法与Python学习
11+阅读 · 2017年7月6日
相关基金
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
3+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
12+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
7+阅读 · 2015年12月31日
国家自然科学基金
14+阅读 · 2008年12月31日
Top
微信扫码咨询专知VIP会员