-
Notifications
You must be signed in to change notification settings - Fork 581
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
SpaceConsistencyBear: Add blanklines_at_EOF option #2173
Conversation
|
||
if no_blanklines: | ||
if enforce_newline_at_EOF: | ||
# Since every line contains at the end at least one \n, only |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E501 line too long (80 > 79 characters)
Origin: PycodestyleBear (E501), Section: autopep8
.
replacement += '\n' | ||
result_texts.append('No newline at EOF.') | ||
additional_info_texts.append( | ||
"A trailing newline character ('\\n') is missing from " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E501 line too long (83 > 79 characters)
Origin: PycodestyleBear (E501), Section: autopep8
.
additional_info_texts.append( | ||
"A trailing newline character ('\\n') is missing from " | ||
'your file. ' | ||
'<http://stackoverflow.com/a/5813359/3212182> gives ' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E501 line too long (81 > 79 characters)
Origin: PycodestyleBear (E501), Section: autopep8
.
@@ -70,3 +70,21 @@ def test_enforce_newline_at_eof(self): | |||
" print('funny')\n", | |||
" print('the result is not funny...')"], | |||
force_linebreaks=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code does not comply to PEP8.
Origin: PEP8Bear, Section: autopep8
.
The issue can be fixed by applying the following patch:
--- a/tmp/tmp40ntr2v2/tests/general/SpaceConsistencyBearTest.py
+++ b/tmp/tmp40ntr2v2/tests/general/SpaceConsistencyBearTest.py
@@ -70,6 +70,7 @@
" print('funny')\n",
" print('the result is not funny...')"],
force_linebreaks=False)
+
def test_trailing_blanklines(self):
self.section.append(Setting('use_spaces', 'true'))
self.section.append(Setting('allow_trailing_whitespace', 'false'))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the end of a class right? thats why PEP8Bear wants an additional blank line
self.section.append(Setting('allow_trailing_blanklines', 'False')) | ||
|
||
self.check_invalidity(self.uut, | ||
['def funny_code():\n', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code does not comply to PEP8.
Origin: PEP8Bear, Section: autopep8
.
The issue can be fixed by applying the following patch:
--- a/tmp/tmp40ntr2v2/tests/general/SpaceConsistencyBearTest.py
+++ b/tmp/tmp40ntr2v2/tests/general/SpaceConsistencyBearTest.py
@@ -77,12 +77,12 @@
self.section.append(Setting('allow_trailing_blanklines', 'False'))
self.check_invalidity(self.uut,
- ['def funny_code():\n',
- " print('Is it funny?')\n",
- " print('Yeah, funny')\n",
- ' ',
- ''],
- force_linebreaks=False)
+ ['def funny_code():\n',
+ " print('Is it funny?')\n",
+ " print('Yeah, funny')\n",
+ ' ',
+ ''],
+ force_linebreaks=False)
self.check_validity(self.uut,
['def funny_code():\n',
" print('Is it funny?')\n",
@@ -70,3 +70,21 @@ def test_enforce_newline_at_eof(self): | |||
" print('funny')\n", | |||
" print('the result is not funny...')"], | |||
force_linebreaks=False) | |||
def test_trailing_blanklines(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E301 expected 1 blank line, found 0
Origin: PycodestyleBear (E301), Section: autopep8
.
self.section.append(Setting('allow_trailing_blanklines', 'False')) | ||
|
||
self.check_invalidity(self.uut, | ||
['def funny_code():\n', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E128 continuation line under-indented for visual indent
Origin: PycodestyleBear (E128), Section: autopep8
.
|
||
if no_blanklines: | ||
if enforce_newline_at_EOF: | ||
# Since every line contains at the end at least one \n, only |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is longer than allowed. (80 > 79)
Origin: LineLengthBear, Section: linelength
.
replacement += '\n' | ||
result_texts.append('No newline at EOF.') | ||
additional_info_texts.append( | ||
"A trailing newline character ('\\n') is missing from " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is longer than allowed. (83 > 79)
Origin: LineLengthBear, Section: linelength
.
99aadeb
to
df062a5
Compare
|
||
self.check_validity(self.uut, | ||
['hello world \n'], | ||
force_linebreaks=False) | ||
self.check_validity(self.uut, | ||
['def somecode():\n', | ||
" print('funny')\n", | ||
" print('funny end.')\n"], | ||
" print('funny end.')\n", | ||
" \n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You do not use the preferred quotation marks.
Origin: QuotesBear, Section: python
.
The issue can be fixed by applying the following patch:
--- a/tmp/tmpl6now4yj/tests/general/SpaceConsistencyBearTest.py
+++ b/tmp/tmpl6now4yj/tests/general/SpaceConsistencyBearTest.py
@@ -62,7 +62,7 @@
['def somecode():\n',
" print('funny')\n",
" print('funny end.')\n",
- " \n"
+ ' \n'
"\n"],
force_linebreaks=False)
self.check_invalidity(self.uut,
" print('funny end.')\n"], | ||
" print('funny end.')\n", | ||
" \n" | ||
"\n"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You do not use the preferred quotation marks.
Origin: QuotesBear, Section: python
.
The issue can be fixed by applying the following patch:
--- a/tmp/tmpl6now4yj/tests/general/SpaceConsistencyBearTest.py
+++ b/tmp/tmpl6now4yj/tests/general/SpaceConsistencyBearTest.py
@@ -63,7 +63,7 @@
" print('funny')\n",
" print('funny end.')\n",
" \n"
- "\n"],
+ '\n'],
force_linebreaks=False)
self.check_invalidity(self.uut,
[' no hello world'],
6550c5f
to
e784493
Compare
reversed(file)) | ||
rev_enumerated_tuple = tuple(rev_enumerated_zip_obj) | ||
line_nr_start = -1 | ||
blanklines_status = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file contains unused source code.
Origin: PyUnusedCodeBear, Section: flakes
.
The issue can be fixed by applying the following patch:
--- a/tmp/tmplpvp2vcv/bears/general/SpaceConsistencyBear.py
+++ b/tmp/tmplpvp2vcv/bears/general/SpaceConsistencyBear.py
@@ -48,7 +48,6 @@
reversed(file))
rev_enumerated_tuple = tuple(rev_enumerated_zip_obj)
line_nr_start = -1
- blanklines_status = False
for line_number, line in rev_enumerated_tuple:
replacement = line
for line_number, line in rev_enumerated_tuple: | ||
replacement = line | ||
if replacement.strip() == '': | ||
blanklines_status = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file contains unused source code.
Origin: PyUnusedCodeBear, Section: flakes
.
The issue can be fixed by applying the following patch:
--- a/tmp/tmplpvp2vcv/bears/general/SpaceConsistencyBear.py
+++ b/tmp/tmplpvp2vcv/bears/general/SpaceConsistencyBear.py
@@ -53,7 +53,6 @@
for line_number, line in rev_enumerated_tuple:
replacement = line
if replacement.strip() == '':
- blanklines_status = True
line_nr_start = line_number
else:
break
934b7d6
to
015c86c
Compare
@@ -38,48 +41,84 @@ def run(self, | |||
result_texts = [] | |||
additional_info_texts = [] | |||
|
|||
def get_blanklines_nr_start(): | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
though not violating PEP8, I usually prefer no blanklines between a def
and code, this kinda interrupts reading flow imo. Optional though if you see it differently ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed 👍
015c86c
to
e1f1a4c
Compare
additional_info_text = ( | ||
'Your source code contains trailing blanklines at EOF.' | ||
'Those usually have no meaning. Please consider' | ||
'removing them.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too much text. Just Trailing blanklines found
or similar. No intentions, no suggestions ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then you can easily inline this value as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah nvm that's the additional-info-text
'Trailing blanklines found.', | ||
diffs={filename: diff}, | ||
file=filename, | ||
additional_info=additional_info_text) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you also fill the affected_code
attribute? Not sure if we are that smart to extract the ranges from the diff^^
additional_info_text = ( | ||
'Your source code contains trailing blanklines at EOF.' | ||
'Those usually have no meaning. Please consider' | ||
'removing them.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation: 4 spaces relative to statement
|
||
else: | ||
blanklines_start_line = line_nr_start + 1 \ | ||
if line_nr_start < len(file) else False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have the feeling you are unnecessary trying to use special values (--> False
).
You can easily check if you have to yield a result if line_nr_start < len(file)
.
The whole stuff can be refactored to roughly this:
if not allow_trailing_blanklines:
line_nr_start = next((i for i, line in reversed(list(
enumerate(file, start=1))) if line.strip() != ''), 0)
# TODO: Maybe more performance: Don't increment from start=1, but start=0 and
# the next-default value = -1, and increment in the end: next(..., -1) + 1
if line_nr_start < len(file):
# Note: You have to provide line_nr_start + 1 to refer to the right lines.
# line_nr_start is right now designed to give the starting line referring
# to zero-based indices.
yield Result(...)
else:
line_nr_start = len(file)
for line_number, line in enumerate(file[:line_nr_start])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note also how I've put the relevant analysis inside the if, to save performance in case someone wants not to take care of blanklines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I wanted to do the same.But I was finding it difficult to cover the case when file is empty.That's why I used that (--> False), but now it will work as we are using start=0
and next default value -1
.
@@ -38,7 +41,36 @@ def run(self, | |||
result_texts = [] | |||
additional_info_texts = [] | |||
|
|||
for line_number, line in enumerate(file, start=1): | |||
line_nr_start = next((i for i, line in reversed(list( | |||
enumerate(file, start=1))) if line.strip() != ''), 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional:
Is it maybe possible to wrap the statement somehow so it's more readable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something in the direction of
line_nr_start = next(
(i
for i, line in reversed(list(enumerate(file, start=1)))
if line.strip() != ''), 0)
['def funny_code():\n', | ||
" print('Is it funny?')\n", | ||
" print('Yeah, funny')\n"], | ||
force_linebreaks=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to see tests for
- an empty file
- a file with only a single blank line
- a file with only multiple blank lines
- a file with leading and trailing blank lines
['def funny_code():\n', | ||
" print('Is it funny?')\n", | ||
" print('Yeah, funny')\n"], | ||
force_linebreaks=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And please use check_results
to assert for the bear results 👍
unack 2e2eaac |
Add allow_trailing_blanklines option for whether to allow trailing blanklines at EOF.And some blanklines are also removed from some files just because of `blanklines_at_EOF` option. Closes coala#1793
2e2eaac
to
3d50da1
Compare
end_column=len(file[-1]), | ||
additional_info=additional_info_text) | ||
|
||
else : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code does not comply to PEP8.
Origin: PEP8Bear, Section: autopep8
.
The issue can be fixed by applying the following patch:
--- a/tmp/tmpyheznxr2/bears/general/SpaceConsistencyBear.py
+++ b/tmp/tmpyheznxr2/bears/general/SpaceConsistencyBear.py
@@ -64,7 +64,7 @@
end_column=len(file[-1]),
additional_info=additional_info_text)
- else :
+ else:
line_nr_start = len(file)
for line_number, line in enumerate(file[:line_nr_start],
end_column=len(file[-1]), | ||
additional_info=additional_info_text) | ||
|
||
else : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E203 whitespace before ':'
Origin: PycodestyleBear (E203), Section: autopep8
.
column=1, | ||
end_line=1, | ||
end_column=3)], | ||
filename = 'xyz.py') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code does not comply to PEP8.
Origin: PEP8Bear, Section: autopep8
.
The issue can be fixed by applying the following patch:
--- a/tmp/tmpyheznxr2/tests/general/SpaceConsistencyBearTest.py
+++ b/tmp/tmpyheznxr2/tests/general/SpaceConsistencyBearTest.py
@@ -94,7 +94,7 @@
column=1,
end_line=1,
end_column=3)],
- filename = 'xyz.py')
+ filename='xyz.py')
# self.check_results(self.uut,
# [' \n',
# ' \n',
column=1, | ||
end_line=1, | ||
end_column=3)], | ||
filename = 'xyz.py') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E251 unexpected spaces around keyword / parameter equals
Origin: PycodestyleBear (E251), Section: autopep8
.
column=1, | ||
end_line=1, | ||
end_column=3)], | ||
filename = 'xyz.py') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E251 unexpected spaces around keyword / parameter equals
Origin: PycodestyleBear (E251), Section: autopep8
.
please fix the gitmate issues ;) |
Conflicts to be resolved. |
no. They are easy to finish. |
end_column=len(file[-1]), | ||
additional_info=additional_info_text) | ||
|
||
else : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix this whitespace issue @khanchi97. Run coala
over your local repository before pushing it to remote 😉
@@ -18,6 +18,7 @@ def run(self, | |||
file, | |||
use_spaces: bool, | |||
allow_trailing_whitespace: bool=False, | |||
allow_trailing_blanklines: bool=False, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alphabetical order 😉
if line.strip() != ''), -1) + 1 | ||
if line_nr_start < len(file): | ||
additional_info_text = ( | ||
'Your source code contains trailing blanklines at EOF.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a space after EOF.
.
if line_nr_start < len(file): | ||
additional_info_text = ( | ||
'Your source code contains trailing blanklines at EOF.' | ||
'Those usually have no meaning. Please consider' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too.
@@ -44,23 +46,28 @@ def test_data_sets_tabs(self): | |||
self.section.append(Setting('use_spaces', 'false')) | |||
self.section.append(Setting('allow_trailing_whitespace', 'true')) | |||
self.section.append(Setting('enforce_newline_at_EOF', 'false')) | |||
self.section.append(Setting('allow_trailing_blanklines', 'false')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is not required.
self.section['allow_trailing_whitespace'] = 'false' | ||
self.section['enforce_newline_at_EOF'] = 'true' | ||
self.section['allow_trailing_blanklines'] = 'false' | ||
self.check_results(self.uut, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you say about this?
def test_trailing_blanklines(self):
self.maxDiff = None
self.section.append(Setting('use_spaces', 'true'))
self.section.append(Setting('allow_trailing_whitespace', 'false'))
self.section.append(Setting('enforce_newline_at_EOF', 'true'))
self.section.append(Setting('allow_trailing_blanklines', 'false'))
self.check_validity(self.uut,
['sangam\n'],
force_linebreaks=False)
self.check_invalidity(self.uut,
[' \n'],
force_linebreaks=False)
Add allow_trailing_blanklines option for whether to allow
trailing blanklines at EOF.
Closes #1793
For short term contributors: we understand that getting your commits well
defined like we require is a hard task and takes some learning. If you
look to help without wanting to contribute long term there's no need
for you to learn this. Just drop us a message and we'll take care of brushing
up your stuff for merge!
Checklist
them.
individually. It is not sufficient to have "fixup commits" on your PR,
our bot will still report the issues for the previous commit.) You will
likely receive a lot of bot comments and build failures if coala does not
pass on every single commit!
After you submit your pull request, DO NOT click the 'Update Branch' button.
When asked for a rebase, consult coala.io/rebase
instead.
Please consider helping us by reviewing other peoples pull requests as well:
cobot mark wip <URL>
to get it outof the review queue.
The more you review, the more your score will grow at coala.io and we will
review your PRs faster!