Skip to content

Commit

Permalink
LineContinuationBear: Add ignore_with option
Browse files Browse the repository at this point in the history
Add `ignore_with` option in LineContinuationBear to ignore
backslashes used in `with` statements.

Closes #2807
  • Loading branch information
rajgoesout authored and gitmate-bot committed Feb 4, 2019
1 parent 5b6de3e commit c377407
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
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 ':
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 \\",
'heyfile'])

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

0 comments on commit c377407

Please sign in to comment.