Просмотр исходного кода

Add mypy hints for migrate_cpp (#784)

(these are minimal, but adding them for completeness)
Jon Meow 4 лет назад
Родитель
Сommit
8b8715e871
2 измененных файлов с 21 добавлено и 8 удалено
  1. 6 0
      migrate_cpp/BUILD
  2. 15 8
      migrate_cpp/migrate_cpp.py

+ 6 - 0
migrate_cpp/BUILD

@@ -3,6 +3,7 @@
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
 load("@rules_python//python:defs.bzl", "py_binary")
+load("@mypy_integration//:mypy.bzl", "mypy_test")
 
 py_binary(
     name = "migrate_cpp",
@@ -13,3 +14,8 @@ py_binary(
     ],
     python_version = "PY3",
 )
+
+mypy_test(
+    name = "migrate_cpp_mypy_test",
+    deps = [":migrate_cpp"],
+)

+ 15 - 8
migrate_cpp/migrate_cpp.py

@@ -11,6 +11,7 @@ import glob
 import os
 import subprocess
 import sys
+from typing import List, Optional
 
 _CPP_REFACTORING = "./cpp_refactoring/cpp_refactoring"
 _H_EXTS = {".h", ".hpp"}
@@ -18,7 +19,11 @@ _CPP_EXTS = {".c", ".cc", ".cpp", ".cxx"}
 
 
 class _Workflow(object):
-    def __init__(self):
+    _parsed_args: argparse.Namespace
+    _data_dir: str
+    _cpp_files: Optional[List[str]]
+
+    def __init__(self) -> None:
         """Parses command-line arguments and flags."""
         parser = argparse.ArgumentParser(description=__doc__)
         parser.add_argument(
@@ -35,7 +40,7 @@ class _Workflow(object):
         if not os.path.isdir(parsed_args.dir):
             sys.exit("%r must point to a directory." % parsed_args.dir)
 
-    def run(self):
+    def run(self) -> None:
         """Runs the migration workflow."""
         try:
             self._gather_files()
@@ -47,17 +52,17 @@ class _Workflow(object):
             # Discard the stack for subprocess errors.
             sys.exit(e)
 
-    def _data_file(self, relative_path):
+    def _data_file(self, relative_path: str) -> str:
         """Returns the path to a data file."""
         return os.path.join(self._data_dir, relative_path)
 
     @staticmethod
-    def _print_header(header):
+    def _print_header(header: str) -> None:
         print("*" * 79)
         print("* %-75s *" % header)
         print("*" * 79)
 
-    def _gather_files(self):
+    def _gather_files(self) -> None:
         """Returns the list of C++ files to convert."""
         self._print_header("Gathering C++ files...")
         all_files = glob.glob(
@@ -73,7 +78,7 @@ class _Workflow(object):
         self._cpp_files = sorted(cpp_files)
         print("%d files found." % len(self._cpp_files))
 
-    def _clang_tidy(self):
+    def _clang_tidy(self) -> None:
         """Runs clang-tidy to fix C++ files in a directory."""
         self._print_header("Running clang-tidy...")
         with open(self._data_file("clang_tidy.yaml")) as f:
@@ -83,16 +88,18 @@ class _Workflow(object):
             check=True,
         )
 
-    def _cpp_refactoring(self):
+    def _cpp_refactoring(self) -> None:
         """Runs cpp_refactoring to migrate C++ files towards Carbon syntax."""
         self._print_header("Running cpp_refactoring...")
         cpp_refactoring = self._data_file(_CPP_REFACTORING)
+        assert self._cpp_files is not None
         subprocess.run([cpp_refactoring] + self._cpp_files, check=True)
 
-    def _rename_files(self):
+    def _rename_files(self) -> None:
         """Renames C++ files to the destination Carbon filenames."""
         api_renames = 0
         impl_renames = 0
+        assert self._cpp_files is not None
         for f in self._cpp_files:
             parts = os.path.splitext(f)
             if parts[1] in _H_EXTS: