Skip to content

Commit 3a59c91

Browse files
authored
Merge pull request #34 from csdms/mcflugen/add-pre-commit-file
Add pre-commit file for linters
2 parents b0a471c + 64b04e9 commit 3a59c91

File tree

9 files changed

+164
-56
lines changed

9 files changed

+164
-56
lines changed

.github/workflows/format.yml

-21
This file was deleted.

.github/workflows/lint.yml

-23
This file was deleted.

.github/workflows/test.yml

+3-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
strategy:
2121
matrix:
2222
os: [ubuntu-latest, macos-latest, windows-latest]
23-
python-version: ["3.9", "3.10", "3.11", "3.12"]
23+
python-version: ["3.10", "3.11", "3.12"]
2424

2525
steps:
2626
- uses: actions/checkout@v4
@@ -36,17 +36,13 @@ jobs:
3636
conda info
3737
conda list
3838
39-
- name: Install requirements
40-
run: |
41-
mamba install --file=requirements.txt --file=requirements-testing.txt
42-
4339
- name: Build and install package
44-
run: pip install -e .
40+
run: pip install -e .[testing]
4541

4642
- name: Test
4743
run: |
4844
pytest --cov=heat --cov-report=xml:./coverage.xml -vvv
49-
bmi-test heat:BmiHeat --config-file=./examples/heat.yaml --root-dir=./examples -vvv
45+
bmi-test heat:BmiHeat --config-file=${GITHUB_WORKSPACE}/examples/heat.yaml --root-dir=examples -vvv
5046
5147
- name: Coveralls
5248
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.11'

.pre-commit-config.yaml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
repos:
2+
- repo: https://github.com/psf/black
3+
rev: 23.12.1
4+
hooks:
5+
- id: black
6+
name: black
7+
description: "Black: The uncompromising Python code formatter"
8+
entry: black
9+
language: python
10+
language_version: python3
11+
minimum_pre_commit_version: 2.9.2
12+
require_serial: true
13+
types_or: [python, pyi]
14+
15+
- repo: https://github.com/pycqa/flake8
16+
rev: 7.0.0
17+
hooks:
18+
- id: flake8
19+
additional_dependencies:
20+
- flake8-bugbear
21+
- flake8-comprehensions
22+
- flake8-simplify
23+
args: [--max-line-length=88]
24+
25+
- repo: https://github.com/asottile/pyupgrade
26+
rev: v3.15.0
27+
hooks:
28+
- id: pyupgrade
29+
args: [--py310-plus]
30+
31+
- repo: https://github.com/pycqa/isort
32+
rev: 5.13.2
33+
hooks:
34+
- id: isort
35+
36+
- repo: https://github.com/pre-commit/pre-commit-hooks
37+
rev: v4.5.0
38+
hooks:
39+
- id: check-builtin-literals
40+
- id: check-added-large-files
41+
- id: check-case-conflict
42+
- id: check-toml
43+
- id: check-yaml
44+
- id: debug-statements
45+
- id: end-of-file-fixer
46+
- id: forbid-new-submodules
47+
- id: mixed-line-ending
48+
- id: trailing-whitespace
49+
50+
- repo: https://github.com/pre-commit/mirrors-mypy
51+
rev: v1.8.0
52+
hooks:
53+
- id: mypy
54+
language_version: python3.12
55+
files: src/.*\.py$

