Skip to content

Commit

Permalink
Use pre-commit for linting and formatting (#72)
Browse files Browse the repository at this point in the history
* Remove more files with clean session

* Match workflow files to Landlab

* Use pre-commit hooks for linting

* Remove unused dependencies

* Remove coverage artifacts

* Back to flake8 from ruff

* Rename job and call the lint session

* Add pre-commit hooks
  • Loading branch information
mdpiper authored Aug 14, 2024
1 parent e09ec64 commit d978a30
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 26 deletions.
10 changes: 10 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[flake8]
exclude = docs, build, .nox
ignore =
E203
E501
W503
W605
max-line-length = 88
max-complexity = 18
select = B,C,E,F,W,T4,B9
6 changes: 3 additions & 3 deletions .github/workflows/format.yml → .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: Format
name: Lint

on: [push, pull_request]

jobs:

format:
lint:
name: Check for lint and format code to a standard style
if:
github.event_name == 'push' || github.event.pull_request.head.repo.full_name !=
Expand All @@ -24,4 +24,4 @@ jobs:
run: pip install nox

- name: Format code
run: nox -s format -- --check --verbose --diff
run: nox -s lint
File renamed without changes.
98 changes: 98 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
repos:
- repo: local
hooks:
- id: unnecessary_doctest_directives
name: Check for redundant doctest directives
description:
"Check for inline doctest directives that are specified globally through pyproject.toml"
types: [python]
entry: >
(?x)(
\+IGNORE_EXCEPTION_DETAIL|
\+NORMALIZE_WHITESPACE
)
language: pygrep

- repo: https://github.com/psf/black
rev: 24.4.0
hooks:
- id: black
name: black
description: "Black: The uncompromising Python code formatter"
entry: black
language: python
language_version: python3
minimum_pre_commit_version: 2.9.2
require_serial: true
types_or: [python, pyi]
- id: black-jupyter
name: black-jupyter
description:
"Black: The uncompromising Python code formatter (with Jupyter Notebook support)"
entry: black
language: python
minimum_pre_commit_version: 2.9.2
require_serial: true
types_or: [python, pyi, jupyter]
additional_dependencies: [".[jupyter]"]

- repo: https://github.com/keewis/blackdoc
rev: v0.3.9
hooks:
- id: blackdoc
description: "Black for doctests"
additional_dependencies: ["black==24.4.0"]
- id: blackdoc-autoupdate-black

- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies:
# - flake8-bugbear!=24.4.21
# - flake8-comprehensions
- flake8-simplify

- repo: https://github.com/nbQA-dev/nbQA
rev: 1.8.5
hooks:
- id: nbqa-pyupgrade
args: ["--py310-plus"]
# - id: nbqa-isort
- id: nbqa-flake8
args: ["--extend-ignore=E402"]

- repo: https://github.com/kynan/nbstripout
rev: 0.7.1
hooks:
- id: nbstripout
description: Strip output from jupyter notebooks
args: [--drop-empty-cells]

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
hooks:
- id: pyupgrade
args: [--py310-plus]

- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
name: isort (python)
# args: [--force-single-line-imports]
types: [python]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-builtin-literals
- id: check-added-large-files
- id: check-case-conflict
- id: check-toml
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: forbid-new-submodules
- id: mixed-line-ending
- id: trailing-whitespace
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
- rioxarray
- bmipy
- black
- ruff
- flake8
- isort
- nox
- pytest
Expand Down
16 changes: 5 additions & 11 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,10 @@ def test_cli(session: nox.Session) -> None:


@nox.session
def format(session: nox.Session) -> None:
def lint(session: nox.Session) -> None:
"""Clean lint and assert style."""
session.install(".[dev]")

if session.posargs:
black_args = session.posargs
else:
black_args = []

session.run("black", *black_args, *PATHS)
session.run("isort", *PATHS)
session.run("ruff", "check", *PATHS)
session.install("pre-commit")
session.run("pre-commit", "run", "--all-files")


@nox.session(name="prepare-docs")
Expand Down Expand Up @@ -148,6 +140,8 @@ def clean(session):
shutil.rmtree(f"{PACKAGE}.egg-info", ignore_errors=True)
shutil.rmtree(".pytest_cache", ignore_errors=True)
shutil.rmtree(".venv", ignore_errors=True)
if os.path.exists("coverage.xml"):
os.remove("coverage.xml")
if os.path.exists(".coverage"):
os.remove(".coverage")
for p in chain(ROOT.rglob("*.py[co]"), ROOT.rglob("__pycache__")):
Expand Down
11 changes: 0 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ Changelog = "https://github.com/csdms/bmi-topography/blob/main/CHANGES.md"

[project.optional-dependencies]
dev = [
"black",
"ruff",
"isort",
"nox",
]
build = [
Expand Down Expand Up @@ -118,14 +115,6 @@ force_grid_wrap = 0
combine_as_imports = true
line_length = 88

[tool.ruff]
exclude = ["docs"]
line-length = 88
ignore = [
"E203",
"E501",
]

[tool.coverage.run]
relative_files = true

Expand Down

0 comments on commit d978a30

Please sign in to comment.