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

Commit

Permalink
Fix IndentationError while parsing function (#392)
Browse files Browse the repository at this point in the history
* Fix IndentationError while parsing function

* Changelog

* Test for #370

* Fixed D400
  • Loading branch information
sfstpala authored and Nurdok committed Aug 12, 2019
1 parent 49183a3 commit 0e9d708
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Bug Fixes
e.g., init and initialize / initiate (#382).
* Fix parser hanging when there's a comment directly after ``__all__``
(#391, #366).
* Fixed IndentationError when parsing function arguments (#392).

4.0.0 - July 6th, 2019
---------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/pydocstyle/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ast
import string
import sys
import textwrap
import tokenize as tk
from itertools import takewhile, chain
from re import compile as re
Expand Down Expand Up @@ -917,7 +918,7 @@ def get_leading_words(line):

def get_function_args(function_string):
"""Return the function arguments given the source-code string."""
function_arg_node = ast.parse(function_string).body[0].args
function_arg_node = ast.parse(textwrap.dedent(function_string)).body[0].args
arg_nodes = function_arg_node.args
kwonly_arg_nodes = function_arg_node.kwonlyargs
return set(arg_node.arg for arg_node in chain(arg_nodes, kwonly_arg_nodes))
19 changes: 19 additions & 0 deletions src/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,3 +1107,22 @@ def test_syntax_error_multiple_files(env):
assert code == 1
assert 'first.py: Cannot parse file' in err
assert 'second.py: Cannot parse file' in err


def test_indented_function(env):
"""Test that nested functions do not cause IndentationError."""
env.write_config(ignore='D')
with env.open("test.py", 'wt') as fobj:
fobj.write(textwrap.dedent('''\
def foo():
def bar(a):
"""A docstring
Args:
a : An argument.
"""
pass
'''))
out, err, code = env.invoke(args="-v")
assert code == 0
assert "IndentationError: unexpected indent" not in err

0 comments on commit 0e9d708

Please sign in to comment.