CHANGES.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Changelog for bmi-example-python
3131
- Match bmipy (#12)
3232
- Use GitHub Actions for continuous integration (#15)
3333
- Dimensionalize flattened values passed into set_value (#17)
34-
- Update CI (#18)
34+
- Update CI (#18)
3535
- Switch from versioneer to zest.releaser
3636
- Remove obsolete docs directory
3737

@@ -46,4 +46,3 @@ Changelog for bmi-example-python
4646
------------------
4747

4848
- Initial release
49-

LICENSE

-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22-

heat/bmi_heat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def initialize(self, filename=None):
3939
if filename is None:
4040
self._model = Heat()
4141
elif isinstance(filename, str):
42-
with open(filename, "r") as file_obj:
42+
with open(filename) as file_obj:
4343
self._model = Heat.from_file_like(file_obj.read())
4444
else:
4545
self._model = Heat.from_file_like(filename)

heat/heat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def solve_2d(temp, spacing, out=None, alpha=1.0, time_step=1.0):
5353
return np.add(temp, out, out=out)
5454

5555

56-
class Heat(object):
56+
class Heat:
5757

5858
"""Solve the Heat equation on a grid.
5959

noxfile.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from __future__ import annotations
2+
3+
import os
4+
import pathlib
5+
import shutil
6+
7+
import nox
8+
9+
PROJECT = "heat"
10+
ROOT = pathlib.Path(__file__).parent
11+
12+
13+
@nox.session
14+
def test(session: nox.Session) -> None:
15+
"""Run the tests."""
16+
session.install(".[testing]")
17+
18+
args = ["--cov", PROJECT, "-vvv"] + session.posargs
19+
20+
if "CI" in os.environ:
21+
args.append(f"--cov-report=xml:{ROOT.absolute()!s}/coverage.xml")
22+
session.run("pytest", *args)
23+
24+
if "CI" not in os.environ:
25+
session.run("coverage", "report", "--ignore-errors", "--show-missing")
26+
27+
28+
@nox.session
29+
def lint(session: nox.Session) -> None:
30+
"""Look for lint."""
31+
session.install("pre-commit")
32+
session.run("pre-commit", "run", "--all-files")
33+
34+
35+
@nox.session
36+
def build(session: nox.Session) -> None:
37+
session.install("pip")
38+
session.install("build")
39+
session.run("python", "--version")
40+
session.run("pip", "--version")
41+
session.run("python", "-m", "build", "--outdir", "./build/wheelhouse")
42+
43+
44+
@nox.session(name="publish-testpypi")
45+
def publish_testpypi(session):
46+
"""Publish wheelhouse/* to TestPyPI."""
47+
session.run("twine", "check", "build/wheelhouse/*")
48+
session.run(
49+
"twine",
50+
"upload",
51+
"--skip-existing",
52+
"--repository-url",
53+
"https://test.pypi.org/legacy/",
54+
"build/wheelhouse/*.tar.gz",
55+
)
56+
57+
58+
@nox.session(name="publish-pypi")
59+
def publish_pypi(session):
60+
"""Publish wheelhouse/* to PyPI."""
61+
session.run("twine", "check", "build/wheelhouse/*")
62+
session.run(
63+
"twine",
64+
"upload",
65+
"--skip-existing",
66+
"build/wheelhouse/*.tar.gz",
67+
)
68+
69+
70+
@nox.session(python=False)
71+
def clean(session):
72+
"""Remove all .venv's, build files and caches in the directory."""
73+
folders = (
74+
(ROOT,) if not session.posargs else (pathlib.Path(f) for f in session.posargs)
75+
)
76+
for folder in folders:
77+
if not str(folder.resolve()).startswith(str(ROOT.resolve())):
78+
session.log(f"skipping {folder}: folder is outside of repository")
79+
continue
80+
81+
with session.chdir(folder):
82+
session.log(f"cleaning {folder}")
83+
84+
shutil.rmtree("build", ignore_errors=True)
85+
shutil.rmtree("dist", ignore_errors=True)
86+
shutil.rmtree(f"src/{PROJECT}.egg-info", ignore_errors=True)
87+
shutil.rmtree(".pytest_cache", ignore_errors=True)
88+
shutil.rmtree(".venv", ignore_errors=True)
89+
90+
for pattern in ["*.py[co]", "__pycache__"]:
91+
_clean_rglob(pattern)
92+
93+
94+
def _clean_rglob(pattern):
95+
nox_dir = pathlib.Path(".nox")
96+
97+
for p in pathlib.Path(".").rglob(pattern):
98+
if nox_dir in p.parents:
99+
continue
100+
if p.is_dir():
101+
p.rmdir()
102+
else:
103+
p.unlink()

0 commit comments

Comments
 (0)