-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 part of #3905: Add lint checks for alphabetical import order #4343
Conversation
@seanlip and @DubeySandeep: Please review! |
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.
Good start, thanks @apb7! Took a first pass.
@@ -16,8 +16,9 @@ | |||
|
|||
import os | |||
|
|||
import constants #pylint: disable=relative-import | |||
from core.tests import test_utils #pylint: disable=relative-import | |||
import constants # pylint: disable=relative-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.
This check for two spaces is good! Does it come with isort too (i.e. will it error if it's wrong)?
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.
Yes! It comes with isort. It is the default setting though I will look into it.
core/controllers/admin.py
Outdated
@@ -36,6 +34,7 @@ | |||
from core.domain import user_services | |||
from core.platform import models | |||
import feconf | |||
import jinja2 |
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.
jinja2 is actually an App Engine library, not part of our codebase. It should be considered third-party.
Same for pipeline, webapp2, webtest and yaml, I think.
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 will put them on the third-party list.
@@ -16,15 +16,11 @@ | |||
|
|||
"""Controllers for the editor view.""" | |||
|
|||
import StringIO |
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.
It seems a bit odd to me to have uppercase above lowercase when sorting, instead of treating this similar to stringIO. Is there a setting that can be used to ignore case? If not, that's fine.
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 could not find a way around this. There was no setting pertaining to this.
core/controllers/editor.py
Outdated
import feconf | ||
import jinja2 | ||
import mutagen |
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.
Same thing -- jinja2, mutagen are 3rd-party
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'll add them!
core/controllers/editor_test.py
Outdated
from core.domain import user_services | ||
from core.platform import models | ||
from core.platform.taskqueue import gae_taskqueue_services as taskqueue_services | ||
from core.platform.taskqueue 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.
Nope -- never end lines with . See our coding guidelines. The previous way was fine, I think (and, if not, a different workaround is needed).
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, got it! I will change the length of import lines according to our coding guidelines.
core/tests/gae_suite.py
Outdated
|
||
import feconf | ||
|
||
#pylint: enable=wrong-import-order |
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.
Well... if it works without this then we don't need this at all, right?
Also, more generally, please make sure to verify all files manually after the changes have been made by isort.
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 will see this and manually check the files.
@@ -20,11 +20,10 @@ | |||
import time | |||
import urlparse | |||
|
|||
import browsermobproxy |
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.
3rd party?
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.
Got it!
|
||
from core.domain.base_classifier import BaseClassifier | ||
import feconf | ||
import numpy |
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.
3rd party
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, got it!
|
||
from mapreduce import main as mapreduce_main | ||
from mapreduce import parameters as mapreduce_parameters | ||
import webapp2 | ||
from webapp2_extras.routes import RedirectRoute | ||
|
||
# pylint: enable=relative-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.
Check whether this is still needed, please.
Ditto elsewhere. Make sure to manually audit all files and drop pragmas that are no longer needed.
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.
Sure! I will ensure this.
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.
Why is this still here? Ditto elsewhere.
It used to exist because of mapreduce/webapp2 I think, but if that's not needed any more then I'm not sure what it's point is. Could you clarify?
Also, make sure to run the app locally (plus at least one backend test) to ensure all of the infrastructure still works after your 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.
I tried removing it and then running the lint checks. It produces an error as Pylint recommends import .feconf
instead of import feconf
. It is not because of mapreduce/webapp2.
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 ok. I'm fine with import .feconf
, though it's a bit unfamiliar. Does from . import feconf
work? If so, that would probably be better.
Also, if we change it here, we should change it everywhere, for consistency.
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, so this is the error produced in main.py
:
I think it will difficult to do away with pylint: enable=relative-import
as we will have to change it manually for each such import.
Please suggest what should be done. We can also disable relative import check from .pylintrc and then remove these statements.
Thanks!
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 see, interesting. OK, let's go with the enable-relative-import pragma then, as we had it before. Thanks for checking!
scripts/pre_commit_linter.py
Outdated
newline_messages = _check_newline_character(all_files) | ||
linter_messages = _pre_commit_linter(all_files) | ||
pattern_messages = _check_bad_patterns(all_files) | ||
all_messages = ( | ||
linter_messages + newline_messages + pattern_messages) | ||
if any([message.startswith(_MESSAGE_TYPE_FAILED) for message in | ||
all_messages]): | ||
all_messages]) or import_order_message: |
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.
why not just add import_order_message to all_messages? Also keep the order in line 663 the same as the order in lines 658-661 for consistency.
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.
import_order_message returns boolean and not the actual message. Therefore I used it this way. I will ensure the consistency though.
Hey @apb7, one tip: instead of replying to comments one-by-one, go to the "Files Changed" tab and make comments inline, choosing "Start a review" as you do so. Then GitHub will send a single email once you've made all the comments. Thanks! |
Sorry, @seanlip! I will take care of this from the next time. Also, thanks for such an in-depth review! |
Codecov Report
@@ Coverage Diff @@
## develop #4343 +/- ##
===========================================
+ Coverage 43.74% 44.66% +0.92%
===========================================
Files 375 375
Lines 23409 23038 -371
Branches 3778 3663 -115
===========================================
+ Hits 10241 10291 +50
+ Misses 13168 12747 -421
Continue to review full report at Codecov.
|
Hey @seanlip, I confirmed the isort documentary and it does check for two lines after top-level class or function, by default. Also, I have removed |
.isort.cfg
Outdated
line_length=80 | ||
force_single_line=true | ||
force_sort_within_sections=true | ||
known_third_party=google.appengine,mapreduce,webapp2,webapp2_extras,jinja2,pipeline,webtest,yaml,browsermobproxy,numpy,mutagen,cloudstorage |
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.
Suggest alphabetizing this list.
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 will do so.
@@ -20,6 +20,7 @@ | |||
from core.tests import test_utils | |||
import feconf | |||
|
|||
|
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.
OK -- just to confirm, let's say you make this just have one line, then run the pre-commit script (but no autofixing). Will this show up as an error? What does the error message say?
from core.platform.email import gae_email_services | ||
import feconf | ||
import requests |
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.
requests is 3rd-party. There are other third-party libs I'm seeing too that you haven't detected, so, again, please check this whole PR carefully.
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, sure!
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.
@seanlip: I have added requests to the known third-party list. I will try to add others. Can you please list others which have been missed?
Thanks!
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 think I mentioned before how to check this, but maybe I wasn't clear enough. Stuff like import feconf
and import utils
is fine because, at the top level of the codebase, there are feconf.py and utils.py. But import cloudstorage
isn't fine because there's no cloudstorage.py at the top-level of the codebase, so that's a third-party lib. That's how you can check.
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.
Thank you @seanlip! I will see this. I have included all the libraries which were being installed by the the bash scripts for now. Thanks!
|
||
from mapreduce import main as mapreduce_main | ||
from mapreduce import parameters as mapreduce_parameters | ||
import webapp2 | ||
from webapp2_extras.routes import RedirectRoute | ||
|
||
# pylint: enable=relative-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.
Why is this still here? Ditto elsewhere.
It used to exist because of mapreduce/webapp2 I think, but if that's not needed any more then I'm not sure what it's point is. Could you clarify?
Also, make sure to run the app locally (plus at least one backend test) to ensure all of the infrastructure still works after your changes.
scripts/pre_commit_linter.py
Outdated
any(fnmatch.fnmatch(filename, pattern) for pattern in EXCLUDED_PATHS)] | ||
failed = False | ||
for filename in all_files: | ||
if isort.SortImports(filename, check=True).incorrectly_sorted: |
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.
Just to confirm: does this line update the files with wrong import order as well, or does it just return True/False and do nothing 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.
This line prints the actual error message with the file path and returns True if it finds an error 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.
Ah, that seems very side-effecty. Please add a comment above this line explaining that, since it's rather unexpected.
scripts/pre_commit_linter.py
Outdated
print '----------------------------------------' | ||
print '' | ||
if failed: | ||
summary_message = '%s Import order failed' % _MESSAGE_TYPE_FAILED |
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 isn't really useful for someone trying to debug and fix a lint error...? You'll need to give more information, including perhaps a list of files (and errors, if possible).
Also, here and below, the message should be "Import order checks failed"
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 just to maintain consistency with the rest of the checks so that import order messages can be incorporated in all messages. The actual error message is printed automatically by Line 649 when SortImports is called.
Thanks!
@seanlip and @DubeySandeep: I am unable to understand why Travis build is failing in case of |
Thank you @nithusha21! |
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 think it looks good. Thanks @apb7!
Thank you @seanlip! |
This PR enables
pre_commit_linter.py
to check for alphabetical import order. Also, import order for all files have been fixed.Checklist
python scripts/pre_commit_linter.py
andbash scripts/run_frontend_tests.sh
.