Skip to content

Commit 3f2a944

Browse files
authored
[skip changelog] Add linting and formatting for Python scripts (#832)
* [skip changelog] Added Python linting and formatting Python linting is done using flake8 and formatting with black. A linting workflow will run any time a Python file is modified, a format workflow instead can be triggered by commenting in a PR. Taskfile has been updated with lint and format tasks. * [skip changelog] Fixed all Python linting issues * [skip changelog] Fix Python linting and removed format workflow
1 parent 8603487 commit 3f2a944

19 files changed

+364
-185
lines changed

.flake8

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TODO: move this to pyproject.toml when supported: https://gitlab.com/pycqa/flake8/merge_requests/245
2+
3+
[flake8]
4+
max-line-length = 120
5+
ignore = E741

.github/workflows/python-lint.yaml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: "Lints Python code"
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
paths:
8+
- "**.py"
9+
- ".flake8"
10+
pull_request:
11+
paths:
12+
- "**.py"
13+
- ".flake8"
14+
15+
jobs:
16+
python-linting:
17+
name: "Lints Python code"
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@master
23+
24+
- name: Install Taskfile
25+
uses: Arduino/actions/setup-taskfile@master
26+
with:
27+
repo-token: ${{ secrets.GITHUB_TOKEN }}
28+
29+
- name: Activate Python
30+
uses: actions/setup-python@v1
31+
with:
32+
python-version: '3.8'
33+
architecture: 'x64'
34+
35+
- name: Install Poetry
36+
run: pip install poetry
37+
38+
- name: Lints Python files
39+
run: task python:check

.github/workflows/test.yaml

+10-9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ jobs:
4343
with:
4444
repo-token: ${{ secrets.GITHUB_TOKEN }}
4545

46+
- name: Install Python
47+
uses: actions/setup-python@v1
48+
with:
49+
python-version: "3.8"
50+
architecture: "x64"
51+
52+
- name: Install Poetry
53+
run: pip install poetry
54+
4655
- name: Check the code is good
4756
run: task check
4857

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

68-
- name: Install Python
69-
uses: actions/setup-python@v1
70-
with:
71-
python-version: '3.8'
72-
architecture: 'x64'
73-
7477
- name: Run integration tests
75-
run: |
76-
pip install poetry
77-
task test-integration
78+
run: task test-integration
7879

7980
- name: Send unit tests coverage to Codecov
8081
# Since secrets aren't available on forks, we only

Taskfile.yml

+13
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ tasks:
104104
- go vet {{ default .DEFAULT_TARGETS .TARGETS }}
105105
- "'{{.GOLINTBIN}}' {{.GOLINTFLAGS}} {{ default .DEFAULT_TARGETS .TARGETS }}"
106106
- task: i18n:check
107+
- task: python:check
108+
109+
python:check:
110+
desc: Linting for Python files
111+
cmds:
112+
- poetry install --no-root
113+
- poetry run flake8
114+
115+
python:format:
116+
desc: Automatically formats Python files
117+
cmds:
118+
- poetry install --no-root
119+
- poetry run black .
107120

108121
check-legacy:
109122
desc: Check fmt and lint for the `legacy` package

docs/CONTRIBUTING.md

+14
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ task test-integration
154154

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

157+
When editing any Python file in the project remember to run linting checks with:
158+
159+
```shell
160+
task python:check
161+
```
162+
163+
This will run `flake8` automatically and return any error in the code formatting, if not already installed it will also install integration tests dependencies.
164+
165+
In case of linting errors you should be able to solve most of them by automatically formatting with:
166+
167+
```shell
168+
task python:format
169+
```
170+
157171
## Working on docs
158172

159173
Documentation is provided to final users in form of static HTML content generated

docs/build.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,11 @@ def main(test, dry, remote):
100100
# version is the most recent
101101
docs_version, alias = get_docs_version(repo.active_branch.name, rel_br_names)
102102
if docs_version is None:
103-
print(
104-
f"Can't get version from current branch '{repo.active_branch}', skip docs generation"
105-
)
103+
print(f"Can't get version from current branch '{repo.active_branch}', skip docs generation")
106104
return 0
107105

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

113109
if dry:
114110
print(cmd)

0 commit comments

Comments
 (0)