TypeWriter: neural type prediction with search-based validation

Maintaining large code bases written in dynamically typed languages, such as JavaScript or Python, can be challenging due to the absence of type annotations: simple data compatibility errors proliferate, IDE support is limited, and APIs are hard to comprehend. Recent work attempts to address those issues through either static type inference or probabilistic type prediction. Unfortunately, static type inference for dynamic languages is inherently limited, while probabilistic approaches suffer from imprecision. This paper presents TypeWriter, the first combination of probabilistic type prediction with search-based refinement of predicted types. TypeWriter’s predictor learns to infer the return and argument types for functions from partially annotated code bases by combining the natural language properties of code with programming language-level information. To validate predicted types, TypeWriter invokes a gradual type checker with different combinations of the predicted types, while navigating the space of possible type combinations in a feedback-directed manner. We implement the TypeWriter approach for Python and evaluate it on two code corpora: a multi-million line code base at Facebook and a collection of 1,137 popular open-source projects. We show that TypeWriter’s type predictor achieves an F1 score of 0.64 (0.79) in the top-1 (top-5) predictions for return types, and 0.57 (0.80) for argument types, which clearly outperforms prior type prediction models. By combining predictions with search-based validation, TypeWriter can fully annotate between 14% to 44% of the files in a randomly selected corpus, while ensuring type correctness. A comparison with a static type inference tool shows that TypeWriter adds many more non-trivial types. TypeWriter currently suggests types to developers at Facebook and several thousands of types have already been accepted with minimal changes.

[1]  Jim Baker,et al.  Design and evaluation of gradual typing for python , 2014, DLS.

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

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

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

[5]  Jean-Baptiste Jeannin,et al.  Type inference for static compilation of JavaScript , 2016, OOPSLA.

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

[7]  Yves Le Traon,et al.  Learning to Spot and Refactor Inconsistent Method Names , 2019, 2019 IEEE/ACM 41st International Conference on Software Engineering (ICSE).

[8]  T. Stephen Strickland,et al.  The ruby type checker , 2013, SAC '13.

[9]  Günther Ruhe,et al.  Search Based Software Engineering , 2013, Lecture Notes in Computer Science.

[10]  Premkumar T. Devanbu,et al.  A Survey of Machine Learning for Big Code and Naturalness , 2017, ACM Comput. Surv..

[11]  Charles A. Sutton,et al.  Suggesting accurate method and class names , 2015, ESEC/SIGSOFT FSE.

[12]  Koushik Sen,et al.  Retrieval on source code: a neural code search , 2018, MAPL@PLDI.

[13]  Michael Pradel,et al.  Scaffle: bug localization on millions of files , 2020, ISSTA.

[14]  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).

[15]  Premkumar T. Devanbu,et al.  Are deep neural networks the best choice for modeling source code? , 2017, ESEC/SIGSOFT FSE.

[16]  Xiaodong Gu,et al.  Deep Code Search , 2018, 2018 IEEE/ACM 40th International Conference on Software Engineering (ICSE).

[17]  Gang Zhao,et al.  DeepSim: deep learning code functional similarity , 2018, ESEC/SIGSOFT FSE.

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

[19]  Rafael-Michael Karampatsis,et al.  Maybe Deep Neural Networks are the Best Choice for Modeling Source Code , 2019, ArXiv.

[20]  Sang Peter Chin,et al.  Learning to Repair Software Vulnerabilities with Generative Adversarial Networks , 2018, NeurIPS.

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

[22]  Koushik Sen,et al.  DeepBugs: a learning approach to name-based bug detection , 2018, Proc. ACM Program. Lang..

[23]  Rui Zhao,et al.  Neural Networks for Modeling Source Code Edits , 2018, ArXiv.

[24]  Johannes Bader,et al.  Getafix: learning to fix bugs automatically , 2019, Proc. ACM Program. Lang..

[25]  Rahul Gupta,et al.  DeepFix: Fixing Common C Language Errors by Deep Learning , 2017, AAAI.

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

[27]  Tao Wang,et al.  Convolutional Neural Networks over Tree Structures for Programming Language Processing , 2014, AAAI.

[28]  Avik Chaudhuri,et al.  Dynamic inference of static types for ruby , 2011, POPL '11.

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

[30]  Hailong Sun,et al.  A Novel Neural Source Code Representation Based on Abstract Syntax Tree , 2019, 2019 IEEE/ACM 41st International Conference on Software Engineering (ICSE).

[31]  Gabriele Bavota,et al.  On Learning Meaningful Assert Statements for Unit Test Cases , 2020, 2020 IEEE/ACM 42nd International Conference on Software Engineering (ICSE).

[32]  Ondrej Lhoták,et al.  In defense of soundiness , 2015, Commun. ACM.

[33]  Romain Robbes,et al.  Modeling Vocabulary for Big Code Machine Learning , 2019, ArXiv.

[34]  Peter Thiemann,et al.  Type Analysis for JavaScript , 2009, SAS.

[35]  Michael Pradel,et al.  Anything to Hide? Studying Minified and Obfuscated Code in the Web , 2019, WWW.

[36]  Gabriele Bavota,et al.  On Learning Meaningful Code Changes Via Neural Machine Translation , 2019, 2019 IEEE/ACM 41st International Conference on Software Engineering (ICSE).

[37]  Danny Dig,et al.  Graph-Based Mining of In-the-Wild, Fine-Grained, Semantic Code Change Patterns , 2019, 2019 IEEE/ACM 41st International Conference on Software Engineering (ICSE).

[38]  Premkumar T. Devanbu,et al.  Recovering clear, natural identifiers from obfuscated JS names , 2017, ESEC/SIGSOFT FSE.

[39]  Graham Neubig,et al.  Learning to Represent Edits , 2018, ICLR.

[40]  Sophia Drossopoulou,et al.  Towards Type Inference for JavaScript , 2005, ECOOP.

[41]  Stefan Hanenberg,et al.  An empirical study of the influence of static type systems on the usability of undocumented software , 2012, OOPSLA '12.

[42]  Andrea Janes,et al.  Big Code != Big Vocabulary: Open-Vocabulary Models for Source Code , 2020, 2020 IEEE/ACM 42nd International Conference on Software Engineering (ICSE).

[43]  KrauseAndreas,et al.  Predicting program properties from 'big code' , 2015 .

[44]  Martin White,et al.  Deep learning code fragments for code clone detection , 2016, 2016 31st IEEE/ACM International Conference on Automated Software Engineering (ASE).