Type4Py: Practical Deep Similarity Learning-Based Type Inference for Python

Dynamic languages, such as Python and Javascript, trade static typing for developer flexibility and productivity. Lack of static typing can cause run-time exceptions and is a major factor for weak IDE support. To alleviate these issues, PEP 484 introduced optional type annotations for Python. As retrofitting types to existing codebases is error-prone and laborious, machine learning (ML)-based approaches have been proposed to enable automatic type inference based on existing, partially annotated codebases. However, previousML-based approaches are trained and evaluated on humanprovided type annotations, which might not always be sound, and hence this may limit the practicality for real-world usage. In this paper, we present Type4Py, a deep similarity learning-based hierarchical neural network model. It learns to discriminate between similar and dissimilar types in a high-dimensional space, which results in clusters of types. Likely types for arguments, variables, and return values can then be inferred through the nearest neighbor search. Unlike previous work, we trained and evaluated our model on a type-checked dataset and used mean reciprocal rank (MRR) to reflect the performance perceived by users. The obtained results show that Type4Py achieves an MRR of 77.1%, which is a substantial improvement of 8.1% and 16.7% over the state-of-theart approaches Typilus and TypeWriter, respectively. Finally, to aid developers with retrofitting types, we released a Visual Studio Code extension, which uses Type4Py to provide ML-based type auto-completion for Python.

[1]  Premkumar T. Devanbu,et al.  A large scale study of programming languages and code quality in github , 2014, SIGSOFT FSE.

[2]  Nanning Zheng,et al.  Person Re-identification by Multi-Channel Parts-Based CNN with Improved Triplet Loss Function , 2016, 2016 IEEE Conference on Computer Vision and Pattern Recognition (CVPR).

[3]  Sebastian Kleinschmager,et al.  An empirical study on the impact of static typing on software maintainability , 2013, Empirical Software Engineering.

[4]  Isil Dillig,et al.  LambdaNet: Probabilistic Type Inference using Graph Neural Networks , 2020, ICLR.

[5]  Kuldip K. Paliwal,et al.  Bidirectional recurrent neural networks , 1997, IEEE Trans. Signal Process..

[6]  LiGuo Huang,et al.  Effective API Recommendation without Historical Software Repositories , 2018, 2018 33rd IEEE/ACM International Conference on Automated Software Engineering (ASE).

[7]  Michael Pradel,et al.  NL2Type: Inferring JavaScript Function Types from Natural Language Information , 2019, 2019 IEEE/ACM 41st International Conference on Software Engineering (ICSE).

[8]  Zhi-Hua Zhou,et al.  A brief introduction to weakly supervised learning , 2018 .

[9]  Ronald J. Williams,et al.  A Learning Algorithm for Continually Running Fully Recurrent Neural Networks , 1989, Neural Computation.

[10]  Fagui Liu,et al.  HieNN-DWE: A hierarchical neural network with dynamic word embeddings for document level sentiment classification , 2020, Neurocomputing.

[11]  Miltiadis Allamanis,et al.  The adverse effects of code duplication in machine learning models of code , 2018, Onward!.

[12]  Navdeep Jaitly,et al.  Pointer Networks , 2015, NIPS.

[13]  Zhiyong Feng,et al.  LSTM with sentence representations for document-level sentiment classification , 2018, Neurocomputing.

[14]  Baowen Xu,et al.  Python probabilistic type inference with natural language support , 2016, SIGSOFT FSE.

[15]  André Carlos Ponce de Leon Ferreira de Carvalho,et al.  Effect of label noise in the complexity of classification problems , 2015, Neurocomputing.

[16]  Zheng Gao,et al.  To Type or Not to Type: Quantifying Detectable Bugs in JavaScript , 2017, 2017 IEEE/ACM 39th International Conference on Software Engineering (ICSE).

[17]  Steven Bird,et al.  NLTK: The Natural Language Toolkit , 2002, ACL.

[18]  Natalia Gimelshein,et al.  PyTorch: An Imperative Style, High-Performance Deep Learning Library , 2019, NeurIPS.

