Skip to content

Switch from custom lint setup with Make to pre-commit #2076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
76ebf25
Add the sample start of a pre-commit config
adamtheturtle May 15, 2024
04ed6c5
Add custom-linters to the pre-commit setup
adamtheturtle May 15, 2024
9cd483e
Add pre-commit as a dependency
adamtheturtle May 15, 2024
f4dd0e9
Remove old Makefile command
adamtheturtle May 15, 2024
2f949b8
Move pyroma to pre-commit setup
adamtheturtle May 15, 2024
f96e5eb
Format pyproject.toml
adamtheturtle May 15, 2024
f9ba27d
Only run custom_linters on manual
adamtheturtle May 15, 2024
7c104bf
Start of running slow linters on push not commit
adamtheturtle May 15, 2024
1d12772
Try pre-push for custom linters
adamtheturtle May 15, 2024
c199525
Try new install option
adamtheturtle May 15, 2024
8e04f4d
Make most pre-commit hooks only run on push
adamtheturtle May 15, 2024
e505ccb
Use pre-commit in CI
adamtheturtle May 15, 2024
e12f7a2
Limit pre-commits to file types
adamtheturtle May 15, 2024
993f452
Move some more linters to pre-commit
adamtheturtle May 15, 2024
788ea45
Move hadolint lint step to pre-commit
adamtheturtle May 15, 2024
5152699
Progress towards ruff pre-commit
adamtheturtle May 15, 2024
6b7d294
Move ruff and doc8 to pre-commit
adamtheturtle May 15, 2024
7a506c4
Move pyproject-fmt to pre-commit
adamtheturtle May 15, 2024
a3e1ab5
Add docs to pre-commit
adamtheturtle May 15, 2024
c83ebcc
Fix vulture error
adamtheturtle May 15, 2024
b7f11d9
Use Self type
adamtheturtle May 15, 2024
b788848
Do not fix whitespace on resource files
adamtheturtle May 15, 2024
5bb6238
Commit changes from lint stage
adamtheturtle May 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ jobs:
curl -LsSf https://astral.sh/uv/install.sh | sh
uv pip install --system --upgrade --editable .[dev]

# We choose not to use a Python wrapper or alternative to hadolint as none
# appear to be well maintained, and they require more setup than we would
# want.
- uses: hadolint/hadolint-action@v3.1.0
with:
dockerfile: src/mock_vws/_flask_server/Dockerfile

- name: "Lint"
run: |
make lint
pre-commit run --all-files --hook-stage commit --verbose
pre-commit run --all-files --hook-stage push --verbose
pre-commit run --all-files --hook-stage manual --verbose

- uses: pre-commit-ci/lite-action@v1.0.2
if: always()
163 changes: 163 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
exclude: ^src/mock_vws/resources/

- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: local
hooks:
- id: custom-linters
name: custom-linters
entry: pytest ci/custom_linters.py
stages: [push]
language: system
types: [yaml, python]
pass_filenames: false

- id: actionlint
name: actionlint
entry: actionlint
language: system
pass_filenames: false
types: [yaml]

- id: mypy
name: mypy
stages: [push]
entry: mypy .
language: system
types: [python, toml]
pass_filenames: false

- id: check-manifest
name: check-manifest
stages: [push]
entry: check-manifest .
language: system
pass_filenames: false

- id: pyright
name: pyright
stages: [push]
entry: pyright .
language: system
types: [python, toml]
pass_filenames: false

- id: pyright-verifytypes
name: pyright-verifytypes
stages: [push]
entry: pyright --verifytypes mock_vws
language: system
pass_filenames: false
types: [python]

- id: vulture
name: vulture
entry: vulture --min-confidence 100 --exclude .eggs
language: system
types: [python]

- id: pyroma
name: pyroma
entry: pyroma --min 10 .
language: system
pass_filenames: false
types: [toml]

- id: deptry
name: deptry
entry: deptry src/
language: system
pass_filenames: false

- id: pylint
name: pylint
entry: pylint *.py src/ tests/ docs/ ci/
language: system
stages: [manual]
pass_filenames: false

- id: hadolint-docker
name: Lint Dockerfiles
description: Runs hadolint Docker image to lint Dockerfiles
language: docker_image
types: [dockerfile]
stages: [manual] # Requires Docker to be running
# We choose not to use a Python wrapper or alternative to hadolint as none
# appear to be well maintained, and they require more setup than we would
# want.
entry: ghcr.io/hadolint/hadolint hadolint

- id: ruff-check
name: Ruff check
entry: ruff check
language: system
types: [python]

