Skip to content

Commit

Permalink
feat+ci+lint: add incremental linting
Browse files Browse the repository at this point in the history
* add `inv lint` command to tasks.py
* add `lint` workflow to .github/workflows/lint.yaml
* update README.md with linting instructions
  • Loading branch information
dshemetov committed May 15, 2024
1 parent 23bb71d commit 4ab5a17
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
docker network create --driver bridge delphi-net
docker run --rm -d -p 13306:3306 --network delphi-net --name delphi_database_epidata --cap-add=sys_nice delphi_database_epidata
docker run --rm -d -p 6379:6379 --network delphi-net --env "REDIS_PASSWORD=1234" --name delphi_redis delphi_redis
- run: |
wget https://raw.githubusercontent.com/eficode/wait-for/master/wait-for
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Lint

on:
pull_request:
branches:
- dev

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-python@v4
with:
python-version: "3.8"
cache: "pip"
cache-dependency-path: "requirements.lint.txt"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.lint.txt
- name: Commit Range
id: commit-range
uses: akaihola/darker/.github/actions/[email protected]
- name: Run lint
run: |
inv lint --revision=${{ steps.commit-range.outputs.commit-range }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ __pycache__/
rsconnect/

*.Rproj
venv
env
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,43 @@ $ cd repos
$ pip install -e . --config-settings editable_mode=strict
```

## Linting and Formatting

The command `inv lint` will lint your changes (using
[lint-diffs](https://github.com/AtakamaLLC/lint-diffs/)) and `inv format` will
format your changes (using [darker](https://github.com/akaihola/darker)). This
will allow us to incrementally bring this repo up to PEP8 compliance. There is
[a CI action](.github/workflows/lint.yaml) to ensure that all new code is
compliant.

To setup the linter commands locally, run the following commands in the root of
the repository:

```sh
# Create a virtual environment
python -m venv venv
source venv/bin/activate
# Install lint dependencies
pip install -r requirements.lint.txt
# Run lint
inv lint
```

If you get the error `ERROR:darker.git:fatal: Not a valid commit name <hash>`,
then it's likely because your local main branch is not up to date; either you
need to rebase or merge. Note that `darker` reads from `pyproject.toml` for
default settings.

If the lines you change are in a file that uses width 2 tabs (which are plenty
in this repo), it will only change those lines and not the rest, which will
likely break the code; in that case, you should probably just pass the whole
file through black and just do it in the same commit. You can do that with the
following command (using the same virtual environment as above):

```sh
black <file>
```

# COVIDcast

At the present, our primary focus is developing and expanding the
Expand Down
51 changes: 31 additions & 20 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
[tool.black]
line-length = 100
line-length = 120
target-version = ['py38']
include = 'server,tests/server'

[tool.darker]
revision = 'origin/dev...'
color = true
isort = true

[tool.isort]
profile = "black"
known_third_party = ["pytest"]

[tool.pylint]
[tool.pylint.'MESSAGES CONTROL']
max-line-length = 100
disable = [
'logging-format-interpolation',
# Allow pytest functions to be part of a class
'no-self-use',
'too-many-locals',
'too-many-arguments',
# Allow pytest classes to have one test
'too-few-public-methods',
]
[tool.pylint.main]
max-line-length = 120
disable = [
'logging-format-interpolation',
# Allow pytest functions to be part of a class
'no-self-use',
'too-many-locals',
'too-many-arguments',
'too-many-branches',
'too-many-statements',
# Allow pytest classes to have one test
'too-few-public-methods',
]
enable = 'useless-suppression'

[tool.pylint.'BASIC']
# Allow arbitrarily short-named variables.
variable-rgx = ['[a-z_][a-z0-9_]*']
argument-rgx = [ '[a-z_][a-z0-9_]*' ]
attr-rgx = ['[a-z_][a-z0-9_]*']
[tool.pylint.basic]
# Allow arbitrarily short-named variables.
variable-rgx = '[A-Za-z_][a-z0-9_]*'
argument-rgx = '[A-Za-z_][a-z0-9_]*'
attr-rgx = '[A-Za-z_][a-z0-9_]*'

[tool.pylint.'DESIGN']
ignored-argument-names = ['(_.*|run_as_module)']
[tool.pylint.design]
ignored-argument-names = ['(_.*|run_as_module)']
7 changes: 7 additions & 0 deletions requirements.lint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Keep versions in sync with cmu-delphi/covidcast-indicators
darker[flynt]~=1.7.2
invoke>=1.4.1
isort==5.12.0
lint_diffs==0.1.22
pylint==2.8.3
requests
24 changes: 21 additions & 3 deletions tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""Repo tasks."""

import pathlib

import requests
from invoke import task


Expand All @@ -12,9 +17,6 @@ def update_gdoc(
sources_url="https://docs.google.com/spreadsheets/d/e/2PACX-1vRfXo-qePhrYGAoZqewVnS1kt9tfnUTLgtkV7a-1q7yg4FoZk0NNGuB1H6k10ah1Xz5B8l1S1RB17N6/pub?gid=0&single=true&output=csv",
signal_url="https://docs.google.com/spreadsheets/d/e/2PACX-1vRfXo-qePhrYGAoZqewVnS1kt9tfnUTLgtkV7a-1q7yg4FoZk0NNGuB1H6k10ah1Xz5B8l1S1RB17N6/pub?gid=329338228&single=true&output=csv",
):
import requests
import pathlib

base_dir = pathlib.Path("./src/server/endpoints/covidcast_utils/")

def _migrate_file(url: str, filename: str):
Expand All @@ -26,3 +28,19 @@ def _migrate_file(url: str, filename: str):

_migrate_file(sources_url, "db_sources.csv")
_migrate_file(signal_url, "db_signals.csv")


@task
def lint(c, revision="origin/dev"):
"""Lint and format.
Additional linter settings can be found in `pyproject.toml` (`darker` will
use those settings as well).
"""
out = c.run(f"darker --diff --check --revision {revision} .")
if out.stdout:
print(out.stdout)

out = c.run(f"git diff -U0 {revision} | lint-diffs")
if out.stdout.strip() != "=== pylint: mine=0, always=0":
print(out.stdout)

0 comments on commit 4ab5a17

Please sign in to comment.