From 82d97cef940679d9739ab81ed3eedeec9d73ab1b Mon Sep 17 00:00:00 2001 From: Atsushi Sakai Date: Sun, 9 Jan 2022 00:01:29 +0900 Subject: [PATCH] add mypy unit test (#621) * add mypy unit test * add mypy unit test * add mypy unit test --- .github/workflows/Linux_CI.yml | 15 -------- .github/workflows/MacOS_CI.yml | 15 -------- PathPlanning/DStarLite/d_star_lite.py | 14 ++++---- requirements.txt | 1 + tests/test_mypy_type_check.py | 50 +++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 tests/test_mypy_type_check.py diff --git a/.github/workflows/Linux_CI.yml b/.github/workflows/Linux_CI.yml index d8cad21172d..d0cdd2996c2 100644 --- a/.github/workflows/Linux_CI.yml +++ b/.github/workflows/Linux_CI.yml @@ -28,21 +28,6 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install -r requirements.txt - - name: install coverage - run: pip install coverage - - name: install mypy - run: pip install mypy - - name: mypy check - run: | - mypy -p AerialNavigation - mypy -p ArmNavigation - mypy -p Bipedal - mypy -p Control - mypy -p Localization - mypy -p Mapping - mypy -p PathPlanning - mypy -p PathTracking - mypy -p SLAM - name: do all unit tests run: bash runtests.sh diff --git a/.github/workflows/MacOS_CI.yml b/.github/workflows/MacOS_CI.yml index 19217991578..7fc1854386f 100644 --- a/.github/workflows/MacOS_CI.yml +++ b/.github/workflows/MacOS_CI.yml @@ -35,20 +35,5 @@ jobs: python -m pip install --upgrade pip pip install numpy # cvxpy install workaround pip install -r requirements.txt - - name: install coverage - run: pip install coverage - - name: install mypy - run: pip install mypy - - name: mypy check - run: | - mypy -p AerialNavigation - mypy -p ArmNavigation - mypy -p Bipedal - mypy -p Control - mypy -p Localization - mypy -p Mapping - mypy -p PathPlanning - mypy -p PathTracking - mypy -p SLAM - name: do all unit tests run: bash runtests.sh \ No newline at end of file diff --git a/PathPlanning/DStarLite/d_star_lite.py b/PathPlanning/DStarLite/d_star_lite.py index 603c053dab9..c250e38f91a 100644 --- a/PathPlanning/DStarLite/d_star_lite.py +++ b/PathPlanning/DStarLite/d_star_lite.py @@ -64,15 +64,15 @@ def __init__(self, ox: list, oy: list): for x, y in zip(ox, oy)] self.start = Node(0, 0) self.goal = Node(0, 0) - self.U = list() + self.U = list() # type: ignore self.km = 0.0 self.kold = 0.0 - self.rhs = list() - self.g = list() - self.detected_obstacles = list() + self.rhs = list() # type: ignore + self.g = list() # type: ignore + self.detected_obstacles = list() # type: ignore if show_animation: - self.detected_obstacles_for_plotting_x = list() - self.detected_obstacles_for_plotting_y = list() + self.detected_obstacles_for_plotting_x = list() # type: ignore + self.detected_obstacles_for_plotting_y = list() # type: ignore def create_grid(self, val: float): grid = list() @@ -248,7 +248,7 @@ def compare_paths(self, path1: list, path2: list): return False return True - def display_path(self, path: list, colour: str, alpha: int = 1): + def display_path(self, path: list, colour: str, alpha: float = 1.0): px = [(node.x + self.x_min_world) for node in path] py = [(node.y + self.y_min_world) for node in path] drawing = plt.plot(px, py, colour, alpha=alpha) diff --git a/requirements.txt b/requirements.txt index c0de8f226af..8b6a5ecbfee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ matplotlib == 3.5.1 cvxpy == 1.1.18 pytest == 6.2.5 # For unit test pytest-xdist == 2.5.0 # For unit test +mypy == 0.931 # For unit test flake8 == 4.0.1 # For unit test sphinx == 4.3.2 # For sphinx documentation sphinx_rtd_theme == 1.0.0 diff --git a/tests/test_mypy_type_check.py b/tests/test_mypy_type_check.py new file mode 100644 index 00000000000..07afb40afd2 --- /dev/null +++ b/tests/test_mypy_type_check.py @@ -0,0 +1,50 @@ +import os +import subprocess + +import conftest + +SUBPACKAGE_LIST = [ + "AerialNavigation", + "ArmNavigation", + "Bipedal", + "Control", + "Localization", + "Mapping", + "PathPlanning", + "PathTracking", + "SLAM", +] + + +def run_mypy(dir_name, project_path, config_path): + res = subprocess.run( + ['mypy', + '--config-file', + config_path, + '-p', + dir_name], + cwd=project_path, + stdout=subprocess.PIPE, + encoding='utf-8') + return res.returncode, res.stdout + + +def test(): + project_dir_path = os.path.dirname( + os.path.dirname(os.path.abspath(__file__))) + print(f"{project_dir_path=}") + + config_path = os.path.join(project_dir_path, "mypy.ini") + print(f"{config_path=}") + + for sub_package_name in SUBPACKAGE_LIST: + rc, errors = run_mypy(sub_package_name, project_dir_path, config_path) + if errors: + print(errors) + else: + print("No lint errors found.") + assert rc == 0 + + +if __name__ == '__main__': + conftest.run_this_test(__file__)