Skip to content

Commit

Permalink
switch to pre-commit (pyvista#2392)
Browse files Browse the repository at this point in the history
* update docs

* add in remainder of tools

* add in pre-commit

* implement mypy and update yml

* remove static req

* Update CONTRIBUTING.rst

Co-authored-by: Tetsuo Koyama <[email protected]>

* Add newline at end of pyproject.toml

* Apply suggestions from code review

Co-authored-by: Andras Deak <[email protected]>

* Update pyvista/ext/plot_directive.py

* add cross-platform notes for windows

* validate workflow files

Co-authored-by: Tetsuo Koyama <[email protected]>
Co-authored-by: Andras Deak <[email protected]>
Co-authored-by: Andras Deak <[email protected]>
  • Loading branch information
4 people authored Mar 30, 2022
1 parent c8be2c4 commit 0fde72b
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 115 deletions.
4 changes: 4 additions & 0 deletions .codespellrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[codespell]
skip = *.pyc,*.txt,*.gif,*.png,*.jpg,*.ply,*.vtk,*.vti,*.vtu,*.js,*.html,*.doctree,*.ttf,*.woff,*.woff2,*.eot,*.mp4,*.inv,*.pickle,*.ipynb,flycheck*,./.git/*,./.hypothesis/*,*.yml,doc/_build/*,./doc/images/*,./dist/*,*~,.hypothesis*,./doc/examples/*,*.mypy_cache/*,*cover,./tests/tinypages/_build/*,*/_autosummary/*
ignore-words-list = lod,byteorder,flem,parm,doubleclick,revered
quiet-level = 3
3 changes: 2 additions & 1 deletion .github/workflows/docker-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: Build and Publish Docker Image
on:
workflow_dispatch:
push:
tags: "*"
tags:
- "*"
branches:
- main
pull_request:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
cache:
description: 'Use build cache'
required: false
default: true
default: "true"
# No cache enabled for `schedule` and `push`
schedule:
- cron: '0 0 1 * *' # once a month on main
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/pre-commit-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Pre-commit auto-update

on:
# every sunday at midnight GMT
schedule:
- cron: "0 0 * * 0"
# on demand
workflow_dispatch:

jobs:
auto-update:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.10'

- name: Install pre-commit
run: pip install pre-commit

- name: Run pre-commit autoupdate
run: pre-commit autoupdate

- uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch: ci/update-pre-commit-hooks
title: Update pre-commit hooks
commit-message: "update pre-commit hooks"
body: Update versions of pre-commit hooks to latest version.
28 changes: 4 additions & 24 deletions .github/workflows/style_docstr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,11 @@ jobs:
with:
python-version: 3.9

- name: Install style requirements
run: |
pip install -r requirements_style.txt --disable-pip-version-check
- name: Code Linting and Spelling Check
run: |
make stylecheck
staticcheck:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Install pre-commit
run: pip install pre-commit

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Install style requirements
run: |
pip install -r requirements_static.txt --disable-pip-version-check
- name: Static code check
run: |
make mypy
- name: Run pre-commit
run: pre-commit run --all-files

docstringcheck:
runs-on: ubuntu-20.04
Expand Down
62 changes: 62 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black

- repo: https://github.com/pycqa/isort
rev: 5.10.1
hooks:
- id: isort

- repo: https://gitlab.com/PyCQA/flake8
rev: 3.9.2
hooks:
- id: flake8
additional_dependencies: [
"flake8-black==0.3.2",
"flake8-isort==4.1.1",
"flake8-quotes==3.3.1",
]

- repo: https://github.com/codespell-project/codespell
rev: v2.1.0
hooks:
- id: codespell
args: [
"doc examples examples_flask pyvista tests",
"*.py *.rst *.md",
]

- repo: https://github.com/pycqa/pydocstyle
rev: 6.1.1
hooks:
- id: pydocstyle
additional_dependencies: [toml==0.10.2]
files: ^(pyvista/|other/)
exclude: ^pyvista/ext/

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.942
hooks:
- id: mypy
exclude: ^(doc/|tests/|examples/|pyvista/ext/)
additional_dependencies: [
"mypy-extensions==0.4.3",
"toml==0.10.2",
"typing-extensions==4.1.1",
]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: check-merge-conflict
- id: debug-statements
- id: no-commit-to-branch
args: [--branch, main]

# this validates our github workflow files
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.14.0
hooks:
- id: check-github-workflows
77 changes: 43 additions & 34 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,10 @@ Python <https://www.python.org/dev/peps/pep-0020/>`_. When in doubt:
import this
Please also use the linting and format checks through the ``Makefile``
to make sure your code adheres to our standards regarding trailing white
space, unused imports, and sorting of imports.
PyVista uses `pre-commit`_ to enforce PEP8 and other styles
automatically. Please see the `Style Checking section <#style-checking>`_ for
further details.

To test the format locally, run:

.. code:: bash
make stylecheck
To automatically fix style issues (including those found by ``black``), run:

.. code:: bash
make format

Branch Naming Conventions
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -239,17 +228,18 @@ track down any new issues from your changes.

To run our comprehensive suite of unit tests, install all the
dependencies listed in ``requirements_test.txt``,
``requirements_docs.txt``, ``requirements_style.txt``:
``requirements_docs.txt``

.. code:: bash
pip install -r requirements_test.txt
pip install -r requirements_docs.txt
pip install -r requirements_style.txt
Then, if you have everything installed, you can run the various test
suites.

Unit Testing
~~~~~~~~~~~~
Run the primary test suite and generate coverage report:

.. code:: bash
Expand All @@ -264,40 +254,56 @@ leverage multiple processes. Example usage:
python -m pytest -n <NUMCORE> --cov pyvista
Run all code examples in the docstrings. Be sure to export the
``PYVISTA_OFF_SCREEN`` environmental variable to enable off-screen
plotting.
Documentation Testing
~~~~~~~~~~~~~~~~~~~~~
Run all code examples in the docstrings with:

.. code:: bash
export PYVISTA_OFF_SCREEN=True
python -m pytest -v --doctest-modules pyvista
Run documentation testing by running:
Build the documentation on Linux or Mac OS with:

.. code:: bash
make
make -C doc html
If you are running windows and ``make`` is unavailable, then run:
Build the documentation on Windows with:

.. code:: bash
cd doc
python -msphinx -M html . _build

pydocstyle pyvista
The generated documentation can be found in the ``doc/_build/html``
directory.

codespell pyvista/ examples/ tests/ -S "*.pyc,*.txt,*.gif,*.png,*.jpg,*.ply,*.vtk,*.vti,*.js,*.html,*.doctree,*.ttf,*.woff,*.woff2,*.eot,*.mp4,*.inv,*.pickle,*.ipynb,flycheck*" -I "ignore_words.txt"
Style Checking
~~~~~~~~~~~~~~
PyVista follows PEP8 standard as outlined in the `Coding Style section
<#coding-style>`_ and implements style checking using `pre-commit`_.

And finally, test the documentation examples:
To ensure your code meets minimum code styling standards, run::

.. code:: bash
pip install pre-commit
pre-commit run --all-files

cd doc
make clean
make doctest
make html -b linkcheck
You can also install this as a pre-commit hook by running::

The finished documentation can be found in the ``doc/_build/html``
directory.
pre-commit install

This way, it's not possible for you to push code that fails the style
checks. For example, each commit automatically checks that you meet the style
requirements::

$ pre-commit install
$ git commit -m "added my cool feature"
black....................................................................Passed
isort....................................................................Passed
flake8...................................................................Passed
codespell................................................................Passed

The actual installation of the environment happens before the first commit
following ``pre-commit install``. This will take a bit longer, but subsequent
commits will only trigger the actual style checks.

Notes Regarding Image Regression Testing
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -511,3 +517,6 @@ should not wait until a minor release. The steps for a patch release
4. If deemed necessary, create a release notes page. Also, open the PR
from conda and follow the directions in step 10 in the minor release
section.


.. _pre-commit: https://pre-commit.com/
34 changes: 1 addition & 33 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,10 @@
CODE_DIRS ?= doc examples examples_flask pyvista tests
# Files in top level directory
CODE_FILES ?= *.py *.rst *.md
CODESPELL_SKIP ?= "*.pyc,*.txt,*.gif,*.png,*.jpg,*.ply,*.vtk,*.vti,*.vtu,*.js,*.html,*.doctree,*.ttf,*.woff,*.woff2,*.eot,*.mp4,*.inv,*.pickle,*.ipynb,flycheck*,./.git/*,./.hypothesis/*,*.yml,doc/_build/*,./doc/images/*,./dist/*,*~,.hypothesis*,./doc/examples/*,*.mypy_cache/*,*cover,./tests/tinypages/_build/*,*/_autosummary/*"
CODESPELL_IGNORE ?= "ignore_words.txt"

# doctest modules must be off screen to avoid plotting everything
doctest-modules: export PYVISTA_OFF_SCREEN = True
# doctest-modules-local-namespace must be off screen to avoid plotting everything
doctest-modules-local-namespace: export PYVISTA_OFF_SCREEN = True

stylecheck: codespell pydocstyle lint

format: isort stylize

codespell:
@echo "Running codespell"
@codespell $(CODE_DIRS) $(CODE_FILES) -S $(CODESPELL_SKIP) -I $(CODESPELL_IGNORE)

pydocstyle:
@echo "Running pydocstyle"
@pydocstyle pyvista --match='(?!coverage).*.py'

doctest-modules:
@echo "Runnnig module doctesting"
pytest -v --doctest-modules pyvista
Expand Down Expand Up @@ -50,20 +35,3 @@ coverage-docs:
@echo "Reporting documentation coverage"
@make -C doc html SPHINXOPTS="-Q" -b coverage
@cat doc/_build/coverage/python.txt

mypy:
@echo "Running mypy static type checking"
mypy pyvista/core/ --no-incremental
mypy pyvista/themes.py --no-incremental

lint:
@echo "Linting with flake8"
@flake8 $(CODE_DIRS) *.py

isort:
@echo "Formatting with isort"
isort .

stylize:
@echo "Formatting"
black .
6 changes: 0 additions & 6 deletions ignore_words.txt

This file was deleted.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ line-length = 100
skip-string-normalization = true
target-version = ["py39"]
exclude='\.eggs|\.git|\.mypy_cache|\.tox|\.venv|_build|buck-out|build|dist|node_modules'

[tool.pydocstyle]
match = '(?!coverage).*.py'
6 changes: 3 additions & 3 deletions pyvista/ext/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def write(self, *ignored: Any) -> None:

def build_c_coverage(self) -> None:
# Fetch all the info from the header files
c_objects = self.env.domaindata['c']['objects']
c_objects = self.env.domaindata['c']['objects'] # type: ignore
for filename in self.c_sourcefiles:
undoc: Set[Tuple[str, str]] = set()
with open(filename) as f:
Expand Down Expand Up @@ -168,7 +168,7 @@ def ignore_pyobj(self, full_name: str) -> bool:
return False

def build_py_coverage(self) -> None:
objects = self.env.domaindata['py']['objects']
objects = self.env.domaindata['py']['objects'] # type: ignore

# objects are sometimes not referenced in the docs as they are
# in the source. Here we simply grab the method and class of
Expand All @@ -178,7 +178,7 @@ def build_py_coverage(self) -> None:
# only include method and class
abbr_names.add(method_from_obj(obj_name))

modules = self.env.domaindata['py']['modules']
modules = self.env.domaindata['py']['modules'] # type: ignore
for mod_name in self.add_modules:
modules[mod_name] = None

Expand Down
2 changes: 1 addition & 1 deletion pyvista/ext/plot_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def _split_code_at_show(text):

# the context of the plot for all directives specified with the
# :context: option
plot_context = dict()
plot_context = {}


class ImageFile:
Expand Down
4 changes: 0 additions & 4 deletions requirements_static.txt

This file was deleted.

8 changes: 0 additions & 8 deletions requirements_style.txt

This file was deleted.

0 comments on commit 0fde72b

Please sign in to comment.