|
2 | 2 | Validation
|
3 | 3 | ==========
|
4 | 4 |
|
| 5 | +Docstring Validation using Pre-Commit Hook |
| 6 | +------------------------------------------ |
| 7 | + |
| 8 | +To enable validation of docstrings as you commit files, add the |
| 9 | +following to your ``.pre-commit-config.yaml`` file: |
| 10 | + |
| 11 | +.. code-block:: yaml |
| 12 | +
|
| 13 | + - repo: https://github.com/numpy/numpydoc |
| 14 | + rev: <version> |
| 15 | + hooks: |
| 16 | + - id: numpydoc-validation |
| 17 | +
|
| 18 | +After installing ``numpydoc``, run the following to see available |
| 19 | +command line options for this hook: |
| 20 | + |
| 21 | +.. code-block:: bash |
| 22 | +
|
| 23 | + $ python -m numpydoc.hooks.validate_docstrings --help |
| 24 | +
|
| 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). |
| 32 | + |
| 33 | +``pyproject.toml``:: |
| 34 | + |
| 35 | + [tool.numpydoc_validation] |
| 36 | + ignore = [ |
| 37 | + "EX01", |
| 38 | + "SA01", |
| 39 | + "ES01", |
| 40 | + ] |
| 41 | + override_SS05 = '^((Process|Assess|Access) )' |
| 42 | + override_GL08 = '^(__init__)$' |
| 43 | + |
| 44 | +``setup.cfg``:: |
| 45 | + |
| 46 | + [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: |
| 53 | + |
| 54 | +.. code-block:: python |
| 55 | +
|
| 56 | + class SomeClass: # numpydoc ignore=EX01,SA01,ES01 |
| 57 | + """This is the docstring for SomeClass.""" |
| 58 | +
|
| 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 |
| 63 | +commit is halted: |
| 64 | + |
| 65 | +.. code-block:: output |
| 66 | +
|
| 67 | + numpydoc-validation......................................................Failed |
| 68 | + - hook id: numpydoc-validation |
| 69 | + - exit code: 1 |
| 70 | +
|
| 71 | + +----------------------+----------------------+---------+--------------------------------------+ |
| 72 | + | file | item | check | description | |
| 73 | + +======================+======================+=========+======================================+ |
| 74 | + | src/pkg/utils.py:1 | utils | GL08 | The object does not have a docstring | |
| 75 | + | src/pkg/utils.py:90 | utils.normalize | PR04 | Parameter "field" has no type | |
| 76 | + | src/pkg/module.py:12 | module.MyClass | GL08 | The object does not have a docstring | |
| 77 | + | src/pkg/module.py:33 | module.MyClass.parse | RT03 | Return value has no description | |
| 78 | + +----------------------+----------------------+---------+--------------------------------------+ |
| 79 | +
|
| 80 | +See below for a full listing of checks. |
| 81 | + |
5 | 82 | Docstring Validation using Python
|
6 | 83 | ---------------------------------
|
7 | 84 |
|
|
0 commit comments