Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #7868: Added lint check for a newline above args in python doc string #7929

Merged
merged 5 commits into from
Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added more tests
  • Loading branch information
Hudda committed Nov 6, 2019
commit 83a2d38cbed840265d61ab4bfc45aa5345a78e6f
21 changes: 18 additions & 3 deletions scripts/pylint_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,17 @@ class SingleNewlineAboveArgsChecker(checkers.BaseChecker):
'C0012': (
'Files must have a single newline above args in doc string.',
'single-space-above-args',
'Please enter a single newline above doc string.'
'Please enter a single newline above args in doc string.'
),
'C0013': (
'Files must have a single newline above returns in doc string.',
'single-space-above-returns',
'Please enter a single newline above returns in doc string.'
),
'C0014': (
'Files must have a single newline above raises in doc string.',
'single-space-above-raises',
'Please enter a single newline above raises in doc string.'
)
}

Expand Down Expand Up @@ -1075,10 +1085,15 @@ def process_module(self, node):
if (line_num + 1 < file_length and (
blank_line_counter == 0 or blank_line_counter > 1)):
line = file_content[line_num + 1].strip()
if (len(line.split()) == 1 and
re.match(br'^[A-Z][a-z]+[:]', line)):
if line == b'Args:':
self.add_message(
'single-space-above-args', line=line_num + 1)
elif line == b'Returns:':
self.add_message(
'single-space-above-returns', line=line_num + 1)
elif line == b'Raises:':
self.add_message(
'single-space-above-raises', line=line_num + 1)


def register(linter):
Expand Down
88 changes: 85 additions & 3 deletions scripts/pylint_extensions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ def test_checks_newline_above_docstring(self):
line=2
),
testutils.Message(
msg_id='single-space-above-args',
msg_id='single-space-above-returns',
line=4
),
):
Expand Down Expand Up @@ -1221,12 +1221,70 @@ def test_checks_newline_above_docstring(self):
line=4
),
testutils.Message(
msg_id='single-space-above-args',
msg_id='single-space-above-returns',
line=8
),
):
temp_file.close()

node_with_no_space_above_return = astroid.scoped_nodes.Module(
name='test',
doc='Custom test')
temp_file = tempfile.NamedTemporaryFile()
filename = temp_file.name

with python_utils.open_file(filename, 'w') as tmp:
tmp.write(
u"""def func():
'''Returns something.
Returns:
returns_something
'''
returns_something
""")
node_with_no_space_above_return.file = filename
node_with_no_space_above_return.path = filename

checker_test_object.checker.process_module(
node_with_no_space_above_return)

with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='single-space-above-returns',
line=2
),
):
temp_file.close()

node_with_no_space_above_raises = astroid.scoped_nodes.Module(
name='test',
doc='Custom test')
temp_file = tempfile.NamedTemporaryFile()
filename = temp_file.name

with python_utils.open_file(filename, 'w') as tmp:
tmp.write(
u"""def func():
'''Raises exception.
Raises:
raises_exception
'''
raises_exception
""")
node_with_no_space_above_raises.file = filename
node_with_no_space_above_raises.path = filename

checker_test_object.checker.process_module(
node_with_no_space_above_raises)

with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='single-space-above-raises',
line=2
),
):
temp_file.close()

node_with_return_in_comment = astroid.scoped_nodes.Module(
name='test',
doc='Custom test')
Expand Down Expand Up @@ -1256,6 +1314,27 @@ def test_checks_newline_above_docstring(self):
with checker_test_object.assertNoMessages():
temp_file.close()

node_with_no_args = astroid.scoped_nodes.Module(
name='test',
doc='Custom test')
temp_file = tempfile.NamedTemporaryFile()
filename = temp_file.name
with python_utils.open_file(filename, 'w') as tmp:
tmp.write(
u"""def func():
'''Do something.'''

do something
""")
node_with_no_args.file = filename
node_with_no_args.path = filename

checker_test_object.checker.process_module(
node_with_no_args)

with checker_test_object.assertNoMessages():
temp_file.close()

node_with_no_error_message = astroid.scoped_nodes.Module(
name='test',
doc='Custom test')
Expand All @@ -1272,8 +1351,11 @@ def test_checks_newline_above_docstring(self):

Returns:
returns_something
'''

Raises:
raises something
'''
raises_something
returns_something
""")
node_with_no_error_message.file = filename
Expand Down