Skip to content

Commit

Permalink
Add prettier for non-python formatting
Browse files Browse the repository at this point in the history
Adding prettier for consistent formatting of non-python files. Ruff format only applies to python files, however, other files in the repo still have similar concerns (consistency, whitespace thrashing, etc). Prettier addresses this concern in the same way as ruff; minimal configurability to prevent bikeshedding.

All non-conformant files have been formatted to pass the pre-commit hook. Very few changes were needed; several yaml files were modified to have consistent whitespace and indentation, including the pre-commit configuration file.

Modern text editors and IDEs (efficiently) support prettier via plugin; this is recommended for local formatting. A cli tool is available, however, prettier itself is written in javascript so it is only accessible via npm/yarn (`npx prettier`/`yarn dlx prettier`). While this is unfortunate, there are very few tools that format general source files, and none within the python ecosystem.

Pre-commit does address this capability gap indirectly. It can be run via `pre-commit run prettier`, either with the `--all-files` flag or by adding a modified file to your working tree. While suboptimal, this does allow local formatting/validation if needed.
  • Loading branch information
cjntaylor committed Dec 28, 2023
1 parent 1af2601 commit 8ba3ef4
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 109 deletions.
52 changes: 26 additions & 26 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ jobs:
runs-on: ubuntu-latest
environment: release
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.x
- name: Install dependencies
run: pip install build
- name: Create packages
run: python -m build
- name: Archive packages
uses: actions/upload-artifact@v3
with:
name: dist
path: dist
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.x
- name: Install dependencies
run: pip install build
- name: Create packages
run: python -m build
- name: Archive packages
uses: actions/upload-artifact@v3
with:
name: dist
path: dist

publish:
name: Publish build artifacts to the PyPI
Expand All @@ -37,10 +37,10 @@ jobs:
permissions:
id-token: write
steps:
- name: Retrieve packages
uses: actions/download-artifact@v3
- name: Upload packages
uses: pypa/gh-action-pypi-publish@release/v1
- name: Retrieve packages
uses: actions/download-artifact@v3
- name: Upload packages
uses: pypa/gh-action-pypi-publish@release/v1

