Skip to content

Commit 032dbc7

Browse files
Add pre-commit hook / linter (#374)
* Add pre-commit hook / linter * Run linter * Fix GH workflow
1 parent 4181a80 commit 032dbc7

15 files changed

+143
-105
lines changed

.circleci/config.yml

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
version: 2
22
jobs:
3-
build_docs:
4-
docker:
5-
- image: circleci/python:3.7-stretch
6-
steps:
7-
- checkout
8-
- run:
9-
name: Set BASH_ENV
10-
command: |
11-
echo "set -e" >> $BASH_ENV;
12-
echo "export PATH=~/.local/bin:$PATH" >> $BASH_ENV;
13-
sudo apt update
14-
sudo apt install dvipng texlive-fonts-recommended texlive-latex-recommended texlive-latex-extra texlive-generic-extra latexmk texlive-xetex
15-
- restore_cache:
16-
keys:
17-
- pip-cache
18-
- run:
19-
name: Get dependencies and install
20-
command: |
21-
pip install --user -q --upgrade pip setuptools
22-
pip install --user -q --upgrade numpy matplotlib sphinx pydata-sphinx-theme
23-
pip install --user -e .
24-
- save_cache:
25-
key: pip-cache
26-
paths:
27-
- ~/.cache/pip
28-
- run:
29-
name: make html
30-
command: |
31-
make -C doc html
32-
- store_artifacts:
33-
path: doc/_build/html/
34-
destination: html
35-
- run:
36-
name: make tinybuild
37-
command: |
38-
make -C numpydoc/tests/tinybuild html
39-
- store_artifacts:
40-
path: numpydoc/tests/tinybuild/_build/html/
41-
destination: tinybuild
3+
build_docs:
4+
docker:
5+
- image: circleci/python:3.7-stretch
6+
steps:
7+
- checkout
8+
- run:
9+
name: Set BASH_ENV
10+
command: |
11+
echo "set -e" >> $BASH_ENV;
12+
echo "export PATH=~/.local/bin:$PATH" >> $BASH_ENV;
13+
sudo apt update
14+
sudo apt install dvipng texlive-fonts-recommended texlive-latex-recommended texlive-latex-extra texlive-generic-extra latexmk texlive-xetex
15+
- restore_cache:
16+
keys:
17+
- pip-cache
18+
- run:
19+
name: Get dependencies and install
20+
command: |
21+
pip install --user -q --upgrade pip setuptools
22+
pip install --user -q --upgrade numpy matplotlib sphinx pydata-sphinx-theme
23+
pip install --user -e .
24+
- save_cache:
25+
key: pip-cache
26+
paths:
27+
- ~/.cache/pip
28+
- run:
29+
name: make html
30+
command: |
31+
make -C doc html
32+
- store_artifacts:
33+
path: doc/_build/html/
34+
destination: html
35+
- run:
36+
name: make tinybuild
37+
command: |
38+
make -C numpydoc/tests/tinybuild html
39+
- store_artifacts:
40+
path: numpydoc/tests/tinybuild/_build/html/
41+
destination: tinybuild
4242

4343
workflows:
4444
version: 2

.github/workflows/lint.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: style
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
pre-commit:
7+
name: Linting
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v3
11+
- uses: actions/setup-python@v3
12+
- uses: pre-commit/[email protected]

.github/workflows/test.yml

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: numpydoc tests
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88

99
jobs:
1010
test:
@@ -15,48 +15,48 @@ jobs:
1515
python-version: ["3.7", "3.8", "3.9", "3.10"]
1616
sphinx-version: ["sphinx==1.8.0", "sphinx==2.1", "sphinx>3.0"]
1717
steps:
18-
- uses: actions/checkout@v2
19-
20-
- name: Python setup
21-
uses: actions/setup-python@v2
22-
with:
23-
python-version: ${{ matrix.python-version }}
24-
25-
- name: Setup environment
26-
run: |
27-
python -m venv venv
28-
source venv/bin/activate
29-
30-
- name: Install
31-
run: |
32-
python -m pip install --upgrade pip wheel setuptools
33-
python -m pip install -r test_requirements.txt -r doc/requirements.txt
34-
python -m pip install codecov
35-
python -m pip install ${{ matrix.sphinx-version }}
36-
python -m pip install .
37-
pip list
38-
39-
- name: Run test suite
40-
run: |
41-
pytest -v --pyargs .
42-
43-
- name: Test coverage
44-
run: |
45-
codecov
46-
47-
- name: Make sure CLI works
48-
run: |
49-
python -m numpydoc numpydoc.tests.test_main._capture_stdout
50-
echo '! python -m numpydoc numpydoc.tests.test_main._invalid_docstring' | bash
51-
python -m numpydoc --validate numpydoc.tests.test_main._capture_stdout
52-
echo '! python -m numpydoc --validate numpydoc.tests.test_main._docstring_with_errors' | bash
53-
54-
- name: Setup for doc build
55-
run: |
56-
sudo apt-get update
57-
sudo apt install texlive texlive-latex-extra latexmk dvipng
58-
59-
- name: Build documentation
60-
run: |
61-
make -C doc html SPHINXOPTS="-nT"
62-
make -C doc latexpdf SPHINXOPTS="-nT"
18+
- uses: actions/checkout@v2
19+
20+
- name: Python setup
21+
uses: actions/setup-python@v2
22+
with:
23+
python-version: ${{ matrix.python-version }}
24+
25+
- name: Setup environment
26+
run: |
27+
python -m venv venv
28+
source venv/bin/activate
29+
30+
- name: Install
31+
run: |
32+
python -m pip install --upgrade pip wheel setuptools
33+
python -m pip install -r requirements/test.txt -r doc/requirements.txt
34+
python -m pip install codecov
35+
python -m pip install ${{ matrix.sphinx-version }}
36+
python -m pip install .
37+
pip list
38+
39+
- name: Run test suite
40+
run: |
41+
pytest -v --pyargs .
42+
43+
- name: Test coverage
44+
run: |
45+
codecov
46+
47+
- name: Make sure CLI works
48+
run: |
49+
python -m numpydoc numpydoc.tests.test_main._capture_stdout
50+
echo '! python -m numpydoc numpydoc.tests.test_main._invalid_docstring' | bash
51+
python -m numpydoc --validate numpydoc.tests.test_main._capture_stdout
52+
echo '! python -m numpydoc --validate numpydoc.tests.test_main._docstring_with_errors' | bash
53+
54+
- name: Setup for doc build
55+
run: |
56+
sudo apt-get update
57+
sudo apt install texlive texlive-latex-extra latexmk dvipng
58+
59+
- name: Build documentation
60+
run: |
61+
make -C doc html SPHINXOPTS="-nT"
62+
make -C doc latexpdf SPHINXOPTS="-nT"

.pre-commit-config.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Install pre-commit hooks via
2+
# pre-commit install
3+
4+
repos:
5+
- repo: https://github.com/pre-commit/pre-commit-hooks
6+
rev: v4.1.0
7+
hooks:
8+
- id: trailing-whitespace
9+
- id: end-of-file-fixer
10+
- id: debug-statements
11+
- id: check-ast
12+
- id: mixed-line-ending
13+
- id: check-yaml
14+
args: [--allow-multiple-documents]
15+
- id: check-json
16+
- id: check-toml
17+
- id: check-added-large-files
18+
19+
- repo: https://github.com/pre-commit/mirrors-prettier
20+
rev: v2.6.2
21+
hooks:
22+
- id: prettier
23+
files: \.(html|md|yml|yaml)
24+
args: [--prose-wrap=preserve]
25+
26+
- repo: https://github.com/asottile/pyupgrade
27+
rev: v2.31.1
28+
hooks:
29+
- id: pyupgrade
30+
args: [--py37-plus]

doc/conf.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
#
32
# project-template documentation build configuration file, created by
43
# sphinx-quickstart on Mon Jan 18 14:44:12 2016.
@@ -61,7 +60,7 @@
6160
master_doc = 'index' # NOTE: will be changed to `root_doc` in sphinx 4
6261

6362
# General information about the project.
64-
project = u'numpydoc'
63+
project = 'numpydoc'
6564
copyright = f'2019-{date.today().year}, numpydoc maintainers'
6665

6766
# The version info for the project you're documenting, acts as replacement for
@@ -209,8 +208,8 @@
209208
# (source start file, target name, title,
210209
# author, documentclass [howto, manual, or own class]).
211210
latex_documents = [
212-
('index', 'numpydoc.tex', u'numpydoc Documentation',
213-
u'Numpydoc maintainers', 'manual'),
211+
('index', 'numpydoc.tex', 'numpydoc Documentation',
212+
'Numpydoc maintainers', 'manual'),
214213
]
215214

216215
# The name of an image file (relative to this directory) to place at the top of

doc/example.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
a line by itself, preferably preceded by a blank line.
1010
1111
"""
12-
from __future__ import division, absolute_import, print_function
1312

1413
import os # standard library imports first
1514

numpydoc/docscrape.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def _parse(self):
378378
self._parse_summary()
379379

380380
sections = list(self._read_sections())
381-
section_names = set([section for section, content in sections])
381+
section_names = {section for section, content in sections}
382382

383383
has_returns = 'Returns' in section_names
384384
has_yields = 'Yields' in section_names

numpydoc/docscrape_sphinx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def _str_member_list(self, name):
268268
out += [''] + autosum
269269

270270
if others:
271-
maxlen_0 = max(3, max([len(p.name) + 4 for p in others]))
271+
maxlen_0 = max(3, max(len(p.name) + 4 for p in others))
272272
hdr = "=" * maxlen_0 + " " + "=" * 10
273273
fmt = '%%%ds %%s ' % (maxlen_0,)
274274
out += ['', '', hdr]
@@ -382,7 +382,7 @@ def __str__(self, indent=0, func_role="obj"):
382382
else self._str_member_list('Attributes'),
383383
'methods': self._str_member_list('Methods'),
384384
}
385-
ns = dict((k, '\n'.join(v)) for k, v in ns.items())
385+
ns = {k: '\n'.join(v) for k, v in ns.items()}
386386

387387
rendered = self.template.render(**ns)
388388
return '\n'.join(self._str_indent(rendered.split('\n'), indent))

numpydoc/tests/test_docscrape.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- encoding:utf-8 -*-
21
from collections import namedtuple
32
from copy import deepcopy
43
import re

numpydoc/tests/test_full.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def test_MyClass(sphinx_app):
4141
src_dir, out_dir = sphinx_app.srcdir, sphinx_app.outdir
4242
class_rst = op.join(src_dir, 'generated',
4343
'numpydoc_test_module.MyClass.rst')
44-
with open(class_rst, 'r') as fid:
44+
with open(class_rst) as fid:
4545
rst = fid.read()
4646
assert r'numpydoc\_test\_module' in rst # properly escaped
4747
class_html = op.join(out_dir, 'generated',
4848
'numpydoc_test_module.MyClass.html')
49-
with open(class_html, 'r') as fid:
49+
with open(class_html) as fid:
5050
html = fid.read()
5151
# ensure that no autodoc weirdness ($) occurs
5252
assert '$self' not in html
@@ -66,7 +66,7 @@ def test_my_function(sphinx_app):
6666
out_dir = sphinx_app.outdir
6767
function_html = op.join(out_dir, 'generated',
6868
'numpydoc_test_module.my_function.html')
69-
with open(function_html, 'r') as fid:
69+
with open(function_html) as fid:
7070
html = fid.read()
7171
assert r'\*args' not in html
7272
assert '*args' in html
@@ -83,7 +83,7 @@ def test_reference(sphinx_app, html_file, expected_length):
8383
"""Test for bad references"""
8484
out_dir = sphinx_app.outdir
8585

86-
with open(op.join(out_dir, *html_file), 'r') as fid:
86+
with open(op.join(out_dir, *html_file)) as fid:
8787
html = fid.read()
8888

8989
reference_list = re.findall(r'<a class="fn-backref" href="\#id\d+">(.*)<\/a>', html)

numpydoc/tests/test_numpydoc.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- encoding:utf-8 -*-
21
import pytest
32
from io import StringIO
43
from copy import deepcopy
@@ -123,9 +122,9 @@ def _function_without_seealso_and_examples():
123122
# Validation configured off - expect no warnings
124123
(set(), [], []),
125124
# Validation on with expected warnings
126-
(set(['SA01', 'EX01']), ('SA01', 'EX01'), []),
125+
({'SA01', 'EX01'}, ('SA01', 'EX01'), []),
127126
# Validation on with only one activated check
128-
(set(['SA01']), ('SA01',), ('EX01',)),
127+
({'SA01'}, ('SA01',), ('EX01',)),
129128
),
130129
)
131130
def test_mangle_docstring_validation_warnings(
@@ -190,7 +189,7 @@ def test_update_config_exclude_str():
190189
app = MockApp()
191190
app.config.numpydoc_validation_checks = set()
192191
app.config.numpydoc_validation_exclude = "shouldnt-be-a-str"
193-
with pytest.raises(ValueError, match="\['shouldnt-be-a-str'\]"):
192+
with pytest.raises(ValueError, match=r"\['shouldnt-be-a-str'\]"):
194193
update_config(app)
195194

196195

numpydoc/tests/test_xref.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- encoding:utf-8 -*-
21
import pytest
32
from numpydoc.xref import make_xref, DEFAULT_LINKS
43

requirements/developer.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pre-commit>=2.18
File renamed without changes.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def read(fname):
4747
python_requires=">=3.7",
4848
extras_require={
4949
"testing": [
50-
req for req in read('test_requirements.txt').split('\n')
50+
req for req in read('requirements/test.txt').split('\n')
5151
if not req.startswith('#')
5252
],
5353
},

0 commit comments

Comments
 (0)