Skip to content

Commit

Permalink
Configure mypy in workspace (#156)
Browse files Browse the repository at this point in the history
* Add mypy type checker

* Create type checking vscode task and remove mypy extension

* Modify mypy task to lint entire ROS packages rather than a single file

* Specify interpreter path for python extensions (#155)

* Add path to python interpreter for black formatter

* Add python interpreter path to isort and flake8 config

* Format all Python strings to be double quotes (#158)

* Add github copilot extension (#159)

* Delete deprecated python setting

* Enable mypy extension

* Add mypy task TODO

* Install ament_mypy

* Add mypy to CI

* Update TODO

* Fix mypy CI

* Uncomment mypy task

* Update base-dev image tag

* Create mypy vscode tasks for each python pkg

* Add mypy global_launch task

* Fix global_launch mypy error

* Loop over valid sub directories

* Fix ament linter script

* Uses CI script in tasks

* Exit with error if any lint run has errors

---------

Co-authored-by: Patrick Creighton <[email protected]>
  • Loading branch information
DFriend01 and patrick-5546 authored Oct 11, 2023
1 parent c2087b9 commit 3574523
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ghcr.io/ubcsailbot/sailbot_workspace/dev:py-plotly
FROM ghcr.io/ubcsailbot/sailbot_workspace/dev:add-ament-mypy-v2

# Copy configuration files (e.g., .vimrc) from config/ to the container's home directory
ARG USERNAME=ros
Expand Down
3 changes: 2 additions & 1 deletion .devcontainer/base-dev/base-dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,15 @@ RUN apt-get update && apt-get install -y \
wget \
# Install ros distro testing packages
ros-humble-ament-lint \
ros-humble-ament-mypy \
ros-humble-launch-testing \
ros-humble-launch-testing-ament-cmake \
ros-humble-launch-testing-ros \
python3-autopep8 \
&& rm -rf /var/lib/apt/lists/* \
&& rosdep init || echo "rosdep already initialized" \
# Update pydocstyle
&& pip3 install --upgrade pydocstyle
&& pip3 install --upgrade pydocstyle mypy

ARG USERNAME=ros
ARG USER_UID=1000
Expand Down
46 changes: 30 additions & 16 deletions .devcontainer/config/sailbot_workspace.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@
"DCMAKE",
"deque",
"devcontainer",
"dtype",
"gaurav",
"ints",
"isort",
"kmph",
"mkdocs",
"mongocxx",
"mypy",
"noqa",
"OMPL",
"pallete",
Expand Down Expand Up @@ -159,7 +161,6 @@
],
"flake8.interpreter": ["/usr/bin/python3"],
// formatter: black and isort extensions
"python.formatting.provider": "none",
"black-formatter.interpreter": ["/usr/bin/python3"],
"black-formatter.args": [
"--line-length=99",
Expand All @@ -172,6 +173,11 @@
"--ignore=/workspaces/sailbot_workspace/src/raye-local-pathfinding",
],
"python.testing.pytestEnabled": true,
// type checker: mypy
"mypy-type-checker.args": [
"--ignore-missing-imports",
],
"mypy-type-checker.interpreter": ["/usr/bin/python3"],

// c/c++
"[cpp]": {
Expand Down Expand Up @@ -352,15 +358,13 @@
{
"label": "lint_cmake",
"detail": "Run lint on cmake files.",
"type": "ament",
"task": "lint_cmake",
"path": "src/",
"type": "shell",
"command": "LINTER=lint_cmake LOCAL_RUN=true .github/actions/ament-lint/run.sh",
"problemMatcher": [
"$ament_lint_cmake"
],
"presentation": {
"panel": "dedicated",
"reveal": "silent",
"clear": true
}
},
Expand All @@ -372,38 +376,45 @@
"problemMatcher": [],
"presentation": {
"panel": "dedicated",
"reveal": "always",
"clear": true
}
},
{
"label": "flake8",
"detail": "Run flake8 on python files.",
"type": "ament",
"task": "flake8",
"path": "src/",
"commandOptions": "--exclude=src/virtual_iridium,src/raye-local-pathfinding",
"type": "shell",
"command": "LINTER=flake8 LOCAL_RUN=true .github/actions/ament-lint/run.sh",
"problemMatcher": [
"$ament_flake8"
],
"presentation": {
"panel": "dedicated",
"reveal": "silent",
"clear": true
}
},
{
"label": "mypy",
"detail": "Run mypy on python files.",
"type": "shell",
"command": "LINTER=mypy LOCAL_RUN=true .github/actions/ament-lint/run.sh",
"problemMatcher": [
"$ament_mypy",
],
"presentation": {
"panel": "dedicated",
"clear": true
}
},
{
"label": "xmllint",
"detail": "Run xmllint on xml files.",
"type": "ament",
"task": "xmllint",
"path": "src/",
"type": "shell",
"command": "LINTER=xmllint LOCAL_RUN=true .github/actions/ament-lint/run.sh",
"problemMatcher": [
"$ament_xmllint",
],
"presentation": {
"panel": "dedicated",
"reveal": "silent",
"clear": true
}
},
Expand All @@ -413,6 +424,7 @@
"dependsOn": [
"lint_cmake",
"flake8",
"mypy",
"xmllint",
],
"problemMatcher": []
Expand All @@ -438,10 +450,12 @@
{
"id": "package",
"type": "pickString",
"description": "Package to build",
"description": "Package to select",
"options": [
"boat_simulator",
"controller",
"custom_interfaces",
"global_launch",
"local_pathfinding",
"network_systems",
]
Expand Down
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"ms-python.vscode-pylance",
"ms-toolsai.jupyter",
"ms-vsliveshare.vsliveshare",
"ms-python.mypy-type-checker",
"njpwerner.autodocstring",
"stevejpurves.cucumber",
"streetsidesoftware.code-spell-checker",
Expand Down
23 changes: 18 additions & 5 deletions .github/actions/ament-lint/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function get_search_command {
case ${LINTER} in
lint_cmake) CMD='find ${VALID_SRC_DIRS_ARG} -type f \( -name "CMakeLists.txt" -o -name "*.cmake" -o -name "*.cmake.in" \)' ;;
flake8) CMD='find ${VALID_SRC_DIRS_ARG} -type f -name "*.py"' ;;
mypy) CMD='find ${VALID_SRC_DIRS_ARG} -type f -name "*.py"' ;;
xmllint) CMD='find ${VALID_SRC_DIRS_ARG} -type f -name "*.xml"' ;;
*) error "ERROR: Invalid linter ${LINTER} specified in ament-lint action"; exit 1 ;;
esac
Expand All @@ -42,10 +43,22 @@ function lint {
fi
}

source /opt/ros/${ROS_DISTRO}/setup.bash
./setup.sh
cd src
if [[ $LOCAL_RUN != "true" ]]; then
source /opt/ros/${ROS_DISTRO}/setup.bash
./setup.sh
fi

# Exclude repos and files we don't want to lint
VALID_SRC_DIRS=$(ls | grep -v -e virtual_iridium -e docs -e raye-local-pathfinding -e polaris.repos)
lint ${VALID_SRC_DIRS}
VALID_SRC_DIRS=$(ls src | grep -v -e virtual_iridium -e docs -e raye-local-pathfinding -e website -e notebooks -e polaris.repos)
lint_errors=0

# Loop over each directory and lint it
for dir in $VALID_SRC_DIRS; do
echo "Run $LINTER on src/$dir"
lint src/$dir || { warn "WARNING: $LINTER errors in src/$dir, continuing with others"; lint_errors=1; }
done

# Exit with an error if any lint command failed
if [ "$lint_errors" -ne 0 ]; then
exit 1
fi
4 changes: 2 additions & 2 deletions .github/workflows/test_definitions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ jobs:
strategy:
fail-fast: false
matrix:
# ament_lint_common, except for copyright, cppcheck, cpplint, uncrustify, and pep257
linter: [lint_cmake, flake8, xmllint]
# mypy and ament_lint_common, except for copyright, cppcheck, cpplint, uncrustify, and pep257
linter: [lint_cmake, flake8, mypy, xmllint]
name: ament_${{ matrix.linter }}
runs-on: ubuntu-latest
if: ${{ inputs.ros-ci }}
Expand Down
8 changes: 5 additions & 3 deletions src/global_launch/main_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
from typing import List

from launch import LaunchDescription, LaunchDescriptionEntity
from launch import LaunchDescription
from launch.actions import (
DeclareLaunchArgument,
IncludeLaunchDescription,
Expand All @@ -20,7 +20,9 @@
DEVELOPMENT_ROS_PACKAGES = ["boat_simulator", "local_pathfinding", "network_systems"]

# Global launch arguments and constants.
ROS_PACKAGES_DIR = os.path.join(os.getenv("ROS_WORKSPACE"), "src")
ROS_PACKAGES_DIR = os.path.join(
os.getenv("ROS_WORKSPACE", default="/workspaces/sailbot_workspace"), "src"
)
GLOBAL_LAUNCH_ARGUMENTS = [
DeclareLaunchArgument(
name="config",
Expand Down Expand Up @@ -65,7 +67,7 @@ def generate_launch_description() -> LaunchDescription:
return launch_description


def setup_launch(context: LaunchContext) -> List[LaunchDescriptionEntity]:
def setup_launch(context: LaunchContext) -> List[IncludeLaunchDescription]:
"""Collects launch descriptions from all local launch files.
Args:
Expand Down

0 comments on commit 3574523

Please sign in to comment.