Skip to content

Commit d664529

Browse files
authored
PR #39: Linting Fix for Jupyter Notebook
Fix for linting works for Jupyter Notebook Merge pull request #39 from iamwatchdogs/linting-workflow-fix
2 parents 0608ab0 + 580f605 commit d664529

File tree

1 file changed

+58
-41
lines changed

1 file changed

+58
-41
lines changed

.github/workflows/python-linter.yml

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,67 @@ name: lint-python-code
33
on:
44
pull_request:
55
branches: [main]
6-
paths: ['**.py', '**.ipynb']
6+
paths: ["**.py", "**.ipynb"]
77
workflow_call:
88

99
jobs:
1010
python-linter:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- name: Checking out repo
14-
uses: actions/[email protected]
15-
16-
- name: Set up Python
17-
uses: actions/[email protected]
18-
with:
19-
python-version: '3.12'
20-
21-
- name: Install dependencies
22-
run: |
23-
pip install flake8
24-
pip install pynblint
25-
26-
- name: Lint Python Code
27-
run: |
28-
flake8 . --select=E901,E999,F821,F822,F823 --exclude=__init__.py
29-
env:
30-
FLAKE8_OPTIONS: "--ignore=E203,W503"
31-
32-
- name: Getting PR details
33-
run: |
34-
touch pr.json
35-
gh pr view $PR_NUMBER --json files > pr.json
36-
touch pr.json
37-
env:
38-
PR_NUMBER: ${{ github.event.pull_request.number }}
39-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
40-
41-
- name: Linting all Jupyter Notebook files
42-
uses: jannekem/[email protected]
43-
with:
44-
script: |
45-
import os
46-
import json
47-
with open('pr.json','r') as json_file:
48-
data = json.load(json_file)
49-
for file in data["files"]:
50-
path = file["path"]
51-
if os.path.exists(path):
52-
os.system(f"pynblint {path}")
13+
- name: Checking out repo
14+
uses: actions/[email protected]
15+
16+
- name: Set up Python
17+
uses: actions/[email protected]
18+
with:
19+
python-version: "3.12"
20+
21+
- name: Install dependencies
22+
run: |
23+
pip install flake8 nbqa pylint
24+
25+
- name: Lint Python Code
26+
run: |
27+
flake8 . --select=E901,E999,F821,F822,F823 --exclude=__init__.py
28+
env:
29+
FLAKE8_OPTIONS: "--ignore=E203,W503"
30+
31+
- name: Getting PR details
32+
run: |
33+
touch pr.json
34+
gh pr view $PR_NUMBER --json files > pr.json
35+
touch pr.json
36+
env:
37+
PR_NUMBER: ${{ github.event.pull_request.number }}
38+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
39+
40+
- name: Linting all Jupyter Notebook files
41+
uses: jannekem/[email protected]
42+
with:
43+
script: |
44+
import os
45+
import sys
46+
import json
47+
import subprocess
48+
49+
with open('pr.json', 'r') as pr_details:
50+
files = [
51+
f['path']
52+
for f in json.load(pr_details)['files']
53+
if f['path'].endswith('.ipynb') and os.path.exists(f['path'])
54+
]
55+
56+
exit_codes = []
57+
for path in files:
58+
cmd = ['nbqa', 'pylint', *os.environ['LINTER_CONFIG'].split(),
59+
path, *os.environ['NBQA_CONFIG'].split()]
60+
result = subprocess.run(cmd, capture_output=True, text=True)
61+
print(result.stdout)
62+
if result.stderr:
63+
print(result.stderr, file=sys.stderr)
64+
exit_codes.append(result.returncode)
65+
66+
sys.exit(int(any(exit_codes)))
67+
env:
68+
LINTER_CONFIG: "--disable=C,import-error,no-name-in-module --fail-under=8 --verbose --output-format=colorized"
69+
NBQA_CONFIG: "--nbqa-dont-skip-bad-cells"

0 commit comments

Comments
 (0)