- id: ruff-format-check
name: Ruff format check
entry: ruff format --check
language: system
types: [python]

- id: ruff-check-fix
name: Ruff check fix
entry: ruff check --fix
language: system
types: [python]

- id: ruff-format-fix
name: Ruff format
entry: ruff format
language: system
types: [python]

- id: doc8
name: doc8
entry: doc8
language: system
types: [rst]

- id: pyproject-fmt-check
name: pyproject-fmt check
entry: pyproject-fmt --check
language: system
types: [toml]
files: pyproject.toml

- id: pyproject-fmt-fix
name: pyproject-fmt
entry: pyproject-fmt
language: system
types: [toml]
files: pyproject.toml

- id: linkcheck
name: linkcheck
entry: make -C docs/ linkcheck SPHINXOPTS=-W
language: system
types: [rst]
stages: [manual]
pass_filenames: false

- id: spelling
name: spelling
entry: make -C docs/ spelling SPHINXOPTS=-W
language: system
types: [rst]
stages: [manual]
pass_filenames: false

- id: docs
name: Build Documentation
entry: make docs
language: system
stages: [manual]
pass_filenames: false
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

25 changes: 0 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
SHELL := /bin/bash -euxo pipefail

include lint.mk

# Treat Sphinx warnings as errors
SPHINXOPTS := -W

Expand All @@ -11,29 +9,6 @@ update-secrets:
tar cvf secrets.tar ci_secrets/
gpg --yes --batch --passphrase=${PASSPHRASE_FOR_VUFORIA_SECRETS} --symmetric --cipher-algo AES256 secrets.tar

.PHONY: lint
lint: \
actionlint \
check-manifest \
deptry \
doc8 \
linkcheck \
mypy \
pyproject-fmt \
pyright \
pyright-verifytypes \
pyroma \
ruff \
spelling \
vulture \
pylint \
custom-linters

.PHONY: fix-lint
fix-lint: \
fix-pyproject-fmt \
fix-ruff

.PHONY: docs
docs:
make -C docs clean html SPHINXOPTS=$(SPHINXOPTS)
Expand Down
17 changes: 10 additions & 7 deletions docs/source/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,23 @@ and on Ubuntu with ``apt``:

apt-get install -y enchant

Linting
-------

Run lint tools:
Install ``pre-commit`` hooks:

.. prompt:: bash

make lint
pre-commit install
pre-commit install --hook-type pre-push

Linting
-------

To fix some lint errors, run the following:
Run lint tools either by committing, or with:

.. prompt:: bash

make fix-lint
pre-commit run --all-files --hook-stage commit --verbose
pre-commit run --all-files --hook-stage push --verbose
pre-commit run --all-files --hook-stage manual --verbose

.. _Homebrew: https://brew.sh

Expand Down
73 changes: 0 additions & 73 deletions lint.mk

This file was deleted.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ dev = [
"freezegun==1.5.1",
"furo==2024.5.6",
"mypy==1.10.0",
"pre-commit==3.7.1",
"pydocstyle==6.3",
"pyenchant==3.2.2",
"pylint==3.2.0",
Expand Down
4 changes: 2 additions & 2 deletions src/mock_vws/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import uuid
from dataclasses import dataclass, field
from typing import TypedDict
from typing import Self, TypedDict

from mock_vws._constants import TargetStatuses
from mock_vws.states import States
Expand Down Expand Up @@ -99,7 +99,7 @@ def get_target(self, target_id: str) -> Target:
return target

@classmethod
def from_dict(cls, database_dict: DatabaseDict) -> VuforiaDatabase:
def from_dict(cls, database_dict: DatabaseDict) -> Self:
"""
Load a database from a dictionary.
"""
Expand Down
6 changes: 3 additions & 3 deletions src/mock_vws/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import statistics
import uuid
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, TypedDict
from typing import TYPE_CHECKING, Self, TypedDict
from zoneinfo import ZoneInfo

from PIL import Image, ImageStat
Expand Down Expand Up @@ -153,7 +153,7 @@ def tracking_rating(self) -> int:
return self._post_processing_target_rating

@classmethod
def from_dict(cls, target_dict: TargetDict) -> Target:
def from_dict(cls, target_dict: TargetDict) -> Self:
"""
Load a target from a dictionary.
"""
Expand Down Expand Up @@ -183,7 +183,7 @@ def from_dict(cls, target_dict: TargetDict) -> Target:
target_tracking_rater = HardcodedTargetTrackingRater(
rating=target_dict["tracking_rating"],
)
return Target(
return cls(
target_id=target_id,
name=name,
active_flag=active_flag,
Expand Down