Skip to content

Commit 80d4a40

Browse files
ci: Add pre-commit hooks and pyright checks (#36)
* ci: Add pre-commit hooks and pyright checks * style: auto fixes from pre-commit hooks * ci: Remove flake8, fix pyright on push Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 15d3a34 commit 80d4a40

File tree

14 files changed

+236
-85
lines changed

14 files changed

+236
-85
lines changed

.github/workflows/lint.yml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
name: lint
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
pre-commit:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v3
18+
19+
- name: Set up python 3.8
20+
uses: actions/setup-python@v4
21+
with:
22+
python-version: 3.8
23+
cache: pip
24+
cache-dependency-path: |
25+
setup.py
26+
27+
- name: Install dependencies
28+
run: python -m pip install -e ".[dev]"
29+
30+
- name: Run pre-commit
31+
uses: pre-commit/action@v3.0.0
32+
33+
pyright:
34+
runs-on: ubuntu-latest
35+
strategy:
36+
matrix:
37+
python-version: ["3.8", "3.9", "3.10"]
38+
fail-fast: false
39+
steps:
40+
- name: Checkout code
41+
uses: actions/checkout@v3
42+
43+
- name: Set up python ${{ matrix.python-version }}
44+
uses: actions/setup-python@v4
45+
with:
46+
python-version: ${{ matrix.python-version }}
47+
cache: pip
48+
cache-dependency-path: |
49+
setup.py
50+
51+
# all extras are installed to test
52+
- name: Install dependencies
53+
run: python -m pip install -e ".[dev]" -e ".[docs]"
54+
55+
- name: Set up pyright
56+
run: echo "PYRIGHT_VERSION=$(python -c 'import pyright; print(pyright.__pyright_version__)')" >> $GITHUB_ENV
57+
58+
- name: Run pyright (Linux)
59+
uses: jakebailey/pyright-action@v1.3.0
60+
with:
61+
version: ${{ env.PYRIGHT_VERSION }}
62+
python-version: ${{ matrix.python-version }}
63+
python-platform: Linux
64+
# only add comments for 3.8
65+
no-comments: ${{ matrix.python-version != '3.8' }}
66+
warnings: true
67+
68+
- name: Run pyright (Windows)
69+
uses: jakebailey/pyright-action@v1.3.0
70+
# run anyway
71+
if: success() || failure()
72+
with:
73+
version: ${{ env.PYRIGHT_VERSION }}
74+
python-version: ${{ matrix.python-version }}
75+
python-platform: Windows
76+
# only add comments for one platform
77+
no-comments: true
78+
warnings: true
79+
80+
slotscheck:
81+
runs-on: ubuntu-latest
82+
83+
steps:
84+
- name: Checkout code
85+
uses: actions/checkout@v3
86+
87+
- name: Set up python 3.8
88+
uses: actions/setup-python@v4
89+
with:
90+
python-version: 3.8
91+
cache: pip
92+
cache-dependency-path: |
93+
setup.py
94+
95+
- name: Install dependencies
96+
run: python -m pip install -e ".[dev]"
97+
98+
- name: Run slotscheck
99+
run: task slotscheck

.github/workflows/python-app.yml renamed to .github/workflows/python-test.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will install Python dependencies, run tests and lint with a single version of Python
22
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
33

4-
name: Python application
4+
name: Test application
55

66
on:
77
workflow_dispatch:
@@ -27,9 +27,6 @@ jobs:
2727
python -m pip install --upgrade pip
2828
pip install flake8==3.8.4 pytest tox
2929
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
30-
- name: Lint with flake8
31-
run: |
32-
python setup.py lint
3330
- name: Test with pytest
3431
run: |
3532
tox

.pre-commit-config.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Pre-commit setup
2+
3+
ci:
4+
autofix_commit_msg: |
5+
style: auto fixes from pre-commit hooks
6+
7+
repos:
8+
- repo: https://github.com/psf/black
9+
rev: 22.3.0
10+
hooks:
11+
- id: black
12+
name: Running black in all files.
13+
14+
- repo: https://github.com/pycqa/isort
15+
rev: 5.10.1
16+
hooks:
17+
- id: isort
18+
args: ["--profile", "black", "--extend-skip", "table2ascii"]
19+
name: Running isort in all files.
20+
21+
- repo: https://github.com/pre-commit/pre-commit-hooks
22+
rev: v4.2.0
23+
hooks:
24+
- id: check-ast
25+
name: Check if python files are valid syntax for the ast parser
26+
- id: check-case-conflict
27+
name: Check for case conflict on file names for case insensitive systems.
28+
- id: check-merge-conflict
29+
name: Check for merge conflict syntax.
30+
- id: check-toml
31+
name: Check TOML files for valid syntax.
32+
- id: check-yaml
33+
name: Check YAML files for valid syntax.
34+
- id: debug-statements
35+
name: Check for debug statements.

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Documentation and examples are available at [table2ascii.rtfd.io](https://table2
1212

1313
## 📥 Installation
1414

15-
``pip install -U table2ascii``
15+
`pip install -U table2ascii`
1616

1717
**Requirements:** `Python 3.7+`
1818

@@ -178,16 +178,12 @@ All parameters are optional.
178178

179179
## 🧰 Development
180180

181-
### Running tests
181+
Install development dependencies with `pip install -e .[dev]`
182182

183-
1. Install `tox` with the command ``pip install -U tox``
183+
### Running tests
184184

185-
2. Run tests with the command ``tox``
185+
Run tests with the command `tox`
186186

187187
### Linting
188188

189-
Run the following command to lint with flake8
190-
191-
``python setup.py lint``
192-
193-
(Note: The exact command may vary depending on your Python version and environment)
189+
Run `task lint` to lint with black, isort, and pre-commit hooks.

docs/source/_templates/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from sphinx.locale import _
1313
from sphinx.util.logging import getLogger
1414

15-
1615
__version__ = "1.0.0"
1716
__version_full__ = __version__
1817

@@ -38,9 +37,7 @@ def config_initiated(app, config):
3837
# See http://www.sphinx-doc.org/en/stable/theming.html#distribute-your-theme-as-a-python-package
3938
def setup(app):
4039
if python_version[0] < 3:
41-
logger.warning(
42-
"Python 2 is deprecated with sphinx_rtd_theme, update to Python 3"
43-
)
40+
logger.warning("Python 2 is deprecated with sphinx_rtd_theme, update to Python 3")
4441
app.require_sphinx("1.6")
4542
if sphinx_version <= (2, 0, 0):
4643
logger.warning(

docs/source/generate_style_list.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
23
__import__("sys").path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
34
from table2ascii import PresetStyle, table2ascii
45

pyproject.toml

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,63 @@ requires = [
55
]
66
build-backend = "setuptools.build_meta"
77

8+
89
[project]
910
name = "table2ascii"
1011
authors = [{name = "Jonah Lawrence", email = "jonah@freshidea.com"}]
11-
dynamic = ["version", "description"]
12+
dynamic = ["version", "description"]
13+
14+
15+
[tool.black]
16+
line-length = 100
17+
target-version = ["py38", "py39", "py310"]
18+
19+
20+
[tool.isort]
21+
profile = "black"
22+
py_version = 38
23+
line_length = 100
24+
combine_as_imports = true
25+
filter_files = true
26+
27+
28+
[tool.taskipy.tasks]
29+
black = { cmd = "task lint black", help = "Run black" }
30+
docs = { cmd = "cd docs && sphinx-autobuild source _build/html --ignore _build --watch ../table2ascii --port 8888", help = "Build the documentation on an autoreloading server."}
31+
isort = { cmd = "task lint isort", help = "Run isort" }
32+
lint = { cmd = "pre-commit run --all-files", help = "Check all files for linting errors" }
33+
precommit = { cmd = "pre-commit install --install-hooks", help = "Install the precommit hook" }
34+
pyright = { cmd = "dotenv -f task.env run -- pyright", help = "Run pyright" }
35+
slotscheck = { cmd = "python -m slotscheck --verbose -m table2ascii", help = "Run slotscheck" }
36+
37+
38+
[tool.slotscheck]
39+
strict-imports = true
40+
require-superclass = true
41+
require-subclass = false
42+
43+
44+
[tool.pyright]
45+
typeCheckingMode = "basic"
46+
include = [
47+
"table2ascii",
48+
"*.py",
49+
]
50+
pythonVersion = "3.7"
51+
52+
# https://github.com/microsoft/pyright/blob/main/docs/configuration.md
53+
reportInvalidStringEscapeSequence = false
54+
reportPropertyTypeMismatch = true
55+
reportDuplicateImport = true
56+
reportUntypedFunctionDecorator = true
57+
reportUntypedClassDecorator = true
58+
reportUntypedBaseClass = true
59+
reportUntypedNamedTuple = true
60+
reportUnknownLambdaType = true
61+
reportInvalidTypeVarUse = true
62+
reportUnnecessaryCast = true
63+
reportSelfClsParameterName = true
64+
reportUnsupportedDunderAll = true
65+
reportUnusedVariable = true
66+
reportUnnecessaryComparison = true
67+
reportUnnecessaryTypeIgnoreComment = true

setup.py

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,18 @@
11
# /usr/bin/env python
22
import os
33
import re
4+
45
from setuptools import setup
56
from setuptools.command.test import test as Command
67

78

8-
class LintCommand(Command):
9-
"""
10-
A copy of flake8's Flake8Command
11-
"""
12-
13-
description = "Run flake8 on modules registered in setuptools"
14-
user_options = []
15-
16-
def initialize_options(self):
17-
# must override
18-
pass
19-
20-
def finalize_options(self):
21-
# must override
22-
pass
23-
24-
def distribution_files(self):
25-
if self.distribution.packages:
26-
for package in self.distribution.packages:
27-
yield package.replace(".", os.path.sep)
28-
29-
if self.distribution.py_modules:
30-
for filename in self.distribution.py_modules:
31-
yield "%s.py" % filename
32-
33-
def run(self):
34-
from flake8.api.legacy import get_style_guide
35-
36-
flake8_style = get_style_guide(config_file="setup.cfg")
37-
paths = self.distribution_files()
38-
report = flake8_style.check_files(paths)
39-
raise SystemExit(report.total_errors > 0)
40-
41-
429
def version():
4310
version = ""
4411
with open("table2ascii/__init__.py") as f:
45-
version = re.search(
46-
r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read(), re.MULTILINE
47-
).group(1)
12+
version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read(), re.MULTILINE)
4813
if not version:
4914
raise RuntimeError("version is not set")
50-
return version
15+
return version.group(1)
5116

5217

5318
def long_description():
@@ -68,12 +33,21 @@ def requirements():
6833

6934
extras_require = {
7035
"docs": [
71-
"sphinx>=4.4.0,<5",
72-
"enum-tools>=0.9.0.post1,<1",
73-
"sphinx-toolbox>=2.18.0,<3",
74-
"sphinxcontrib_trio>=1.1.2,<2",
75-
"sphinx-rtd-theme>=1.0.0,<2",
76-
"sphinxext-opengraph>=0.6.2,<1",
36+
"sphinx",
37+
"enum-tools",
38+
"sphinx-toolbox",
39+
"sphinxcontrib_trio",
40+
"sphinx-rtd-theme",
41+
"sphinxext-opengraph",
42+
"sphinx-autobuild",
43+
],
44+
"dev": [
45+
"pre-commit==2.18.1",
46+
"taskipy==1.10.1",
47+
"slotscheck==0.14.0",
48+
"python-dotenv==0.20.0",
49+
"pyright==1.1.244",
50+
"tox==3.24.5",
7751
],
7852
}
7953

@@ -109,7 +83,4 @@ def requirements():
10983
tests_require=[
11084
"pytest>=6.2,<7",
11185
],
112-
cmdclass={
113-
"lint": LintCommand,
114-
},
11586
)

0 commit comments

Comments
 (0)