From 8b8715e871f6454c67909b1c8de8452733066ba6 Mon Sep 17 00:00:00 2001 From: Jon Meow <46229924+jonmeow@users.noreply.github.com> Date: Thu, 26 Aug 2021 09:28:16 -0700 Subject: [PATCH] Add mypy hints for migrate_cpp (#784) (these are minimal, but adding them for completeness) --- migrate_cpp/BUILD | 6 ++++++ migrate_cpp/migrate_cpp.py | 23 +++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/migrate_cpp/BUILD b/migrate_cpp/BUILD index 4e042d7fc86e..f3104fb26d65 100644 --- a/migrate_cpp/BUILD +++ b/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"], +) diff --git a/migrate_cpp/migrate_cpp.py b/migrate_cpp/migrate_cpp.py index 81d3ce72965b..4ef9519461ce 100644 --- a/migrate_cpp/migrate_cpp.py +++ b/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: