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

LineContinuationBear: Add ignore_with option #2844

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions bears/general/LineContinuationBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def run(self,
filename,
file,
language,
ignore_with: bool = False,
):
"""
It is a generic bear which will look for any lines which
Expand All @@ -27,6 +28,8 @@ def run(self,
File that needs to be checked in the form of a list of strings.
:param language:
Language to be used for finding the line continuation tokens.
:param ignore_with:
Ignore line continuation operators in with statements.
"""
try:
lang = Language[language]
Expand All @@ -36,7 +39,7 @@ def run(self,
return

for line_number, line in enumerate(file):
if len(line) > 1:
if len(line) > 1 and not ignore_with:
if line[-2] in line_continuation:
yield Result.from_values(
origin=self,
Expand All @@ -46,4 +49,15 @@ def run(self,
column=len(line) - 1,
end_line=line_number + 1,
end_column=len(line),
)
)
elif len(line) > 1 and ignore_with:
if line[-2] in line_continuation and line[:5] != 'with ':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line[:5] assumes the position of with in the source line, which is not acceptable as with can appear at any indentation.

yield Result.from_values(
origin=self,
message='Explicit line continuation is not allowed.',
file=filename,
line=line_number + 1,
column=len(line) - 1,
end_line=line_number + 1,
end_column=len(line),
)
22 changes: 22 additions & 0 deletions tests/general/LineContinuationBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ def test_bad_file(self):
line=1, column=9, end_line=1, end_column=10,
file='default')],
filename='default')
self.check_results(
self.uut, ["with open ('hey.txt') as \\",
'heyfile'],
[Result.from_values('LineContinuationBear',
'Explicit line continuation is not allowed.',
line=1, column=26, end_line=1, end_column=27,
file='default')],
filename='default')

def test_data_ignore_with(self):
self.section.append(Setting('language', 'Python'))
self.section.append(Setting('ignore_with', 'true'))
self.check_results(
self.uut, ['a = 1 + \\', '2'],
[Result.from_values('LineContinuationBear',
'Explicit line continuation is not allowed.',
line=1, column=9, end_line=1, end_column=10,
file='default')],
filename='default')
self.check_validity(
self.uut, ["with open('hey.txt') as \\",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is invalid syntax. Please use valid syntax in the python you use for your test cases

'heyfile'])

def test_lang_exception(self):
self.section.append(Setting('language', 'BlaBlaBla'))
Expand Down