release:
name: Create a GitHub release
Expand All @@ -49,11 +49,11 @@ jobs:
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- id: changelog
uses: agronholm/release-notes@v1
with:
path: docs/versionhistory.rst
- uses: ncipollo/release-action@v1
with:
body: ${{ steps.changelog.outputs.changelog }}
- uses: actions/checkout@v4
- id: changelog
uses: agronholm/release-notes@v1
with:
path: docs/versionhistory.rst
- uses: ncipollo/release-action@v1
with:
body: ${{ steps.changelog.outputs.changelog }}
150 changes: 75 additions & 75 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ jobs:
tests-changed: ${{ steps.changed-files.outputs.tests_any_changed }}
docs-changed: ${{ steps.changed-files.outputs.doc_any_changed }}
steps:
- uses: actions/checkout@v4
- name: Get changed files by category
id: changed-files
uses: tj-actions/changed-files@v37
with:
files_yaml: |
workflow:
- .github/workflows/test.yml
pyproject:
- pyproject.toml
src:
- src/**
tests:
- tests/**
doc:
- README.rst
- docs/**
- uses: actions/checkout@v4
- name: Get changed files by category
id: changed-files
uses: tj-actions/changed-files@v37
with:
files_yaml: |
workflow:
- .github/workflows/test.yml
pyproject:
- pyproject.toml
src:
- src/**
tests:
- tests/**
doc:
- README.rst
- docs/**
pyright:
runs-on: ubuntu-latest
Expand All @@ -42,19 +42,19 @@ jobs:
|| (needs.changed-files.outputs.src-changed == 'true')
}}
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.x
- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: pip-pyright
- name: Install dependencies
run: pip install -e . pyright pytest
- name: Run pyright
run: pyright --verifytypes anyio
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.x
- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: pip-pyright
- name: Install dependencies
run: pip install -e . pyright pytest
- name: Run pyright
run: pyright --verifytypes anyio

test:
strategy:
Expand All @@ -63,14 +63,14 @@ jobs:
os: [ubuntu-latest]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", pypy-3.10]
include:
- os: macos-latest
python-version: "3.8"
- os: macos-latest
python-version: "3.11"
- os: windows-latest
python-version: "3.8"
- os: windows-latest
python-version: "3.11"
- os: macos-latest
python-version: "3.8"
- os: macos-latest
python-version: "3.11"
- os: windows-latest
python-version: "3.8"
- os: windows-latest
python-version: "3.11"
runs-on: ${{ matrix.os }}
needs: changed-files
if: |
Expand All @@ -81,28 +81,28 @@ jobs:
|| (needs.changed-files.outputs.tests-changed == 'true')
}}
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
cache: pip
cache-dependency-path: pyproject.toml
- name: Install dependencies
run: pip install -e .[test]
- name: Test with pytest
run: |
coverage run -m pytest -v
coverage xml
timeout-minutes: 5
env:
PYTEST_DISABLE_PLUGIN_AUTOLOAD: 1
- name: Upload Coverage
uses: coverallsapp/github-action@v2
with:
parallel: true
file: coverage.xml
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
cache: pip
cache-dependency-path: pyproject.toml
- name: Install dependencies
run: pip install -e .[test]
- name: Test with pytest
run: |
coverage run -m pytest -v
coverage xml
timeout-minutes: 5
env:
PYTEST_DISABLE_PLUGIN_AUTOLOAD: 1
- name: Upload Coverage
uses: coverallsapp/github-action@v2
with:
parallel: true
file: coverage.xml

docs:
runs-on: ubuntu-latest
Expand All @@ -115,24 +115,24 @@ jobs:
|| (needs.changed-files.outputs.docs-changed == 'true')
}}
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: pip
cache-dependency-path: pyproject.toml
- name: Install dependencies
run: pip install -e .[doc]
- name: Build documentation
run: sphinx-build -W docs build/sphinx
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: pip
cache-dependency-path: pyproject.toml
- name: Install dependencies
run: pip install -e .[doc]
- name: Build documentation
run: sphinx-build -W docs build/sphinx

coveralls:
name: Finish Coveralls
needs: test
runs-on: ubuntu-latest
steps:
- name: Finished
uses: coverallsapp/github-action@v2
with:
parallel-finished: true
- name: Finished
uses: coverallsapp/github-action@v2
with:
parallel-finished: true
13 changes: 9 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repos:
- id: debug-statements
- id: end-of-file-fixer
- id: mixed-line-ending
args: [ "--fix=lf" ]
args: [--fix=lf]
- id: trailing-whitespace

- repo: https://github.com/astral-sh/ruff-pre-commit
Expand All @@ -22,6 +22,11 @@ repos:
args: [--fix, --show-fixes]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
hooks:
- id: prettier

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
Expand All @@ -34,6 +39,6 @@ repos:
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal
16 changes: 16 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# macOS resource forks
._*
.DS_Store

# Build artifacts
/build
/dist

# Generated environments
/venv

# Cache files
/.hypothesis
/.mypy_cache
/.pytest_cache
/.ruff_cache
4 changes: 4 additions & 0 deletions .prettierrc.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
trailingComma = "none"
tabWidth = 2
semi = false
printWidth = 100
8 changes: 4 additions & 4 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sphinx:
fail_on_warning: true

python:
install:
- method: pip
path: .
extra_requirements: [doc]
install:
- method: pip
path: .
extra_requirements: [doc]
5 changes: 5 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Version history
===============

**UNRELEASED**

- Add `Prettier <https://prettier.io/>`_ to the pre-commit configuration to enforce
consistent formatting of non-python files (PR by Colin Taylor)

This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.

**4.2.0**
Expand Down

0 comments on commit 8ba3ef4

Please sign in to comment.