Type inference for dynamic programming languages such as Python is an important yet challenging task. Static type inference techniques can precisely infer variables with enough static constraints but are unable to handle variables with dynamic features. Deep learning (DL) based approaches are feature-agnostic, but they cannot guarantee the correctness of the predicted types. Their performance significantly depends on the quality of the training data (i.e., DL models perform poorly on some common types that rarely appear in the training dataset). It is interesting to note that the static and DL-based approaches offer complementary benefits. Unfortunately, to our knowledge, precise type inference based on both static inference and neural predictions has not been exploited and remains an open challenge. In particular, it is hard to integrate DL models into the framework of rule-based static approaches. This paper fills the gap and proposes a hybrid type inference approach named HiTyper based on both static inference and deep learning. Specifically, our key insight is to record type dependencies among variables in each function and encode the dependency information in type dependency graphs (TDGs). Based on TDGs, we can easily integrate type inference rules in the nodes to conduct static inference and type rejection rules to inspect the correctness of neural predictions. HiTyper iteratively conducts static inference and DL-based prediction until the TDG is fully inferred. Experiments on two benchmark datasets show that HiTyper outperforms state-of-the-art DL models by exactly matching 10% more human annotations. HiTyper also achieves an increase of more than 30% on inferring rare types. Considering only the static part of HiTyper, it infers 2x ~ 3x more types than existing static type inference tools.
Bugs in the Shadows: Static Detection of Faulty Python Refactorings
Federal University of Campina Grande(坎皮纳格兰德联邦大学);Federal University of Alagoas(阿拉戈阿斯联邦大学);Pontifical Catholic University of Rio de Janeiro(里约热内卢天主教 Pontifical 大学)
Type4Py: Practical Deep Similarity Learning-Based Type Inference for Python
Delft University of Technology(代尔夫特理工大学);Facebook(脸书)
动态语言,如Python和JavaScript,用静态类型换取开发人员的灵活性和生产力。缺少静态类型会导致运行时异常,并且是IDE支持薄弱的主要因素。为了缓解这些问题,PEP 484为Python引入了可选的类型注释。由于将类型改装到现有代码库是容易出错且费力的,因此已经提出了基于机器学习(ML)的方法来实现基于现有的部分注释的代码库的自动类型推断。然而,以前的基于ML的方法是在人类提供的类型注释上训练和评估的,这可能并不总是合理的,因此这可能限制了现实世界使用的实用性。在本文中,我们提出了Type4Py,一个基于深度相似性学习的分层神经网络模型。它学会在高维空间中区分相似和不相似的类型,从而产生类型集群。参数、变量和返回值的可能类型可以通过最近邻搜索来推断。与以前的工作不同,我们在经过类型检查的数据集上训练和评估了我们的模型,并使用平均倒数排名(MRR)来反映用户感知的性能。所获得的结果表明,Type4Py实现了77.1%的MRR,这比最先进的方法Typilus和TypeWriter分别大幅提高了8.1%和16.7%。最后,为了帮助开发人员改进类型,我们发布了一个Visual Studio Code扩展,它使用Type4Py为Python提供基于ML的类型自动完成。
SemOpt: LLM-Driven Code Optimization via Rule-Based Analysis
Key Laboratory of High Confidence Software Technologies (Peking University), Ministry of Education(北京大学计算机学院;教育部高可信软件技术重点实验室);School of Computer Science, Peking University