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

Commit d85735e

Browse files
theyuvalrazsambhav
andauthored
Adding tests for parsing one line functions/ Fixing parser bug. (#499)
* 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 <[email protected]>
1 parent 5cbac26 commit d85735e

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

src/pydocstyle/parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,9 @@ def parse_definition(self, class_):
683683
else: # one-liner definition
684684
skipped_error_codes = ''
685685
docstring = self.parse_docstring()
686-
decorators = [] # TODO
686+
decorators = self._accumulated_decorators
687+
self.log.debug("current accumulated decorators: %s", decorators)
688+
self._accumulated_decorators = []
687689
children = []
688690
end = self.line
689691
self.leapfrog(tk.NEWLINE)

src/tests/parser_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,19 @@ def test():
880880
def test():
881881
pass
882882
"""),
883+
CodeSnippet("""\
884+
'''Test this'''
885+
886+
@property
887+
def test(): pass
888+
"""),
889+
CodeSnippet("""\
890+
'''Test this'''
891+
892+
@first_decorator
893+
@property
894+
def test(): pass
895+
"""),
883896
))
884897
def test_parsing_function_decorators(code):
885898
"""Test to ensure we are correctly parsing function decorators."""

src/tests/test_cases/test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,33 @@ def oneliner_d102(): return
337337
def oneliner_withdoc(): """One liner"""
338338

339339

340+
def ignored_decorator(func): # noqa: D400,D401,D415
341+
"""Runs something"""
342+
func()
343+
pass
344+
345+
346+
def decorator_for_test(func): # noqa: D400,D401,D415
347+
"""Runs something"""
348+
func()
349+
pass
350+
351+
352+
@ignored_decorator
353+
def oneliner_ignored_decorator(): """One liner"""
354+
355+
356+
@decorator_for_test
357+
@expect("D400: First line should end with a period (not 'r')")
358+
@expect("D415: First line should end with a period, question mark,"
359+
" or exclamation point (not 'r')")
360+
def oneliner_with_decorator_expecting_errors(): """One liner"""
361+
362+
363+
@decorator_for_test
364+
def valid_oneliner_with_decorator(): """One liner."""
365+
366+
340367
@expect("D207: Docstring is under-indented")
341368
@expect('D213: Multi-line docstring summary should start at the second line')
342369
def docstring_start_in_same_line(): """First Line.

src/tests/test_definitions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def test_complex_file(test_case):
3737
test_case + '.py')
3838
results = list(check([test_case_file],
3939
select=set(ErrorRegistry.get_error_codes()),
40-
ignore_decorators=re.compile('wraps')))
40+
ignore_decorators=re.compile(
41+
'wraps|ignored_decorator')))
4142
for error in results:
4243
assert isinstance(error, Error)
4344
results = {(e.definition.name, e.message) for e in results}

0 commit comments

Comments
 (0)