Skip to content

ci: Add pre-commit hooks and pyright checks #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
name: lint

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
pre-commit:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up python 3.8
uses: actions/setup-python@v4
with:
python-version: 3.8
cache: pip
cache-dependency-path: |
setup.py

- name: Install dependencies
run: python -m pip install -e ".[dev]"

- name: Run pre-commit
uses: pre-commit/action@v3.0.0

pyright:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
fail-fast: false
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: pip
cache-dependency-path: |
setup.py

# all extras are installed to test
- name: Install dependencies
run: python -m pip install -e ".[dev]" -e ".[docs]"

- name: Set up pyright
run: echo "PYRIGHT_VERSION=$(python -c 'import pyright; print(pyright.__pyright_version__)')" >> $GITHUB_ENV

- name: Run pyright (Linux)
uses: jakebailey/pyright-action@v1.3.0
with:
version: ${{ env.PYRIGHT_VERSION }}
python-version: ${{ matrix.python-version }}
python-platform: Linux
# only add comments for 3.8
no-comments: ${{ matrix.python-version != '3.8' }}
warnings: true

- name: Run pyright (Windows)
uses: jakebailey/pyright-action@v1.3.0
# run anyway
if: success() || failure()
with:
version: ${{ env.PYRIGHT_VERSION }}
python-version: ${{ matrix.python-version }}
python-platform: Windows
# only add comments for one platform
no-comments: true
warnings: true

slotscheck:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up python 3.8
uses: actions/setup-python@v4
with:
python-version: 3.8
cache: pip
cache-dependency-path: |
setup.py

- name: Install dependencies
run: python -m pip install -e ".[dev]"

- name: Run slotscheck
run: task slotscheck
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python application
name: Test application

on:
workflow_dispatch:
Expand All @@ -27,9 +27,6 @@ jobs:
python -m pip install --upgrade pip
pip install flake8==3.8.4 pytest tox
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
python setup.py lint
- name: Test with pytest
run: |
tox
35 changes: 35 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Pre-commit setup

ci:
autofix_commit_msg: |
style: auto fixes from pre-commit hooks

repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
name: Running black in all files.

- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort
args: ["--profile", "black", "--extend-skip", "table2ascii"]
name: Running isort in all files.

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
hooks:
- id: check-ast
name: Check if python files are valid syntax for the ast parser
- id: check-case-conflict
name: Check for case conflict on file names for case insensitive systems.
- id: check-merge-conflict
name: Check for merge conflict syntax.
- id: check-toml
name: Check TOML files for valid syntax.
- id: check-yaml
name: Check YAML files for valid syntax.
- id: debug-statements
name: Check for debug statements.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Documentation and examples are available at [table2ascii.rtfd.io](https://table2

## 📥 Installation

``pip install -U table2ascii``
`pip install -U table2ascii`

**Requirements:** `Python 3.7+`

Expand Down Expand Up @@ -178,16 +178,12 @@ All parameters are optional.

## 🧰 Development

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

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

2. Run tests with the command ``tox``
Run tests with the command `tox`

### Linting

Run the following command to lint with flake8

``python setup.py lint``

(Note: The exact command may vary depending on your Python version and environment)
Run `task lint` to lint with black, isort, and pre-commit hooks.
5 changes: 1 addition & 4 deletions docs/source/_templates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from sphinx.locale import _
from sphinx.util.logging import getLogger


__version__ = "1.0.0"
__version_full__ = __version__

Expand All @@ -38,9 +37,7 @@ def config_initiated(app, config):
# See http://www.sphinx-doc.org/en/stable/theming.html#distribute-your-theme-as-a-python-package
def setup(app):
if python_version[0] < 3:
logger.warning(
"Python 2 is deprecated with sphinx_rtd_theme, update to Python 3"
)
logger.warning("Python 2 is deprecated with sphinx_rtd_theme, update to Python 3")
app.require_sphinx("1.6")
if sphinx_version <= (2, 0, 0):
logger.warning(
Expand Down
1 change: 1 addition & 0 deletions docs/source/generate_style_list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

__import__("sys").path.append(os.path.join(os.path.dirname(__file__), "..", ".."))
from table2ascii import PresetStyle, table2ascii

Expand Down
58 changes: 57 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,63 @@ requires = [
]
build-backend = "setuptools.build_meta"


[project]
name = "table2ascii"
authors = [{name = "Jonah Lawrence", email = "jonah@freshidea.com"}]
dynamic = ["version", "description"]
dynamic = ["version", "description"]


[tool.black]
line-length = 100
target-version = ["py38", "py39", "py310"]


[tool.isort]
profile = "black"
py_version = 38
line_length = 100
combine_as_imports = true
filter_files = true


[tool.taskipy.tasks]
black = { cmd = "task lint black", help = "Run black" }
docs = { cmd = "cd docs && sphinx-autobuild source _build/html --ignore _build --watch ../table2ascii --port 8888", help = "Build the documentation on an autoreloading server."}
isort = { cmd = "task lint isort", help = "Run isort" }
lint = { cmd = "pre-commit run --all-files", help = "Check all files for linting errors" }
precommit = { cmd = "pre-commit install --install-hooks", help = "Install the precommit hook" }
pyright = { cmd = "dotenv -f task.env run -- pyright", help = "Run pyright" }
slotscheck = { cmd = "python -m slotscheck --verbose -m table2ascii", help = "Run slotscheck" }


[tool.slotscheck]
strict-imports = true
require-superclass = true
require-subclass = false


[tool.pyright]
typeCheckingMode = "basic"
include = [
"table2ascii",
"*.py",
]
pythonVersion = "3.7"

# https://github.com/microsoft/pyright/blob/main/docs/configuration.md
reportInvalidStringEscapeSequence = false
reportPropertyTypeMismatch = true
reportDuplicateImport = true
reportUntypedFunctionDecorator = true
reportUntypedClassDecorator = true
reportUntypedBaseClass = true
reportUntypedNamedTuple = true
reportUnknownLambdaType = true
reportInvalidTypeVarUse = true
reportUnnecessaryCast = true
reportSelfClsParameterName = true
reportUnsupportedDunderAll = true
reportUnusedVariable = true
reportUnnecessaryComparison = true
reportUnnecessaryTypeIgnoreComment = true
65 changes: 18 additions & 47 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,18 @@
# /usr/bin/env python
import os
import re

from setuptools import setup
from setuptools.command.test import test as Command


class LintCommand(Command):
"""
A copy of flake8's Flake8Command
"""

description = "Run flake8 on modules registered in setuptools"
user_options = []

def initialize_options(self):
# must override
pass

def finalize_options(self):
# must override
pass

def distribution_files(self):
if self.distribution.packages:
for package in self.distribution.packages:
yield package.replace(".", os.path.sep)

if self.distribution.py_modules:
for filename in self.distribution.py_modules:
yield "%s.py" % filename

def run(self):
from flake8.api.legacy import get_style_guide

flake8_style = get_style_guide(config_file="setup.cfg")
paths = self.distribution_files()
report = flake8_style.check_files(paths)
raise SystemExit(report.total_errors > 0)


def version():
version = ""
with open("table2ascii/__init__.py") as f:
version = re.search(
r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read(), re.MULTILINE
).group(1)
version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', f.read(), re.MULTILINE)
if not version:
raise RuntimeError("version is not set")
return version
return version.group(1)


def long_description():
Expand All @@ -68,12 +33,21 @@ def requirements():

extras_require = {
"docs": [
"sphinx>=4.4.0,<5",
"enum-tools>=0.9.0.post1,<1",
"sphinx-toolbox>=2.18.0,<3",
"sphinxcontrib_trio>=1.1.2,<2",
"sphinx-rtd-theme>=1.0.0,<2",
"sphinxext-opengraph>=0.6.2,<1",
"sphinx",
"enum-tools",
"sphinx-toolbox",
"sphinxcontrib_trio",
"sphinx-rtd-theme",
"sphinxext-opengraph",
"sphinx-autobuild",
],
"dev": [
"pre-commit==2.18.1",
"taskipy==1.10.1",
"slotscheck==0.14.0",
"python-dotenv==0.20.0",
"pyright==1.1.244",
"tox==3.24.5",
],
}

Expand Down Expand Up @@ -109,7 +83,4 @@ def requirements():
tests_require=[
"pytest>=6.2,<7",
],
cmdclass={
"lint": LintCommand,
},
)
Loading