2
2
Validation
3
3
==========
4
4
5
+ .. _pre_commit_hook :
6
+
5
7
Docstring Validation using Pre-Commit Hook
6
8
------------------------------------------
7
9
@@ -22,44 +24,55 @@ command line options for this hook:
22
24
23
25
$ python -m numpydoc.hooks.validate_docstrings --help
24
26
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 >`.
32
45
33
46
``pyproject.toml ``::
34
47
35
48
[tool.numpydoc_validation]
36
- ignore = [
49
+ checks = [
50
+ "all", # report on all checks, except the below
37
51
"EX01",
38
52
"SA01",
39
53
"ES01",
40
54
]
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
+ ]
43
64
44
65
``setup.cfg ``::
45
66
46
67
[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 ,
53
71
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.
58
74
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
63
76
commit is halted:
64
77
65
78
.. code-block :: output
@@ -77,7 +90,9 @@ commit is halted:
77
90
| src/pkg/module.py:33 | module.MyClass.parse | RT03 | Return value has no description |
78
91
+----------------------+----------------------+---------+--------------------------------------+
79
92
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 :
81
96
82
97
Docstring Validation using Python
83
98
---------------------------------
@@ -94,12 +109,16 @@ This will validate that the docstring can be built.
94
109
For an exhaustive validation of the formatting of the docstring, use the
95
110
``--validate `` parameter. This will report the errors detected, such as
96
111
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 :
98
117
99
118
Docstring Validation during Sphinx Build
100
119
----------------------------------------
101
120
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
103
122
process.
104
123
This behavior is controlled by the ``numpydoc_validation_checks `` configuration
105
124
parameter in ``conf.py ``.
@@ -109,7 +128,7 @@ following line to ``conf.py``::
109
128
110
129
numpydoc_validation_checks = {"PR01"}
111
130
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
113
132
that has undocumented parameters in the signature.
114
133
The full set of validation checks can be activated by::
115
134
@@ -125,6 +144,48 @@ special keyword ``"all"``::
125
144
# Report warnings for all validation checks except GL01, GL02, and GL05
126
145
numpydoc_validation_checks = {"all", "GL01", "GL02", "GL05"}
127
146
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
+
128
189
.. _validation_checks :
129
190
130
191
Built-in Validation Checks
0 commit comments