Skip to content

[skip changelog] Add linting and formatting for Python scripts #832

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 3 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TODO: move this to pyproject.toml when supported: https://gitlab.com/pycqa/flake8/merge_requests/245

[flake8]
max-line-length = 120
ignore = E741
39 changes: 39 additions & 0 deletions .github/workflows/python-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: "Lints Python code"

on:
push:
branches:
- master
paths:
- "**.py"
- ".flake8"
pull_request:
paths:
- "**.py"
- ".flake8"

jobs:
python-linting:
name: "Lints Python code"
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@master

- name: Install Taskfile
uses: Arduino/actions/setup-taskfile@master
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Activate Python
uses: actions/setup-python@v1
with:
python-version: '3.8'
architecture: 'x64'

- name: Install Poetry
run: pip install poetry

- name: Lints Python files
run: task python:check
19 changes: 10 additions & 9 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ jobs:
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Install Python
uses: actions/setup-python@v1
with:
python-version: "3.8"
architecture: "x64"

- name: Install Poetry
run: pip install poetry

- name: Check the code is good
run: task check

Expand All @@ -65,16 +74,8 @@ jobs:
if: matrix.operating-system == 'ubuntu-latest'
run: task test-legacy

- name: Install Python
uses: actions/setup-python@v1
with:
python-version: '3.8'
architecture: 'x64'

- name: Run integration tests
run: |
pip install poetry
task test-integration
run: task test-integration

- name: Send unit tests coverage to Codecov
# Since secrets aren't available on forks, we only
Expand Down
13 changes: 13 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ tasks:
- go vet {{ default .DEFAULT_TARGETS .TARGETS }}
- "'{{.GOLINTBIN}}' {{.GOLINTFLAGS}} {{ default .DEFAULT_TARGETS .TARGETS }}"
- task: i18n:check
- task: python:check

python:check:
desc: Linting for Python files
cmds:
- poetry install --no-root
- poetry run flake8

python:format:
desc: Automatically formats Python files
cmds:
- poetry install --no-root
- poetry run black .

check-legacy:
desc: Check fmt and lint for the `legacy` package
Expand Down
14 changes: 14 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ task test-integration

This will automatically install the necessary dependencies, if not already installed, and run the integration tests automatically.

When editing any Python file in the project remember to run linting checks with:

```shell
task python:check
```

This will run `flake8` automatically and return any error in the code formatting, if not already installed it will also install integration tests dependencies.

In case of linting errors you should be able to solve most of them by automatically formatting with:

```shell
task python:format
```

## Working on docs

Documentation is provided to final users in form of static HTML content generated
Expand Down
8 changes: 2 additions & 6 deletions docs/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,11 @@ def main(test, dry, remote):
# version is the most recent
docs_version, alias = get_docs_version(repo.active_branch.name, rel_br_names)
if docs_version is None:
print(
f"Can't get version from current branch '{repo.active_branch}', skip docs generation"
)
print(f"Can't get version from current branch '{repo.active_branch}', skip docs generation")
return 0

# Taskfile args aren't regular args so we put everything in one string
cmd = (
f"task docs:publish DOCS_REMOTE={remote} DOCS_VERSION={docs_version} DOCS_ALIAS={alias}",
)
cmd = (f"task docs:publish DOCS_REMOTE={remote} DOCS_VERSION={docs_version} DOCS_ALIAS={alias}",)

if dry:
print(cmd)
Expand Down
Loading