anntzer / defopt

Effortless argument parser
https://pypi.org/project/defopt/
MIT License
213 stars 11 forks source link

Exception for numpy "notes" sections #81

Closed kyleabeauchamp closed 4 years ago

kyleabeauchamp commented 4 years ago

I've noticed that certain numpy-style docstrings that contain either a Note or Notes sections cause an exception to occur during CLI parsing. In the following example (largely copied from https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_numpy.html), I've taken their PEP484 example and added a single Notes section from another example on that page. The exception is posted below. If you simply rename the Notes section to be called any other word, things appear to work fine.

#!/usr/bin/env python
import defopt

def run(param1: int, param2: str) -> bool:
    """Example function with PEP 484 type annotations.

    The return type must be duplicated in the docstring to comply
    with the NumPy docstring style.

    Parameters
    ----------
    param1
        The first parameter.
    param2
        The second parameter.

    Returns
    -------
    bool
        True if successful, False otherwise.

    Notes
    -----
    Do not include the `self` parameter in the ``Parameters`` section.

    """

if __name__ == "__main__":
    defopt.run(run)
$ ./test_defopt.py -h
Traceback (most recent call last):
  File "./test_defopt.py", line 31, in <module>
    defopt.run(run)
  File "~/miniconda3/envs/testenv/lib/python3.7/site-packages/defopt.py", line 96, in run
    parser = _create_parser(funcs, **kwargs)
  File "~/miniconda3/envs/testenv/lib/python3.7/site-packages/defopt.py", line 129, in _create_parser
    _populate_parser(funcs, parser, parsers, short, strict_kwonly)
  File "~/miniconda3/envs/testenv/lib/python3.7/site-packages/defopt.py", line 179, in _populate_parser
    doc = _parse_function_docstring(func)
  File "~/miniconda3/envs/testenv/lib/python3.7/site-packages/defopt.py", line 362, in _parse_function_docstring
    return _parse_docstring(inspect.getdoc(func))
  File "~/miniconda3/envs/testenv/lib/python3.7/site-packages/defopt.py", line 514, in _parse_docstring
    tree.walkabout(visitor)
  File "~/miniconda3/envs/testenv/lib/python3.7/site-packages/docutils/nodes.py", line 178, in walkabout
    if child.walkabout(visitor):
  File "~/miniconda3/envs/testenv/lib/python3.7/site-packages/docutils/nodes.py", line 170, in walkabout
    visitor.dispatch_visit(self)
  File "~/miniconda3/envs/testenv/lib/python3.7/site-packages/docutils/nodes.py", line 1912, in dispatch_visit
    return method(node)
  File "~/miniconda3/envs/testenv/lib/python3.7/site-packages/docutils/nodes.py", line 1937, in unknown_visit
    % (self.__class__, node.__class__.__name__))
NotImplementedError: <class 'defopt._parse_docstring.<locals>.Visitor'> visiting unknown node type: rubric
anntzer commented 4 years ago

I assume this you are on defopt<6; this was fixed in the very recently released defopt 6. (The help string is formatted not-so-optimally because napoleon introduces a lot of blank lines and we just copy them verbatim; I guess it should not be too hard to strip extra blank lines out ourselves.)

kyleabeauchamp commented 4 years ago

Upgrading from 5.1.0 to 6.0 fixes the issue, thank you!