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

Commit

Permalink
Adding tests for parsing one line functions/ Fixing parser bug. (#499)
Browse files Browse the repository at this point in the history
* Added tests for parsing "One line functions"
The tests look valid but don't pass.

from debugging looks like the parser cant find the decorator, although it has no problem finding the function and accepting it as valid.

* Fix parser.py to work on one line functions.

* PEP8 fixes.

* Adding a test for multiple decorators over a "single line function".

* Added tests for single line function with decorator.
1. oneliner_with_decorator_expecting_errors(): The decorator is not ignored, expecting the D400, D415 errors.
2. valid_oneliner_with_decorator(): A valid one line function that shouldn't produce any errors.

* Added tests for single line function with decorator.
1. oneliner_with_decorator_expecting_errors(): The decorator is not ignored, expecting the D400, D415 errors.
2. valid_oneliner_with_decorator(): A valid one line function that shouldn't produce any errors.

Co-authored-by: Sambhav Kothari <sambhavs.email@gmail.com>
  • Loading branch information
theyuvalraz and samj1912 committed Sep 5, 2020
1 parent 5cbac26 commit d85735e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/pydocstyle/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,9 @@ def parse_definition(self, class_):
else: # one-liner definition
skipped_error_codes = ''
docstring = self.parse_docstring()
decorators = [] # TODO
decorators = self._accumulated_decorators
self.log.debug("current accumulated decorators: %s", decorators)
self._accumulated_decorators = []
children = []
end = self.line
self.leapfrog(tk.NEWLINE)
Expand Down
13 changes: 13 additions & 0 deletions src/tests/parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,19 @@ def test():
def test():
pass
"""),
CodeSnippet("""\
'''Test this'''
@property
def test(): pass
"""),
CodeSnippet("""\
'''Test this'''
@first_decorator
@property
def test(): pass
"""),
))
def test_parsing_function_decorators(code):
"""Test to ensure we are correctly parsing function decorators."""
Expand Down
27 changes: 27 additions & 0 deletions src/tests/test_cases/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,33 @@ def oneliner_d102(): return
def oneliner_withdoc(): """One liner"""


def ignored_decorator(func): # noqa: D400,D401,D415
"""Runs something"""
func()
pass


def decorator_for_test(func): # noqa: D400,D401,D415
"""Runs something"""
func()
pass


@ignored_decorator
def oneliner_ignored_decorator(): """One liner"""


@decorator_for_test
@expect("D400: First line should end with a period (not 'r')")
@expect("D415: First line should end with a period, question mark,"
" or exclamation point (not 'r')")
def oneliner_with_decorator_expecting_errors(): """One liner"""


@decorator_for_test
def valid_oneliner_with_decorator(): """One liner."""


@expect("D207: Docstring is under-indented")
@expect('D213: Multi-line docstring summary should start at the second line')
def docstring_start_in_same_line(): """First Line.
Expand Down
3 changes: 2 additions & 1 deletion src/tests/test_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def test_complex_file(test_case):
test_case + '.py')
results = list(check([test_case_file],
select=set(ErrorRegistry.get_error_codes()),
ignore_decorators=re.compile('wraps')))
ignore_decorators=re.compile(
'wraps|ignored_decorator')))
for error in results:
assert isinstance(error, Error)
results = {(e.definition.name, e.message) for e in results}
Expand Down

0 comments on commit d85735e

Please sign in to comment.