-
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
PyImportSortBear.py: Show incorrectly sorted imports #886
base: master
Are you sure you want to change the base?
Conversation
Thanks for your contribution! Reviewing pull requests take really a lot of time and we're all volunteers. Please make sure you go through the following check list and complete them all before pinging someone for a review.
As you learn things over your Pull Request please help others on the chat and on PRs to get their stuff right as well! |
Comment on 30cbdbc. Shortlog of HEAD commit is 2 character(s) longer than the limit (52 > 50). GitCommitBear, severity NORMAL, section |
@sils could you please review ? |
build's red |
tests failing; marking as WIP till then |
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.
Okay I don't know about the Diff class enough to review the rest, sorry.
@@ -182,7 +184,19 @@ def run(self, filename, file, | |||
|
|||
if new_file != tuple(file): | |||
diff = Diff.from_string_arrays(file, new_file) | |||
all_imports = '' | |||
diff = Diff.from_string_arrays(file, new_file) |
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 188 and Line 186 do the same thing
Could anyone please review? |
@wimpykid26 please help us reviewing other PRs so you can get faster reviews. Also note that on many other big organizations you have average waiting times of weeks to months for patches. |
@@ -182,7 +184,18 @@ def run(self, filename, file, | |||
|
|||
if new_file != tuple(file): | |||
diff = Diff.from_string_arrays(file, new_file) | |||
all_imports = '' | |||
for line_nr in sorted(diff._changes): |
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.
_changes
is a "protected" member, you shouldn't access it^^
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.
there's probably another member that allows you to iterate over the changes 👍
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.
@Makman2 Thanks for pointing that out :). Should I use the range function to get the range of the diff and then use the _get_changes function for parsing?
list_of_imports[0].lstrip() | ||
list_of_imports.sort() | ||
string_of_imports = ' '.join(list_of_imports) | ||
all_imports = all_imports + " " + string_of_imports |
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 can do this a bit more elegant :)
Make all_imports
above to a list and change this line to:
all_imports = []
for line_nr in sorted(diff._changes):
#....
all_imports.append(string_of_imports)
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 and below doing
"Imports " + " ".join(all_imports) + " are sorted incorrectly"
or you could use format()
:
"Imports {} are sorted incorrectly.".format(" ".join(...))
@@ -182,7 +184,18 @@ def run(self, filename, file, | |||
|
|||
if new_file != tuple(file): | |||
diff = Diff.from_string_arrays(file, new_file) | |||
all_imports = '' | |||
for line_nr in sorted(diff._changes): | |||
linediff = diff._file[max(line_nr - 1, 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.
here too, please don't access _file
, I think we have actually a file
property, don't we?
for line_nr in sorted(diff._changes): | ||
linediff = diff._file[max(line_nr - 1, 0)] | ||
list_of_imports = re.split(', +', | ||
linediff.rstrip()) |
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.
fits into a single line (improves readability because of the comma inside the quotes, that's kinda confusing :3)
yield Result(self, | ||
"Imports can be sorted.", | ||
"Imports" + all_imports + " are sorted incorrectly", |
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.
Missing space after Imports
and missing period at end of message
list_of_imports = re.split(', +', | ||
linediff.rstrip()) | ||
if list_of_imports[0].startswith('import'): | ||
list_of_imports[0] = list_of_imports[0].lstrip('import') |
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.
lstrip
doesn't work like this, for example this string
importimoopX
would expand to
X
as lstrip
strips each character away, not a complete string in the given order. You can use str.replace
or if you need additional whitespace stripping, you can use regexes and their replacement functions
unack 151cb89 |
@@ -182,7 +184,19 @@ def run(self, filename, file, | |||
|
|||
if new_file != tuple(file): | |||
diff = Diff.from_string_arrays(file, new_file) | |||
all_imports = [] | |||
for line_nr in range(diff.range(filename).start.line, diff.range(filename).end.line + 1): |
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. (101 > 79)
LineLengthBear, severity NORMAL, section linelength
.
@@ -182,7 +184,20 @@ def run(self, filename, file, | |||
|
|||
if new_file != tuple(file): | |||
diff = Diff.from_string_arrays(file, new_file) | |||
all_imports = [] | |||
for line_nr in range(diff.range(filename).start.line, | |||
diff.range(filename).end.line + 1): |
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.
PEP8Bear, severity NORMAL, section autopep8
.
The issue can be fixed by applying the following patch:
--- a/bears/python/PyImportSortBear.py
+++ b/bears/python/PyImportSortBear.py
@@ -186,7 +186,7 @@
diff = Diff.from_string_arrays(file, new_file)
all_imports = []
for line_nr in range(diff.range(filename).start.line,
- diff.range(filename).end.line + 1):
+ diff.range(filename).end.line + 1):
if(diff._get_change(line_nr).change is not False):
line_diff = diff._get_change(line_nr).change[0]
list_of_imports = re.split(', +', line_diff.rstrip())
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
28 similar comments
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again! |
This will ensure that coala is
able to display the import statements
that are sorted incorrectly.
Closes #397