@@ -3,50 +3,67 @@ name: lint-python-code
33on :
44 pull_request :
55 branches : [main]
6- paths : [' **.py', ' **.ipynb' ]
6+ paths : [" **.py", " **.ipynb" ]
77 workflow_call :
88
99jobs :
1010 python-linter :
1111 runs-on : ubuntu-latest
1212 steps :
13- - name : Checking out repo
14- 15-
16- - name : Set up Python
17- 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- 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+ 15+
16+ - name : Set up Python
17+ 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+ 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