Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

IndentationError on line continuations for numpydoc #437

Closed
anntzer opened this issue Dec 11, 2019 · 3 comments · Fixed by #441
Closed

IndentationError on line continuations for numpydoc #437

anntzer opened this issue Dec 11, 2019 · 3 comments · Fixed by #441

Comments

@anntzer
Copy link
Contributor

anntzer commented Dec 11, 2019

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
Copy link

kmantel commented Dec 12, 2019

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)

@samj1912
Copy link
Member

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.

@samj1912
Copy link
Member

samj1912 commented Dec 15, 2019

Shoudl be fixed by #441

samj1912 added a commit to samj1912/pydocstyle that referenced this issue May 8, 2020
In order to determine the missing arguments, we currently
use ast.parse to parse partial source code. This parsing
might lead to syntax errors. We catch the syntax error and
make the parsing more resilient to errors in the source code.

Fixes PyCQA#437
samj1912 added a commit that referenced this issue Jul 8, 2020
In order to determine the missing arguments, we currently
use ast.parse to parse partial source code. This parsing
might lead to syntax errors. We catch the syntax error and
make the parsing more resilient to errors in the source code.

Fixes #437
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants