22Validation
33==========
44
5+ .. _pre_commit_hook :
6+
57Docstring 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
6376commit 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
8297Docstring Validation using Python
8398---------------------------------
@@ -94,12 +109,16 @@ This will validate that the docstring can be built.
94109For an exhaustive validation of the formatting of the docstring, use the
95110``--validate `` parameter. This will report the errors detected, such as
96111incorrect 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
99118Docstring 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
103122process.
104123This behavior is controlled by the ``numpydoc_validation_checks `` configuration
105124parameter 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
113132that has undocumented parameters in the signature.
114133The 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
130191Built-in Validation Checks
0 commit comments