PyCQA / pydocstyle

docstring style checker
http://pydocstyle.org
MIT License
1.11k stars 189 forks source link

IndentationError on line continuations for numpydoc #437

Closed anntzer closed 4 years ago

anntzer commented 4 years ago

as of pydocstyle 5.0.1:

class T:

    def foo(self, x):
        """
        Does foo.

        Parameters
        ----------
        x : {'some', 'value', 'and', 'more', 'values', \
'numpydoc', 'says', 'they', 'have', 'to', 'fit', 'in', 'one', 'line'}
        """

analyzing with --convention=numpy results in

Traceback (most recent call last):
  File "/usr/bin/pydocstyle", line 11, in <module>
    load_entry_point('pydocstyle==5.0.1', 'console_scripts', 'pydocstyle')()
  File "/usr/lib/python3.8/site-packages/pydocstyle/cli.py", line 68, in main
    sys.exit(run_pydocstyle())
  File "/usr/lib/python3.8/site-packages/pydocstyle/cli.py", line 44, in run_pydocstyle
    errors.extend(check((filename,), select=checked_codes,
  File "/usr/lib/python3.8/site-packages/pydocstyle/checker.py", line 968, in check
    for error in ConventionChecker().check_source(source, filename,
  File "/usr/lib/python3.8/site-packages/pydocstyle/checker.py", line 123, in check_source
    for error in errors:
  File "/usr/lib/python3.8/site-packages/pydocstyle/checker.py", line 915, in check_docstring_sections
    found_numpy = yield from self._check_numpy_sections(lines, definition, docstring)
  File "/usr/lib/python3.8/site-packages/pydocstyle/checker.py", line 879, in _check_numpy_sections
    yield from self._check_numpy_section(docstring, definition, ctx)
  File "/usr/lib/python3.8/site-packages/pydocstyle/checker.py", line 693, in _check_numpy_section
    yield from cls._check_parameters_section(docstring, definition, context)
  File "/usr/lib/python3.8/site-packages/pydocstyle/checker.py", line 731, in _check_parameters_section
    yield from ConventionChecker._check_missing_args(docstring_args, definition)
  File "/usr/lib/python3.8/site-packages/pydocstyle/checker.py", line 762, in _check_missing_args
    function_args = get_function_args(definition.source)
  File "/usr/lib/python3.8/site-packages/pydocstyle/checker.py", line 1002, in get_function_args
    function_arg_node = ast.parse(textwrap.dedent(function_string)).body[0].args
  File "/usr/lib/python3.8/ast.py", line 47, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    def foo(self, x):
    ^
IndentationError: unexpected indent
kmantel commented 4 years ago

Sorry if it's redundant but here's some additional info I mistakenly attributed to another module:

Traceback (most recent call last):
  File "~/.pyenv/versions/pnl368/bin/pydocstyle", line 8, in <module>
    sys.exit(main())
  File "~/.pyenv/versions/3.6.8/envs/pnl368/lib/python3.6/site-packages/pydocstyle/cli.py", line 68, in main
    sys.exit(run_pydocstyle())
  File "~/.pyenv/versions/3.6.8/envs/pnl368/lib/python3.6/site-packages/pydocstyle/cli.py", line 45, in run_pydocstyle
    ignore_decorators=ignore_decorators))
  File "~/.pyenv/versions/3.6.8/envs/pnl368/lib/python3.6/site-packages/pydocstyle/checker.py", line 969, in check
    ignore_decorators):
  File "~/.pyenv/versions/3.6.8/envs/pnl368/lib/python3.6/site-packages/pydocstyle/checker.py", line 123, in check_source
    for error in errors:
  File "~/.pyenv/versions/3.6.8/envs/pnl368/lib/python3.6/site-packages/pydocstyle/checker.py", line 917, in check_docstring_sections
    yield from self._check_google_sections(lines, definition, docstring)
  File "~/.pyenv/versions/3.6.8/envs/pnl368/lib/python3.6/site-packages/pydocstyle/checker.py", line 903, in _check_google_sections
    yield from self._check_google_section(docstring, definition, ctx)
  File "~/.pyenv/versions/3.6.8/envs/pnl368/lib/python3.6/site-packages/pydocstyle/checker.py", line 796, in _check_google_section
    yield from cls._check_args_section(docstring, definition, context)
  File "~/.pyenv/versions/3.6.8/envs/pnl368/lib/python3.6/site-packages/pydocstyle/checker.py", line 748, in _check_args_section
    yield from ConventionChecker._check_missing_args(docstring_args, definition)
  File "~/.pyenv/versions/3.6.8/envs/pnl368/lib/python3.6/site-packages/pydocstyle/checker.py", line 762, in _check_missing_args
    function_args = get_function_args(definition.source)
  File "~/.pyenv/versions/3.6.8/envs/pnl368/lib/python3.6/site-packages/pydocstyle/checker.py", line 1002, in get_function_args
    function_arg_node = ast.parse(textwrap.dedent(function_string)).body[0].args
  File "~/.pyenv/versions/3.6.8/lib/python3.6/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1
    def bar(self):
    ^
IndentationError: unexpected indent

You can replicate it with a file containing

class Foo:

    def bar(self):
        '''

        Arguments
        ---------

        '''
        # a
# b

        pass

and you can avoid the error with several varying changes, like matching the indent of # b to # a, changing Arguments to something else (except Args), or removing the newlines in the docstring, which would then produce the expected docstyle error of D300: Use """triple double quotes""" (found '''-quotes)

sambhav commented 4 years ago

So the error happens because the docstrings are not indented properly. As a result textwrap.dedent can dedent the string. I am trying to look for a clean fix.

sambhav commented 4 years ago

Shoudl be fixed by #441