[19]  Yong Du,et al.  Hierarchical recurrent neural network for skeleton based action recognition , 2015, 2015 IEEE Conference on Computer Vision and Pattern Recognition (CVPR).

[20]  Martin Hirzel,et al.  Python 3 types in the wild: a tale of two type systems , 2020, DLS.

[21]  Xiangyu Zhang,et al.  PyART: Python API Recommendation in Real-Time , 2021, 2021 IEEE/ACM 43rd International Conference on Software Engineering (ICSE).

[22]  Georgios Gousios,et al.  ManyTypes4Py: A Benchmark Python Dataset for Machine Learning-based Type Inference , 2021, 2021 IEEE/ACM 18th International Conference on Mining Software Repositories (MSR).

[23]  Eran Yahav,et al.  On the Bottleneck of Graph Neural Networks and its Practical Implications , 2021, ICLR.

[24]  Yann LeCun,et al.  Learning a similarity metric discriminatively, with application to face verification , 2005, 2005 IEEE Computer Society Conference on Computer Vision and Pattern Recognition (CVPR'05).

[25]  Shane McIntosh,et al.  An Empirical Study of Type-Related Defects in Python Projects , 2022, IEEE Transactions on Software Engineering.

[26]  Jeffrey Dean,et al.  Distributed Representations of Words and Phrases and their Compositionality , 2013, NIPS.

[27]  Stefan Hanenberg,et al.  Static vs. dynamic type systems: an empirical study about the relationship between type casts and development time , 2011, DLS '11.

[28]  Jan Vitek,et al.  DéjàVu: a map of code duplicates on GitHub , 2017, Proc. ACM Program. Lang..

[29]  Toufique Ahmed,et al.  Learning type annotation: is big data enough? , 2021, ESEC/SIGSOFT FSE.

[30]  Nitish Srivastava,et al.  Dropout: a simple way to prevent neural networks from overfitting , 2014, J. Mach. Learn. Res..

[31]  Georgios Gousios,et al.  TypeWriter: neural type prediction with search-based validation , 2020, ESEC/SIGSOFT FSE.

[32]  Mohammad Ghafari,et al.  Exploiting Type Hints in Method Argument Names to Improve Lightweight Type Inference , 2017, 2017 IEEE/ACM 25th International Conference on Program Comprehension (ICPC).

[33]  Eva Maia A Static Type Inference for Python , 2011 .

[34]  Zheng Gao,et al.  Typilus: neural type hints , 2020, PLDI.

[35]  Andrew D. Gordon,et al.  OptTyper: Probabilistic Type Inference by Optimising Logical and Natural Constraints , 2020, ArXiv.

[36]  Jimmy Ba,et al.  Adam: A Method for Stochastic Optimization , 2014, ICLR.

[37]  Sebastian G. Elbaum,et al.  Assessing the Type Annotation Burden , 2018, 2018 33rd IEEE/ACM International Conference on Automated Software Engineering (ASE).

[38]  Jeffrey S. Foster,et al.  Static type inference for Ruby , 2009, SAC '09.

[39]  Honghui Chen,et al.  Hierarchical Neural Representation for Document Classification , 2018, Cognitive Computation.

[40]  James H. Martin,et al.  Speech and Language Processing, 2nd Edition , 2008 .

[41]  Alessandro Orso,et al.  Are automated debugging techniques actually helping programmers? , 2011, ISSTA '11.

[42]  Premkumar T. Devanbu,et al.  On the naturalness of software , 2016, Commun. ACM.

[43]  Peter E. Hart,et al.  Nearest neighbor pattern classification , 1967, IEEE Trans. Inf. Theory.

[44]  Christopher D. Manning,et al.  Introduction to Information Retrieval , 2010, J. Assoc. Inf. Sci. Technol..

[45]  Andreas Krause,et al.  Predicting Program Properties from "Big Code" , 2015, POPL.

[46]  Christian Bird,et al.  Deep learning type inference , 2018, ESEC/SIGSOFT FSE.

[47]  Peter Müller,et al.  MaxSMT-Based Type Inference for Python 3 , 2018, CAV.