Skip to content

Commit 68167a8

Browse files
committed
Add black, isort, and flake8 linters
1 parent 56f0fc1 commit 68167a8

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
ignore = E203 E501 W503 W504

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ What changes were made?
88

99
## Checklist
1010

11+
- [ ] I've run the `pre_push.py` script to format and lint code.
1112
- [ ] I've checked this pull request runs on `Python 3.6.X`.
1213
- [ ] This fixes something in [Issues](https://github.com/eunwoo1104/discord-py-slash-command/issues).
1314
- Issue:

.github/workflows/ci.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
jobs:
2+
lint-multi-os:
3+
name: Lint ${{ matrix.os }}
4+
runs-on: ${{ matrix.os }}
5+
steps:
6+
- uses: actions/checkout@v2
7+
- uses: actions/setup-python@v1
8+
with:
9+
python-version: 3.x
10+
- uses: actions/cache@v1
11+
with:
12+
key: v0-${{ runner.os }}-pip-lint-${{ hashFiles('setup.py') }}
13+
path: ~/.cache/pip
14+
restore-keys: |
15+
v0-${{ runner.os }}-pip-lint-
16+
v0-${{ runner.os }}-pip-
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install .[lint]
21+
- name: Run black
22+
run: black --check --verbose .
23+
- name: Run flake8
24+
run: flake8 --exclude docs --statistics
25+
- name: Run isort
26+
run: isort -cv .
27+
- name: Run sphinx
28+
run: sphinx-build -W --keep-going docs/ /tmp/foo
29+
strategy:
30+
matrix:
31+
os: [macOS-latest, ubuntu-latest, windows-latest]
32+
name: CI
33+
on: [pull_request, push]

pre_push.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python3
2+
"""Run static analysis on the project."""
3+
4+
import sys
5+
from os import path
6+
from shutil import rmtree
7+
from subprocess import CalledProcessError, check_call
8+
from tempfile import mkdtemp
9+
10+
current_directory = path.abspath(path.join(__file__, ".."))
11+
12+
13+
def do_process(args, shell=False):
14+
"""Run program provided by args.
15+
16+
Return True on success.
17+
18+
Output failed message on non-zero exit and return False.
19+
20+
Exit if command is not found.
21+
22+
"""
23+
print(f"Running: {' '.join(args)}")
24+
try:
25+
check_call(args, shell=shell)
26+
except CalledProcessError:
27+
print(f"\nFailed: {' '.join(args)}")
28+
return False
29+
except Exception as exc:
30+
sys.stderr.write(f"{str(exc)}\n")
31+
sys.exit(1)
32+
return True
33+
34+
35+
def run_static():
36+
"""Runs static tests.
37+
38+
Returns a statuscode of 0 if everything ran correctly. Otherwise, it will return
39+
statuscode 1
40+
41+
"""
42+
success = True
43+
# Formatters
44+
success &= do_process(["black", "."])
45+
success &= do_process(["isort", "."])
46+
# Linters
47+
success &= do_process(["flake8", "--exclude=.eggs,build,docs,.venv*"])
48+
49+
tmp_dir = mkdtemp()
50+
try:
51+
success &= do_process(["sphinx-build", "-W", "--keep-going", "docs", tmp_dir])
52+
finally:
53+
rmtree(tmp_dir)
54+
55+
return success
56+
57+
58+
def main():
59+
success = True
60+
try:
61+
success &= run_static()
62+
except KeyboardInterrupt:
63+
return 1
64+
return int(not success)
65+
66+
67+
if __name__ == "__main__":
68+
exit_code = main()
69+
print("\npre_push.py: Success!" if not exit_code else "\npre_push.py: Fail")
70+
sys.exit(exit_code)

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[tool.black]
2+
exclude = '/(\.eggs|\.git|\.mypy_cache|\.venv.*|_build|build|dist)/'
3+
line-length = 100
4+
5+
[tool.isort]
6+
profile = "black"
7+
line_length = 100

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
VERSION = re.search('__version__ = "([^"]+)"', fp.read()).group(1)
1414

1515
extras = {
16+
"lint": ["black", "flake8", "isort"],
1617
"readthedocs": ["sphinx", "sphinx-rtd-theme"],
1718
}
19+
extras["lint"] += extras["readthedocs"]
20+
extras["dev"] = extras["lint"] + extras["readthedocs"]
1821

1922
setup(
2023
name="discord-py-slash-command",

0 commit comments

Comments
 (0)