Skip to content

Commit 803a9bd

Browse files
authored
Align behavior of pre-commit hook and Sphinx extension (#476)
* Extract check selection logic from sphinx side into utility function. * Add test cases for get_validation_checks(). * Change pre-commit hook behavior from check exclusion to check selection like sphinx side. * Update example configs for pre-commit hook in docs. * Add global exclusion option to pre-commit hook. * Add test cases for global exclusion pre-commit option. * Update docs for global exclusion option. * Deprecate override_GL08 since global exclusion covers this. * Expand override logic for pre-commit hook to any check. * Update override examples in docs. * Check for exclusion before running checks. * Port inline comments for ignoring checks to sphinx side. * Move note in docs on inline comments to its own section now that both sides support it. * Remove ignore comment logic from hook now that it is in validate(). * Add note on inline comment usage for --validate. * Add numpydoc_validation_overrides option to Sphinx extension side. * Add numpydoc_validation_overrides option to the docs. * Refine additions to docs. * Clarify comment. * Switch to typing for Python 3.8 * Fix check on type.
1 parent 058f1db commit 803a9bd

10 files changed

+478
-116
lines changed

doc/install.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,17 @@ numpydoc_validation_exclude : set
138138
validation.
139139
Only has an effect when docstring validation is activated, i.e.
140140
``numpydoc_validation_checks`` is not an empty set.
141+
numpydoc_validation_overrides : dict
142+
A dictionary mapping :ref:`validation checks <validation_checks>` to a
143+
container of strings using :py:mod:`re` syntax specifying patterns to
144+
ignore for docstring validation.
145+
For example, the following skips the ``SS02`` check for docstrings
146+
starting with the word ``Process``::
147+
148+
numpydoc_validation_overrides = {"SS02": [r'^Process ']}
149+
150+
The default is an empty dictionary meaning no overrides.
151+
Only has an effect when docstring validation is activated, i.e.
152+
``numpydoc_validation_checks`` is not an empty set. Use
153+
:ref:`inline ignore comments <inline_ignore_comments>` to turn off
154+
specific checks for parts of your code.

doc/validation.rst

Lines changed: 89 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Validation
33
==========
44

5+
.. _pre_commit_hook:
6+
57
Docstring Validation using Pre-Commit Hook
68
------------------------------------------
79

@@ -22,44 +24,55 @@ command line options for this hook:
2224
2325
$ python -m numpydoc.hooks.validate_docstrings --help
2426
25-
Using a config file provides additional customization. Both
26-
``pyproject.toml`` and ``setup.cfg`` are supported; however, if the
27-
project contains both you must use the ``pyproject.toml`` file.
28-
The example below configures the pre-commit hook to ignore three checks
29-
and specifies exceptions to the checks ``SS05`` (allow docstrings to
30-
start with "Process ", "Assess ", or "Access ") and ``GL08`` (allow
31-
the class/method/function with name "__init__" to not have a docstring).
27+
Using a config file provides additional customization. Both ``pyproject.toml``
28+
and ``setup.cfg`` are supported; however, if the project contains both
29+
you must use the ``pyproject.toml`` file. The example below configures
30+
the pre-commit hook as follows:
31+
32+
* ``checks``: Report findings on all checks except ``EX01``, ``SA01``, and
33+
``ES01`` (using the same logic as the :ref:`validation during Sphinx build
34+
<validation_during_sphinx_build>` for ``numpydoc_validation_checks``).
35+
* ``exclude``: Don't report issues on objects matching any of the regular
36+
regular expressions ``\.undocumented_method$`` or ``\.__repr__$``. This
37+
maps to ``numpydoc_validation_exclude`` from the
38+
:ref:`Sphinx build configuration <validation_during_sphinx_build>`.
39+
* ``override_SS05``: Allow docstrings to start with "Process ", "Assess ",
40+
or "Access ". To override different checks, add a field for each code in
41+
the form of ``override_<code>`` with a collection of regular expression(s)
42+
to search for in the contents of a docstring, not the object name. This
43+
maps to ``numpydoc_validation_overrides`` from the
44+
:ref:`Sphinx build configuration <validation_during_sphinx_build>`.
3245

3346
``pyproject.toml``::
3447

3548
[tool.numpydoc_validation]
36-
ignore = [
49+
checks = [
50+
"all", # report on all checks, except the below
3751
"EX01",
3852
"SA01",
3953
"ES01",
4054
]
41-
override_SS05 = '^((Process|Assess|Access) )'
42-
override_GL08 = '^(__init__)$'
55+
exclude = [ # don't report on objects that match any of these regex
56+
'\.undocumented_method$',
57+
'\.__repr__$',
58+
]
59+
override_SS05 = [ # override SS05 to allow docstrings starting with these words
60+
'^Process ',
61+
'^Assess ',
62+
'^Access ',
63+
]
4364

4465
``setup.cfg``::
4566

4667
[tool:numpydoc_validation]
47-
ignore = EX01,SA01,ES01
48-
override_SS05 = ^((Process|Assess|Access) )
49-
override_GL08 = ^(__init__)$
50-
51-
For more fine-tuned control, you can also include inline comments to tell the
52-
validation hook to ignore certain checks:
68+
checks = all,EX01,SA01,ES01
69+
exclude = \.undocumented_method$,\.__repr__$
70+
override_SS05 = ^Process ,^Assess ,^Access ,
5371

54-
.. code-block:: python
55-
56-
class SomeClass: # numpydoc ignore=EX01,SA01,ES01
57-
"""This is the docstring for SomeClass."""
72+
In addition to the above, :ref:`inline ignore comments <inline_ignore_comments>`
73+
can be used to ignore findings on a case by case basis.
5874

59-
def __init__(self): # numpydoc ignore=GL08
60-
pass
61-
62-
If any issues are found when commiting, a report is printed out and the
75+
If any issues are found when commiting, a report is printed out, and the
6376
commit is halted:
6477

6578
.. code-block:: output
@@ -77,7 +90,9 @@ commit is halted:
7790
| src/pkg/module.py:33 | module.MyClass.parse | RT03 | Return value has no description |
7891
+----------------------+----------------------+---------+--------------------------------------+
7992
80-
See below for a full listing of checks.
93+
See :ref:`below <validation_checks>` for a full listing of checks.
94+
95+
.. _validation_via_cli:
8196

8297
Docstring Validation using Python
8398
---------------------------------
@@ -94,12 +109,16 @@ This will validate that the docstring can be built.
94109
For an exhaustive validation of the formatting of the docstring, use the
95110
``--validate`` parameter. This will report the errors detected, such as
96111
incorrect capitalization, wrong order of the sections, and many other
97-
issues.
112+
issues. Note that this will honor :ref:`inline ignore comments <inline_ignore_comments>`,
113+
but will not look for any configuration like the :ref:`pre-commit hook <pre_commit_hook>`
114+
or :ref:`Sphinx extension <validation_during_sphinx_build>` do.
115+
116+
.. _validation_during_sphinx_build:
98117

99118
Docstring Validation during Sphinx Build
100119
----------------------------------------
101120

102-
It is also possible to run docstring validation as part of the sphinx build
121+
It is also possible to run docstring validation as part of the Sphinx build
103122
process.
104123
This behavior is controlled by the ``numpydoc_validation_checks`` configuration
105124
parameter in ``conf.py``.
@@ -109,7 +128,7 @@ following line to ``conf.py``::
109128

110129
numpydoc_validation_checks = {"PR01"}
111130

112-
This will cause a sphinx warning to be raised for any (non-module) docstring
131+
This will cause a Sphinx warning to be raised for any (non-module) docstring
113132
that has undocumented parameters in the signature.
114133
The full set of validation checks can be activated by::
115134

@@ -125,6 +144,48 @@ special keyword ``"all"``::
125144
# Report warnings for all validation checks except GL01, GL02, and GL05
126145
numpydoc_validation_checks = {"all", "GL01", "GL02", "GL05"}
127146

147+
In addition, you can exclude any findings on certain objects with
148+
``numpydoc_validation_exclude``, which maps to ``exclude`` in the
149+
:ref:`pre-commit hook setup <pre_commit_hook>`::
150+
151+
# don't report on objects that match any of these regex
152+
numpydoc_validation_exclude = [
153+
'\.undocumented_method$',
154+
'\.__repr__$',
155+
]
156+
157+
Overrides based on docstring contents are also supported, but the structure
158+
is slightly different than the :ref:`pre-commit hook setup <pre_commit_hook>`::
159+
160+
numpydoc_validation_overrides = {
161+
"SS02": [ # override SS05 to allow docstrings starting with these words
162+
'^Process ',
163+
'^Assess ',
164+
'^Access ',
165+
]
166+
}
167+
168+
.. _inline_ignore_comments:
169+
170+
Ignoring Validation Checks with Inline Comments
171+
-----------------------------------------------
172+
173+
Sometimes you only want to ignore a specific check or set of checks for a
174+
specific piece of code. This level of fine-tuned control is provided via
175+
inline comments:
176+
177+
.. code-block:: python
178+
179+
class SomeClass: # numpydoc ignore=EX01,SA01,ES01
180+
"""This is the docstring for SomeClass."""
181+
182+
def __init__(self): # numpydoc ignore=GL08
183+
pass
184+
185+
This is supported by the :ref:`CLI <validation_via_cli>`,
186+
:ref:`pre-commit hook <pre_commit_hook>`, and
187+
:ref:`Sphinx extension <validation_during_sphinx_build>`.
188+
128189
.. _validation_checks:
129190

130191
Built-in Validation Checks

0 commit comments

Comments
 (0)