Skip to content

Commit

Permalink
Add linting and formatting CU-86bw0yqjw (#47)
Browse files Browse the repository at this point in the history
* Linting and formatting setup using pre-commit
* Updated devcontainer with pre-commit
  • Loading branch information
pietrobolcato authored Nov 20, 2023
1 parent cc237b5 commit cfbfc22
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 15 deletions.
5 changes: 4 additions & 1 deletion .devcontainer/scripts/post_create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ bash "$SCRIPT_DIR"/../../scripts/install_system_dependencies.sh

# setup conda
conda env create -n dev -f envs/dev.yaml
conda init
conda init

# setup pre-commit
pre-commit install --install-hooks
24 changes: 17 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
name: ci
on: [push, pull_request]
on: push

jobs:
ci:
name: CI
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.11"
- uses: pre-commit/[email protected]

test:
name: Test
runs-on: ubuntu-latest
env:
PORT: "8000"
Expand All @@ -14,10 +24,10 @@ jobs:
steps:
- name: Check out repository code
uses: actions/checkout@v2
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-central-1
- name: Set up Python 3.11
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-central-1
- name: Publish the Docker image
Expand Down
82 changes: 82 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
repos:
# a set of useful Python-based pre-commit hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
# list of definitions and supported hooks: https://pre-commit.com/hooks.html
- id: trailing-whitespace # removes any whitespace at the ends of lines
- id: check-toml # check toml syntax by loading all toml files
- id: check-yaml # check yaml syntax by loading all yaml files
- id: check-json # check-json syntax by loading all json files
- id: check-merge-conflict # check for files with merge conflict strings
args: ["--assume-in-merge"] # and run this check even when not explicitly in a merge
- id: check-added-large-files # check that no "large" files have been added
args: ["--maxkb=10240"] # where large means 10MB+, as in Hugging Face's git server
- id: debug-statements # check for python debug statements (import pdb, breakpoint, etc.)
- id: detect-private-key # checks for private keys (BEGIN X PRIVATE KEY, etc.)

# black python autoformatting
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black

# flake8 python linter with all the fixins
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
# additional configuration of flake8 and extensions in pyproject.toml
- id: flake8
additional_dependencies:
[
flake8-annotations,
flake8-bandit,
flake8-bugbear,
flake8-black,
flake8-docstrings,
flake8-import-order,
darglint,
mypy,
pycodestyle,
pydocstyle,
Flake8-pyproject
]

# removed unused imports and variables
- repo: https://github.com/PyCQA/autoflake
rev: v2.0.1
hooks:
- id: autoflake
name: autoflake
entry: autoflake
language: python
"types": [python]
require_serial: true
args:
- "--in-place"
- "--expand-star-imports"
- "--remove-duplicate-keys"
- "--remove-unused-variables"

# add trailing commas to calls and literals
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.4.0
hooks:
- id: add-trailing-comma
args:
- --py36-plus

# upgrade syntax for new version of python
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args:
- --py36-plus

# sort python imports in the right order
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"python": "/opt/conda/envs/dev/bin/python",
"env": {
"FLASK_APP": "vectorizing",
"FLASK_DEBUG": "1",
"FLASK_DEBUG": "1"
},
"args": [
"run",
"--port",
"8080",
"--no-debugger",
"--no-debugger"
],
"justMyCode": true
},
}
]
}

11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ The first time the execution can take few minutes, as it is pulling the dev cont

4. If you want to add or remove **system** dependencies, update the script: [`scripts/install_system_dependencies.sh`](scripts/install_system_dependencies.sh). This is used both in CI and at dev container creation, to keep them consistent.

## Linting and formatting

To perform linting and formatting, run from the root of the repo:

```
pre-commit run --all-files
```

The first execution might take a bit longer, as it will set up the virtual environment
where the linter and the formatter will run.

## Server

The server has a single endpoint that receives `POST` requests.
Expand Down
28 changes: 28 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[project]
name = "Vectorizing"
description = "Utility to vectorize raster images"
readme = "README.md"
packages = [{include = "src"}]

[tool.black]
line-length = 88
target-version = ['py311']

[tool.flake8]
max-complexity = 12 # complexity checker threshold
max-line-length = 88
extend-ignore = [
# import order
'I100',
'I101',
'I202',
]
import-order-style = "google"
docstring-convention = "numpy"
strictness = "short"
docstring-style = "numpy"
suppress-none-returning = true
mypy-init-return = true

[tool.isort]
profile = "black"

0 comments on commit cfbfc22

Please sign in to comment.