Skip to main content
QUICK REVIEW

[論文レビュー] PYInfer: Deep Learning Semantic Type Inference for Python Variables

Siwei Cui, Gang Zhao|arXiv (Cornell University)|Jun 27, 2021
Software Engineering Research参考文献 36被引用数 11
ひとこと要約

PYInfer は、PySonar2 から自動的に生成された大規模データセットを用いて訓練されたニューラルネットワークを活用し、文脈的なコード意味を活用することで、Python 変数の型を深層学習ベースで推論するフレームワークである。11種類の基本型では91.2%の精度を達成し、500種類の一般的な型では81.2%の精度を示しており、速度(187倍速い)とコードカバレッジ(5.2倍高い)において、最先端のツールを上回っている。

ABSTRACT

Python type inference is challenging in practice. Due to its dynamic properties and extensive dependencies on third-party libraries without type annotations, the performance of traditional static analysis techniques is limited. Although semantics in source code can help manifest intended usage for variables (thus help infer types), they are usually ignored by existing tools. In this paper, we propose PYInfer, an end-to-end learning-based type inference tool that automatically generates type annotations for Python variables. The key insight is that contextual code semantics is critical in inferring the type for a variable. For each use of a variable, we collect a few tokens within its contextual scope, and design a neural network to predict its type. One challenge is that it is difficult to collect a high-quality human-labeled training dataset for this purpose. To address this issue, we apply an existing static analyzer to generate the ground truth for variables in source code. Our main contribution is a novel approach to statically infer variable types effectively and efficiently. Formulating the type inference as a classification problem, we can handle user-defined types and predict type probabilities for each variable. Our model achieves 91.2% accuracy on classifying 11 basic types in Python and 81.2% accuracy on classifying 500 most common types. Our results substantially outperform the state-of-the-art type annotators. Moreover, PYInfer achieves 5.2X more code coverage and is 187X faster than a state-of-the-art learning-based tool. With similar time consumption, our model annotates 5X more variables than a state-of-the-art static analysis tool. Our model also outperforms a learning-based function-level annotator on annotating types for variables and function arguments. All our tools and datasets are publicly available to facilitate future research in this direction.

研究の動機と目的

  • Python 変数の型推論のための、大規模かつ高品質な人手ラベル付きデータセットの不足に取り組む。
  • 文脈的なコード意味を活用することで、動的型付けの Python コードにおける型推論の精度と効率を向上させる。
  • Python で一般的であるが、既存のツールがしばしば無視するユーザー定義型の有効な推論を可能にする。
  • 静的解析および学習ベースのベースラインを上回るカバレッジと速度を実現する、スケーラブルでエンドツーエンドの学習ベースのシステムを開発する。
  • 今後の Python 型推論分野の研究を支援するため、公開可能なデータセットとツールを提供する。

提案手法

  • スター数上位4,577個のGitHubリポジトリからソースコードを収集し、合計320,402ファイルにわたり5490万行のコードをカバーする。
  • PySonar2 を静的解析ツールとして用い、変数の初期型アノテーションを自動生成し、学習データセットを構築する。
  • データクリーニング技術を適用して、自動生成されたアノテーションの品質を向上させる。
  • 変数の使用周辺のトークン(文脈的なコードスニペット)を符号化する深層ニューラルネットワークを設計し、分類によって変数の型を予測する。
  • 型推論を多クラス分類問題として定式化することで、組み込み型とユーザー定義型の両方をサポートする。
  • OoV(未知語)トークンの処理と、珍しいまたはカスタム型名への一般化の向上のため、サブワードトークン化(BPE)を活用する。

実験結果

リサーチクエスチョン

  • RQ1従来の静的解析と比較して、文脈的なコード意味が、Python 変数の型推論の精度を顕著に向上させることができるか?
  • RQ2静的解析ツールから自動生成されたアノテーションで訓練された深層学習モデルは、変数の型予測にどの程度効果的か?
  • RQ3提案手法は、学習時に見られなかったユーザー定義型やレア型にも一般化可能か?
  • RQ4精度、速度、コードカバレッジの観点から、PYInfer は最先端のツールと比較してどの程度の性能を示すか?
  • RQ5大規模で自動的に整備されたデータセットは、Python における高精度な型推論をどの程度サポートできるか?

主な発見

  • PYInfer は11種類の基本的な Python 型を分類する際、91.2%の精度を達成し、既存のツールを顕著に上回っている。
  • 500種類の一般的な型を含むより複雑なベンチマークでは、81.2%の精度を達成しており、多様な型への強い一般化能力を示している。
  • 最先端の学習ベースのツールと比較して、187倍速いため、大規模なコードベースにおいても非常に効率的である。
  • 最高の既存の学習ベースのツールと比較して、5.2倍高いコードカバレッジを達成しており、より広範な適用性を示している。
  • 同程度の実行時間で、PySonar2(代表的な静的解析ツール)よりも5倍多くの変数をアノテートしている。
  • 変数および関数引数の両方の型予測において、TypeWriter を上回っており、変数レベルの推論における優位性が顕著に示されている。

より良い研究を、今すぐ始めましょう

論文の読解から最終レビューまで、研究時間を劇的に削減しましょう。

クレジットカード登録不要

このレビューはAIが作成し、人間の編集者が確認しました。