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

Fix part of #3905: Add lint checks for alphabetical import order #4343

Merged
merged 7 commits into from
Jan 6, 2018
Merged

Fix part of #3905: Add lint checks for alphabetical import order #4343

merged 7 commits into from
Jan 6, 2018

Conversation

apb7
Copy link
Contributor

@apb7 apb7 commented Jan 3, 2018

This PR enables pre_commit_linter.py to check for alphabetical import order. Also, import order for all files have been fixed.

Checklist

  • The PR title starts with "Fix #bugnum: ", followed by a short, clear summary of the changes.
  • The linter/Karma presubmit checks have passed.
    • These should run automatically, but if not, you can manually trigger them locally using python scripts/pre_commit_linter.py and bash scripts/run_frontend_tests.sh.
  • The PR is made from a branch that's not called "develop".
  • The PR follows the style guide.
  • The PR is assigned to an appropriate reviewer.
    • If you're a new contributor, please ask on Gitter for someone to assign a reviewer.
    • If you're not sure who the appropriate reviewer is, please assign to the issue's "owner" -- see the "talk-to" label on the issue.

@apb7
Copy link
Contributor Author

apb7 commented Jan 3, 2018

@seanlip and @DubeySandeep: Please review!
Thanks!

@DubeySandeep DubeySandeep requested a review from seanlip January 3, 2018 15:03
@DubeySandeep DubeySandeep changed the title Fixes part of #3905: Add lint checks for alphabetical import order Fix part of #3905: Add lint checks for alphabetical import order Jan 3, 2018
Copy link
Member

@seanlip seanlip left a 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
Copy link
Member

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)?

Copy link
Contributor Author

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.

@@ -36,6 +34,7 @@
from core.domain import user_services
from core.platform import models
import feconf
import jinja2
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

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.

import feconf
import jinja2
import mutagen
Copy link
Member

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

Copy link
Contributor Author

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!

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 \
Copy link
Member

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).

Copy link
Contributor Author

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.


import feconf

#pylint: enable=wrong-import-order
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

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

3rd party?

Copy link
Contributor Author

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
Copy link
Member

Choose a reason for hiding this comment

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

3rd party

Copy link
Contributor Author

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
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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:
import-patch
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!

Copy link
Member

@seanlip seanlip Jan 4, 2018

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!

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:
Copy link
Member

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.

Copy link
Contributor Author

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.

@seanlip
Copy link
Member

seanlip commented Jan 3, 2018

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!

@apb7
Copy link
Contributor Author

apb7 commented Jan 3, 2018

Sorry, @seanlip! I will take care of this from the next time. Also, thanks for such an in-depth review!

@codecov-io
Copy link

codecov-io commented Jan 3, 2018

Codecov Report

Merging #4343 into develop will increase coverage by 0.92%.
The diff coverage is n/a.

Impacted file tree graph

@@             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
Impacted Files Coverage Δ
...dev/head/domain/utilities/BrowserCheckerService.js 72.22% <0%> (-22.52%) ⬇️
...ead/services/contextual/WindowDimensionsService.js 60% <0%> (-20%) ⬇️
...ead/domain/exploration/ExplorationObjectFactory.js 58.46% <0%> (-4.04%) ⬇️
...er/RefresherExplorationConfirmationModalService.js 4.76% <0%> (-2.39%) ⬇️
...s/dev/head/domain/utilities/LanguageUtilService.js 86.11% <0%> (-1.07%) ⬇️
...d/pages/exploration_player/ProgressNavDirective.js 2.22% <0%> (-0.72%) ⬇️
...ions/MusicNotesInput/directives/MusicNotesInput.js 53.96% <0%> (-0.49%) ⬇️
...n/collection/CollectionPlaythroughObjectFactory.js 4.34% <0%> (-0.42%) ⬇️
.../templates/dev/head/services/AudioPlayerService.js 17.24% <0%> (-0.31%) ⬇️
...ges/exploration_editor/editor_tab/StateSolution.js 1.25% <0%> (-0.14%) ⬇️
... and 37 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ae597b...c8e8535. Read the comment docs.

@apb7
Copy link
Contributor Author

apb7 commented Jan 3, 2018

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 #pylint: enable=wrong-import-order wherever possible but we cannot remove #pylint: enable=relative-import yet as it causes linting errors.
Can you please review it again?
Thanks!

.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
Copy link
Member

Choose a reason for hiding this comment

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

Suggest alphabetizing this list.

Copy link
Contributor Author

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


Copy link
Member

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
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, sure!

Copy link
Contributor Author

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!

Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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.

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:
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

print '----------------------------------------'
print ''
if failed:
summary_message = '%s Import order failed' % _MESSAGE_TYPE_FAILED
Copy link
Member

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"

Copy link
Contributor Author

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!

@apb7
Copy link
Contributor Author

apb7 commented Jan 6, 2018

@seanlip and @DubeySandeep: I am unable to understand why Travis build is failing in case of bash scripts/run_e2e_tests.sh --suite=misc --prod_env. It passes all tests when I run it locally.
Thanks!

@nithusha21
Copy link
Contributor

Hi @apb7 I have restarted the test on Travis and it passes. Sometimes Travis tests are flaky and if you feel that the issue is not because of your code changes and the test seems to be failing let us know on the Gitter chat and one of the contributors will restart it for you.

@apb7
Copy link
Contributor Author

apb7 commented Jan 6, 2018

Thank you @nithusha21!
@seanlip: Can you please review it? Thanks!

Copy link
Member

@seanlip seanlip left a 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!

@apb7
Copy link
Contributor Author

apb7 commented Jan 6, 2018

Thank you @seanlip!

@seanlip seanlip merged commit 60067ed into oppia:develop Jan 6, 2018
@apb7 apb7 deleted the import-patch branch January 7, 2018 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants