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 lint check for newline above args
  • Loading branch information
Hudda committed Nov 7, 2019
commit 484c110dab422244c3d79de7c5e19bd84514ecb9
6 changes: 3 additions & 3 deletions scripts/pylint_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ class SingleNewlineAboveArgsChecker(checkers.BaseChecker):
"""Checker for single space above args in python doc string."""

__implements__ = interfaces.IRawChecker
name = 'single-space-above-args'
name = 'single-space-above-args-raises-returns'
priority = -1
msgs = {
'C0012': (
Expand All @@ -1044,8 +1044,8 @@ class SingleNewlineAboveArgsChecker(checkers.BaseChecker):
}

def process_module(self, node):
"""Process a module to ensure that there is a single newline above args
in python doc string.
"""Process a module to ensure that there is a single newline above args,
raises, returns in python doc string.

Args:
node: astroid.scoped_nodes.Function. Node to access module content.
Expand Down
167 changes: 130 additions & 37 deletions scripts/pylint_extensions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,14 +1161,11 @@ def test_checks_newline_above_docstring(self):
with python_utils.open_file(filename, 'w') as tmp:
tmp.write(
u"""def func(arg):
'''Returns something.
'''Do something.
Args:
arg: argument
Returns:
returns_something
'''

return returns_something
do something
""")
node_single_newline_above_args.file = filename
node_single_newline_above_args.path = filename
Expand All @@ -1181,48 +1178,34 @@ def test_checks_newline_above_docstring(self):
msg_id='single-space-above-args',
line=2
),
testutils.Message(
msg_id='single-space-above-returns',
line=4
),
):
temp_file.close()

node_with_two_newline = astroid.scoped_nodes.Module(
node_single_newline_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(arg):
'''Returns something.


Args:
arg: argument


Returns:
returns_something
u"""def func():
'''Raises exception.
Raises:
raises_exception
'''
returns something
raises_exception
""")
node_with_two_newline.file = filename
node_with_two_newline.path = filename
node_single_newline_above_raises.file = filename
node_single_newline_above_raises.path = filename

checker_test_object.checker.process_module(
node_with_two_newline)
node_single_newline_above_raises)

with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='single-space-above-args',
line=4
),
testutils.Message(
msg_id='single-space-above-returns',
line=8
msg_id='single-space-above-raises',
line=2
),
):
temp_file.close()
Expand Down Expand Up @@ -1256,31 +1239,141 @@ def test_checks_newline_above_docstring(self):
):
temp_file.close()

node_with_no_space_above_raises = astroid.scoped_nodes.Module(
node_newline_above_args_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():
u"""def func(arg):
'''Raises exception.

Args:
arg: argument
Raises:
raises_something
'''
raises_exception
""")
node_newline_above_args_raises.file = filename
node_newline_above_args_raises.path = filename

checker_test_object.checker.process_module(
node_newline_above_args_raises)

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

node_newline_above_args_returns = 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(arg):
'''Returns Something.

Args:
arg: argument
Returns:
returns_something
'''
returns_something
""")
node_newline_above_args_returns.file = filename
node_newline_above_args_returns.path = filename

checker_test_object.checker.process_module(
node_newline_above_args_returns)

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

node_newline_above_returns_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():
'''Do something.



Raises:
raises_exception

Returns:
returns_something
'''
raises_exception
returns_something
""")
node_with_no_space_above_raises.file = filename
node_with_no_space_above_raises.path = filename
node_newline_above_returns_raises.file = filename
node_newline_above_returns_raises.path = filename

checker_test_object.checker.process_module(
node_with_no_space_above_raises)
node_newline_above_returns_raises)

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

node_with_two_newline = 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(arg):
'''Returns something.


Args:
arg: argument


Returns:
returns_something
'''
returns something
""")
node_with_two_newline.file = filename
node_with_two_newline.path = filename

checker_test_object.checker.process_module(
node_with_two_newline)

with checker_test_object.assertAddsMessages(
testutils.Message(
msg_id='single-space-above-args',
line=4
),
testutils.Message(
msg_id='single-space-above-returns',
line=8
),
):
temp_file.close()
Expand Down