Skip to content

Commit

Permalink
Switch from pre-commit to Hatch scripts
Browse files Browse the repository at this point in the history
- Add Hatch scripts to replace pre-commit commands
  - check:
    - Black
    - isort
    - Flake8
    - Mypy
    - Prettier
    - CSpell
  - format:
    - Black
    - isort
    - Prettier
- Replace other pre-commit hooks where possible
  - Replace `mixed-line-ending` by adding `* text=auto eol=lf` to
    `.gitattributes`
- Update GitHub Actions workflows to run Hatch scripts
- Update docs for Hatch scripts
- Remove pre-commit from `pyproject.toml`
- Remove `.pre-commit-config.yaml`

https://hatch.pypa.io/latest/config/environment/overview/#scripts
  • Loading branch information
br3ndonland committed Nov 12, 2023
1 parent e3d8d06 commit f56d025
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* text=auto
* text=auto eol=lf
*.pdf filter=lfs diff=lfs merge=lfs -text
*.ppt* filter=lfs diff=lfs merge=lfs -text
*.tar* filter=lfs diff=lfs merge=lfs -text
Expand Down
61 changes: 9 additions & 52 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
- [Hatch](#hatch)
- [Testing with pytest](#testing-with-pytest)
- [Code quality](#code-quality)
- [Running code quality checks](#running-code-quality-checks)
- [Code style](#code-style)
- [Static type checking](#static-type-checking)
- [Pre-commit](#pre-commit)
- [Spell check](#spell-check)
- [GitHub Actions workflows](#github-actions-workflows)
- [Maintainers](#maintainers)
Expand Down Expand Up @@ -85,6 +85,13 @@ export HATCH_ENV_TYPE_VIRTUAL_PATH=.venv # install virtualenvs into .venv

## Code quality

### Running code quality checks

Code quality checks can be run using the Hatch scripts in _pyproject.toml_.

- Check: `hatch run check`
- Format: `hatch run format`

### Code style

- **Python code is formatted with [Black](https://black.readthedocs.io/en/stable/)**. Configuration for Black is stored in _pyproject.toml_.
Expand All @@ -107,59 +114,9 @@ export HATCH_ENV_TYPE_VIRTUAL_PATH=.venv # install virtualenvs into .venv
- [Mypy](https://mypy.readthedocs.io/en/stable/) is used for type-checking. [Mypy configuration](https://mypy.readthedocs.io/en/stable/config_file.html) is included in _pyproject.toml_.
- Mypy strict mode is enabled. Strict includes `--no-explicit-reexport` (`implicit_reexport = false`), which means that objects imported into a module will not be re-exported for import into other modules. Imports can be made into explicit exports with the syntax `from module import x as x` (i.e., changing from `import logging` to `import logging as logging`), or by including imports in `__all__`. This explicit import syntax can be confusing. Another option is to apply mypy overrides to any modules that need to leverage implicit exports.

### Pre-commit

[Pre-commit](https://pre-commit.com/) runs [Git hooks](https://www.git-scm.com/book/en/v2/Customizing-Git-Git-Hooks). Configuration is stored in _.pre-commit-config.yaml_. It can run locally before each commit (hence "pre-commit"), or on different Git events like `pre-push`. Pre-commit is installed in the Python virtual environment. To use:

```sh
~
cd path/to/repo

# install hooks that run before each commit
path/to/repo
❯ hatch run pre-commit install

# and/or install hooks that run before each push
path/to/repo
❯ hatch run pre-commit install --hook-type pre-push
```

### Spell check

Spell check is performed with [CSpell](https://cspell.org/).

In GitHub Actions, CSpell runs using [cspell-action](https://github.com/streetsidesoftware/cspell-action).

To run spell check locally, consider installing [their VSCode extension](https://github.com/streetsidesoftware/vscode-spell-checker) or running from the command line.

CSpell can be run with `pnpm` if `pnpm` is installed:

```sh
pnpm -s dlx cspell --dot --gitignore "**/*.md"
```

or with `npx` if `npm` is installed:

```sh
npx -s -y cspell --dot --gitignore "**/*.md"
```

CSpell also offers a pre-commit hook through their [cspell-cli](https://github.com/streetsidesoftware/cspell-cli) repo. A `.pre-commit-config.yaml` configuration could look like this:

```yml
repos:
- repo: https://github.com/streetsidesoftware/cspell-cli
rev: v6.16.0
hooks:
- id: cspell
files: "^.*.md$"
args: ["--dot", "--gitignore", "**/*.md"]
```
CSpell is not currently used with pre-commit in this project because behavior of the pre-commit hook is inconsistent.
- [CSpell matches files with globs](https://cspell.org/docs/globs/), but [pre-commit uses Python regexes](https://pre-commit.com/#regular-expressions). Two separate file patterns have to be specified (a regex for pre-commit and a glob for CSpell), which is awkward and error-prone.
- When running with pre-commit, CSpell seems to output each error multiple times.
Spell check is performed with [CSpell](https://cspell.org/). The CSpell command is included in the Hatch script for code quality checks (`hatch run check`).

## GitHub Actions workflows

Expand Down
11 changes: 2 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ jobs:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('pyproject.toml') }}
restore-keys: ${{ runner.os }}-pip-
- name: Set up pre-commit cache
if: runner.os == 'Linux'
uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
key: ${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
restore-keys: ${{ runner.os }}-pre-commit-
- name: Install pipx for Python ${{ matrix.python-version }}
run: python -m pip install "pipx==$PIPX_VERSION"
- name: Install Hatch
Expand Down Expand Up @@ -80,8 +73,8 @@ jobs:
files: "**/*.md"
incremental_files_only: false
strict: true
- name: Run pre-commit hooks
run: hatch run pre-commit run --all-files
- name: Run Hatch script for code quality checks
run: hatch run ${{ env.HATCH_ENV }}:check
- name: Run tests
run: hatch run coverage run
- name: Enforce test coverage
Expand Down
41 changes: 0 additions & 41 deletions .pre-commit-config.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
*cache*
*venv*
htmlcov
public
site
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

[![Imports: isort](https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat&labelColor=ef8336)](https://pycqa.github.io/isort/)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://black.readthedocs.io/en/stable/)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
[![ci](https://github.com/br3ndonland/template-python/workflows/ci/badge.svg)](https://github.com/br3ndonland/template-python/actions/workflows/ci.yml)

Brendon Smith ([br3ndonland](https://github.com/br3ndonland))
Expand All @@ -27,10 +26,6 @@ Another common approach, especially for Python, is to use [cookiecutter](https:/
# In the command below, use your repo name instead of 'repo-name'
❯ git grep -l 'template-python' | xargs sed -i '' 's|template-python|repo-name|g'
❯ git grep -l 'template_python' | xargs sed -i '' 's|template_python|repo-name|g'
# Install project
❯ hatch env create
# Install pre-commit hooks
❯ hatch run pre-commit install
# Try running the tests
❯ hatch run coverage run
```
Expand Down
16 changes: 15 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ checks = [
"flake8>=6,<7",
"isort>=5,<6",
"mypy==1.7.0",
"pre-commit>=3,<4",
]
docs = [
"mkdocs-material>=9,<10",
Expand Down Expand Up @@ -66,6 +65,21 @@ features = [
]
path = ".venv"

[tool.hatch.envs.default.scripts]
check = [
"black --check --diff .",
"isort --check --diff .",
"flake8 --extend-exclude=.venv,bin,build,cache,dist,lib --max-line-length=88",
"mypy",
"npx -s -y prettier@'^2' . --check",
"npx -s -y cspell --dot --gitignore *.md **/*.md",
]
format = [
"black .",
"isort .",
"npx -s -y prettier@'^2' . --write",
]

[tool.hatch.envs.production]
dev-mode = false
features = []
Expand Down

0 comments on commit f56d025

Please sign in to comment.