From 4d1384e007ae29eddf62e02a33f361312fe76468 Mon Sep 17 00:00:00 2001 From: Yuval Raz Date: Sat, 22 Aug 2020 03:20:24 +0300 Subject: [PATCH 1/6] 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. --- src/tests/parser_test.py | 6 ++++++ src/tests/test_cases/test.py | 10 ++++++++++ src/tests/test_definitions.py | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/tests/parser_test.py b/src/tests/parser_test.py index 832d1b52..668945f4 100644 --- a/src/tests/parser_test.py +++ b/src/tests/parser_test.py @@ -831,6 +831,12 @@ def test(): def test(): pass """), + CodeSnippet("""\ + '''Test this''' + + @property + def test(): pass + """), )) def test_parsing_function_decorators(code): """Test to ensure we are correctly parsing function decorators.""" diff --git a/src/tests/test_cases/test.py b/src/tests/test_cases/test.py index 20b81fce..bd9e5a6b 100644 --- a/src/tests/test_cases/test.py +++ b/src/tests/test_cases/test.py @@ -348,6 +348,16 @@ def oneliner_d102(): return def oneliner_withdoc(): """One liner""" +def ignored_decorator(func): # noqa: D400,D401,D415 + """Runs something""" + func() + pass + + +@ignored_decorator +def oneliner_ignored_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. diff --git a/src/tests/test_definitions.py b/src/tests/test_definitions.py index 83109b5a..8578d0fe 100644 --- a/src/tests/test_definitions.py +++ b/src/tests/test_definitions.py @@ -37,7 +37,7 @@ 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} From 233a996b9056960042b7f369a44dd2034dad6425 Mon Sep 17 00:00:00 2001 From: Yuval Raz Date: Sun, 23 Aug 2020 08:49:47 +0300 Subject: [PATCH 2/6] Fix parser.py to work on one line functions. --- src/pydocstyle/parser.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pydocstyle/parser.py b/src/pydocstyle/parser.py index 7ffef855..797b4c1e 100644 --- a/src/pydocstyle/parser.py +++ b/src/pydocstyle/parser.py @@ -568,7 +568,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) From ca3f7dee1c9e211020e9ca7ddc6bca9e092dc6ff Mon Sep 17 00:00:00 2001 From: Yuval Raz Date: Sun, 23 Aug 2020 14:47:04 +0300 Subject: [PATCH 3/6] PEP8 fixes. --- src/tests/parser_test.py | 2 +- src/tests/test_definitions.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/tests/parser_test.py b/src/tests/parser_test.py index 668945f4..043fa90a 100644 --- a/src/tests/parser_test.py +++ b/src/tests/parser_test.py @@ -833,7 +833,7 @@ def test(): """), CodeSnippet("""\ '''Test this''' - + @property def test(): pass """), diff --git a/src/tests/test_definitions.py b/src/tests/test_definitions.py index 8578d0fe..93407cd3 100644 --- a/src/tests/test_definitions.py +++ b/src/tests/test_definitions.py @@ -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|ignored_decorator'))) + 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} From cb98e4ac60ff0e211e57c26ba9d40d14af14f929 Mon Sep 17 00:00:00 2001 From: Yuval Raz Date: Mon, 31 Aug 2020 16:46:01 +0300 Subject: [PATCH 4/6] Adding a test for multiple decorators over a "single line function". --- src/tests/parser_test.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tests/parser_test.py b/src/tests/parser_test.py index 2360f9a9..1ba96308 100644 --- a/src/tests/parser_test.py +++ b/src/tests/parser_test.py @@ -894,6 +894,13 @@ def test(): @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.""" From 01adb2ef454a197fdd3ff2de140717d56f134813 Mon Sep 17 00:00:00 2001 From: Yuval Raz Date: Fri, 4 Sep 2020 04:47:44 +0300 Subject: [PATCH 5/6] 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. --- src/tests/test_cases/test.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/tests/test_cases/test.py b/src/tests/test_cases/test.py index 47dbd94b..672020ef 100644 --- a/src/tests/test_cases/test.py +++ b/src/tests/test_cases/test.py @@ -343,10 +343,26 @@ def ignored_decorator(func): # noqa: D400,D401,D415 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. From 2bba27e28e1a24730f94653dd63e49efad74a387 Mon Sep 17 00:00:00 2001 From: Yuval Raz Date: Fri, 4 Sep 2020 04:53:59 +0300 Subject: [PATCH 6/6] 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. --- src/tests/test_cases/test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tests/test_cases/test.py b/src/tests/test_cases/test.py index 672020ef..1072b81d 100644 --- a/src/tests/test_cases/test.py +++ b/src/tests/test_cases/test.py @@ -355,7 +355,8 @@ 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')") +@expect("D415: First line should end with a period, question mark," + " or exclamation point (not 'r')") def oneliner_with_decorator_expecting_errors(): """One liner"""