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

Commit

Permalink
Bugfix: Allow comments before module docstring noqa codes (#446)
Browse files Browse the repository at this point in the history
* Allow comments before module docstring noqa codes

* Add a note to the release notes

* Break after first `noqa` is encountered

The behavior is now the same as on master.

* added integration tests for comment files

* fixed docstring in test_comment_plus_docstring_file

* added integration tests with noqa

Co-authored-by: Cielquan <cielquan@protonmail.com>
Co-authored-by: Christian Riedel <cannotguessit+github@protonmail.com>
Co-authored-by: Sambhav Kothari <sambhavs.email@gmail.com>
  • Loading branch information
4 people committed Jul 15, 2020
1 parent dd5ab9e commit 2dfbb38
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/release_notes.rst
Expand Up @@ -18,6 +18,8 @@ Bug Fixes
* Fix indentation error while parsing class methods (#441).
* Fix a bug in parsing Google-style argument description.
The bug caused some argument names to go unreported in D417 (#448).
* Fixed an issue where skipping errors on module level docstring via #noqa
failed when there where more prior comments (#446).


5.0.2 - January 8th, 2020
Expand Down
20 changes: 14 additions & 6 deletions src/pydocstyle/parser.py
Expand Up @@ -584,12 +584,20 @@ def parse_definition(self, class_):
def parse_skip_comment(self):
"""Parse a definition comment for noqa skips."""
skipped_error_codes = ''
if self.current.kind == tk.COMMENT:
if 'noqa: ' in self.current.value:
skipped_error_codes = ''.join(
self.current.value.split('noqa: ')[1:])
elif self.current.value.startswith('# noqa'):
skipped_error_codes = 'all'
while self.current.kind in (tk.COMMENT, tk.NEWLINE, tk.NL):
if self.current.kind == tk.COMMENT:
if 'noqa: ' in self.current.value:
skipped_error_codes = ''.join(
self.current.value.split('noqa: ')[1:])
elif self.current.value.startswith('# noqa'):
skipped_error_codes = 'all'
self.stream.move()
self.log.debug("parsing comments before docstring, token is %r (%s)",
self.current.kind, self.current.value)

if skipped_error_codes:
break

return skipped_error_codes

def check_current(self, kind=None, value=None):
Expand Down
1 change: 1 addition & 0 deletions src/tests/test_cases/noqa.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# noqa: D400,D415
"""Test case for "# noqa" comments"""
from .expected import Expectation
Expand Down
67 changes: 67 additions & 0 deletions src/tests/test_integration.py
Expand Up @@ -1123,3 +1123,70 @@ def bar(a):
out, err, code = env.invoke(args="-v")
assert code == 0
assert "IndentationError: unexpected indent" not in err


def test_only_comment_file(env):
"""Test that file with only comments does only cause D100."""
with env.open('comments.py', 'wt') as comments:
comments.write(
'#!/usr/bin/env python3\n'
'# -*- coding: utf-8 -*-\n'
'# Useless comment\n'
'# Just another useless comment\n'
)

out, _, code = env.invoke()
assert 'D100' in out
out = out.replace('D100', '')
for err in {'D1', 'D2', 'D3', 'D4'}:
assert err not in out
assert code == 1


def test_comment_plus_docstring_file(env):
"""Test that file with comments and docstring does not cause errors."""
with env.open('comments_plus.py', 'wt') as comments_plus:
comments_plus.write(
'#!/usr/bin/env python3\n'
'# -*- coding: utf-8 -*-\n'
'# Useless comment\n'
'# Just another useless comment\n'
'"""Module docstring."""\n'
)

out, _, code = env.invoke()
assert '' == out
assert code == 0


def test_only_comment_with_noqa_file(env):
"""Test that file with noqa and only comments does not cause errors."""
with env.open('comments.py', 'wt') as comments:
comments.write(
'#!/usr/bin/env python3\n'
'# -*- coding: utf-8 -*-\n'
'# Useless comment\n'
'# Just another useless comment\n'
'# noqa: D100\n'
)

out, _, code = env.invoke()
assert 'D100' not in out
assert code == 0


def test_comment_with_noqa_plus_docstring_file(env):
"""Test that file with comments, noqa, docstring does not cause errors."""
with env.open('comments_plus.py', 'wt') as comments_plus:
comments_plus.write(
'#!/usr/bin/env python3\n'
'# -*- coding: utf-8 -*-\n'
'# Useless comment\n'
'# Just another useless comment\n'
'# noqa: D400\n'
'"""Module docstring without period"""\n'
)

out, _, code = env.invoke()
assert '' == out
assert code == 0

0 comments on commit 2dfbb38

Please sign in to comment.