Skip to content

Commit f5408cc

Browse files
committed
feat: add incremental linting
1 parent be62d66 commit f5408cc

File tree

7 files changed

+114
-7
lines changed

7 files changed

+114
-7
lines changed

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
docker network create --driver bridge delphi-net
6666
docker run --rm -d -p 13306:3306 --network delphi-net --name delphi_database_epidata --cap-add=sys_nice delphi_database_epidata
6767
docker run --rm -d -p 6379:6379 --network delphi-net --env "REDIS_PASSWORD=1234" --name delphi_redis delphi_redis
68-
68+
6969
7070
- run: |
7171
wget https://raw.githubusercontent.com/eficode/wait-for/master/wait-for

.github/workflows/lint.yaml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Lint
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- dev
7+
8+
jobs:
9+
darker:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
with:
14+
fetch-depth: 0
15+
- uses: actions/setup-python@v4
16+
- uses: akaihola/[email protected]
17+
with:
18+
options: "--check --diff --isort --color"
19+
version: "~=1.7.2"
20+
21+
lint-diffs:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Commit Range
25+
id: commit-range
26+
uses: akaihola/darker/.github/actions/[email protected]
27+
- uses: actions/checkout@v3
28+
with:
29+
fetch-depth: 0
30+
- uses: actions/setup-python@v4
31+
with:
32+
python-version: '3.8'
33+
cache: 'pip'
34+
- name: Install dependencies
35+
run: |
36+
python -m pip install --upgrade pip
37+
pip install -r requirements.lint.txt
38+
- name: Lint
39+
run: |
40+
inv lint --diff --no-format --revision=${{ steps.commit-range.outputs.commit-range }}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ __pycache__/
1111
/node_modules
1212
.mypy_cache
1313
/missing_db_signals.csv
14+
venv
15+
env

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ $ cd repos
6464
$ pip install -e . --config-settings editable_mode=strict
6565
```
6666

67+
## Linting and Formatting
68+
69+
We use [darker](https://github.com/akaihola/darker) and
70+
[lint-diffs](https://github.com/AtakamaLLC/lint-diffs/) to incrementally bring
71+
this repo up to PEP8 compliance. There is CI to ensure that all new code is
72+
compliant. To run the linter locally:
73+
74+
```sh
75+
# Install lint dependencies
76+
pip install -r requirements.lint.txt
77+
# Run lint
78+
inv lint
79+
```
80+
6781
# COVIDcast
6882

6983
At the present, our primary focus is developing and expanding the

pyproject.toml

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
[tool.black]
2-
line-length = 100
2+
line-length = 120
33
target-version = ['py38']
4-
include = 'server,tests/server'
4+
5+
[tool.isort]
6+
profile = "black"
7+
known_third_party = ["pytest"]
58

69
[tool.pylint]
10+
# Settings copied from
11+
# cmu-delphi/covidcast-indicators/_delphi_utils_python/.pylintrc
712
[tool.pylint.'MESSAGES CONTROL']
8-
max-line-length = 100
13+
max-line-length = 120
914
disable = [
1015
'logging-format-interpolation',
1116
# Allow pytest functions to be part of a class

requirements.lint.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
darker[flynt]~=1.7.2
2+
invoke>=1.4.1
3+
isort==5.12.0
4+
lint_diffs==0.1.22
5+
pylint==2.8.3
6+
requests

tasks.py

+43-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""Repo tasks."""
2+
import pathlib
3+
import sys
4+
5+
import requests
16
from invoke import task
27

38

@@ -12,9 +17,6 @@ def update_gdoc(
1217
sources_url="https://docs.google.com/spreadsheets/d/e/2PACX-1vRfXo-qePhrYGAoZqewVnS1kt9tfnUTLgtkV7a-1q7yg4FoZk0NNGuB1H6k10ah1Xz5B8l1S1RB17N6/pub?gid=0&single=true&output=csv",
1318
signal_url="https://docs.google.com/spreadsheets/d/e/2PACX-1vRfXo-qePhrYGAoZqewVnS1kt9tfnUTLgtkV7a-1q7yg4FoZk0NNGuB1H6k10ah1Xz5B8l1S1RB17N6/pub?gid=329338228&single=true&output=csv",
1419
):
15-
import requests
16-
import pathlib
17-
1820
base_dir = pathlib.Path("./src/server/endpoints/covidcast_utils/")
1921

2022
def _migrate_file(url: str, filename: str):
@@ -26,3 +28,41 @@ def _migrate_file(url: str, filename: str):
2628

2729
_migrate_file(sources_url, "db_sources.csv")
2830
_migrate_file(signal_url, "db_signals.csv")
31+
32+
33+
@task
34+
def lint(c, incremental=True, format=True, revision="origin/dev", diff=False): # pylint: disable=redefined-builtin
35+
"""Lint and format.
36+
37+
Additional linter settings can be found in `pyproject.toml` (this invocation
38+
will use those settings as well).
39+
40+
Parameters
41+
----------
42+
incremental : bool
43+
Only lint changed files.
44+
format : bool
45+
Apply formatting changes.
46+
revision : str
47+
Revision to compare against.
48+
diff : bool
49+
Only show formatting changes, do not apply.
50+
"""
51+
if not incremental:
52+
reponse = input("This will format all files in this repo, continue? [y/N]")
53+
if reponse.lower() not in ("y", "yes"):
54+
return
55+
56+
diff = "--diff" if diff else ""
57+
if incremental:
58+
if format:
59+
c.run(f"darker --revision {revision}... {diff} --flynt --isort --color --check .")
60+
out = c.run(f"git diff -U0 {revision} | lint-diffs")
61+
if out.stdout.strip() != "=== pylint: mine=0, always=0":
62+
print(out.stdout)
63+
sys.exit(1)
64+
else:
65+
if format:
66+
c.run(f"black {diff} .")
67+
c.run(f"isort {diff} .")
68+
c.run("pylint src/ tests/ integrations/")

0 commit comments

Comments
 (0)