pre-commit: Add a job that runs ament_pep257 Tools/ros2

[`Tools/ros2/ardupilot_dds_tests/test/test_pep257.py`](https://github.com/ArduPilot/ardupilot/blob/master/Tools/ros2/ardupilot_dds_tests/test/test_pep257.py) ensures [PEP 257 – Docstring Conventions](https://peps.python.org/pep-0257) compliance for all Python files in Tools/ros2.

Partially addresses the concern at
* https://github.com/ArduPilot/ardupilot/pull/30426#issuecomment-2994329025

Here are two solutions (we should pick just one) that replicate this functionality in pre-commit.
1. Use `flake8` plus `flake8-docstring` to run the 'ament-style` rules on Tools/ros2.
2. Run [`ament_pep257`](https://pypi.org/project/ament-lint-pep257) on Tools/ros2.

Quick equivalent commands:
```
uv tool run --with=flake8-docstrings flake8 --select=D \
    --ignore=D100,D101,D102,D103,D104,D105,D106,D107,D203,D212,D404 Tools/ros2

uv tool run --from=ament-lint-pep257 ament_pep257 Tools/ros2  > /dev/null
```
How was this tested?
1. Run both equivalent commands to ensure no errors.
2. `pre-commit run flake8 --all-files` to ensure no errors.
3. `pre-commit run ament-pep257 --all-files` to ensure no errors.

Edit `Tools/ros2/ardupilot_sitl/src/ardupilot_sitl/utilities.py` and add a leading or trailing space inside the docstring, and save the file.

Rerun the four commands to ensure that each raises a `D210: No whitespaces allowed surrounding docstring text` error.
This commit is contained in:
Christian Clauss
2025-06-24 08:21:28 +02:00
committed by Peter Barker
parent b65fe267ac
commit ce6dc895fb
2 changed files with 14 additions and 0 deletions

View File

@@ -9,4 +9,6 @@ jobs:
steps:
- uses: actions/checkout@v5
- uses: actions/setup-python@v5
with:
python-version: 3.x
- uses: pre-commit/action@v3.0.1

View File

@@ -55,6 +55,18 @@ repos:
rev: 7.3.0
hooks:
- id: flake8
- repo: local
hooks:
- id: ament-pep257
name: Run ament_pep257 on Tools/ros2
entry: bash -c 'ament_pep257 Tools/ros2 > /dev/null'
language: python
additional_dependencies:
- ament-lint-pep257
- tomli;python_version<'3.11'
pass_filenames: false
files: ^Tools/ros2/
- repo: https://github.com/codespell-project/codespell
rev: v2.4.1