-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Import linter improvements: enforce usage of a package's public API #31903
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
Conversation
|
Thanks for the pull request, @bradenmacdonald! As a core committer in this repo, you can merge this once the pull request is approved per the core committer reviewer requirements and according to the agreement with your edX Champion. |
4a1ac0a to
35f5f6a
Compare
35f5f6a to
24e5712
Compare
|
Can you explain what means? is task importing from content_highlights which imports from block_render? |
Yes, that's exactly what it means.
Although now that I look those links, the LMS import in content_highlights is actually in a function and is not a top-level import, so it may or may not actually be used from |
|
@bradenmacdonald: This seems really nice.
|
I would like to do that but I'm afraid the list of exceptions would be too long. Without having something like this in place, it's kind of been the wild west for cross-package imports. That said, I have only experimented with a few packages so far. It would probably be worth methodically testing every major package (at least in |
|
Following up on my previous comment: Currently this is opt-in on a per-package basis. I definitely could change it to an opt-out approach: default to linting all packages in certain prefixes like I believe this will also support linting openedx django app packages that are developed in separate repos but installed into our edxapp venv, too, but I haven't tried that yet. (For example, it could check that we don't use anything from the |
If the exceptions are all automatically marked with a specially designated amnesty disable pragma, why does the quantity matter? They exist either way, and it seems better to lock things down so no more are added. Anyway, just a thought. |
Currently they'd all have to be listed in the |
|
Hi @bradenmacdonald and @robrap! Just checking in on this :) |
|
@mphilbrick211 I was just waiting for more feedback on this... @ormsbee what did you think? |
ormsbee
left a comment
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 great stuff, and I totally agree with opt-in to start. It might be worth adding something to OEP-49 after this merges.
My only question has to do with why it was necessary to create the api_impl.py module and move things around.
Basically there was a circular import. I wanted to expose That was the primary reason, and the secondary reason is that the code in |
|
@bradenmacdonald: Consider adding a little of that context to the docstring for api_impl.py so this is more obvious in the future. |
|
(Well, approved once that last test suite passes.) |
fcc0647 to
c189fa5
Compare
c189fa5 to
76d49da
Compare
|
@bradenmacdonald 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future. |
|
Thanks for the feedback everyone! I have left this as opt-in for now, and I've opened another PR to update the OEP to mention this, if someone would like to review: openedx/openedx-proposals#467 |
|
EdX Release Notice: This PR has been deployed to the staging environment in preparation for a release to production. |
|
EdX Release Notice: This PR has been deployed to the production environment. |
Two files backport from openedx#31903
Description
#31062 introduced an import linter, with a very simple configuration to flag imports from LMS code into CMS and vice versa.
This PR extends that import linter in two ways, which are intended to help enforce clean boundaries and stronger API contracts among the packages that comprise edx-platform.
Improved detection of LMS<->CMS imports
First, the existing "lms-cms independence" contract has been expanded to include the
openedxpackage namespace as well, which has found 14 additional existing imports (mostly from cms to lms) that shouldn't be there.A typical example of CMS code importing LMS code is:
Enforce usage of a package's python API
Second, I have implemented a new linter that can be given package names like this:
The idea is that these packages define their public API in
api.pyfiles (see OEP-49 Django App Patterns), so importing from any other module within those packages is an error (or in this case, a lint failure).Using this initial small set of example packages, here are the issues I found:
The first two items are a very nice example of catching issues: the code in question is not specific to content libraries but was living in the content_libraries app. To fix the issue, I moved it to the
blockstore_apiapp which is a more relevant location.I fixed all of the other errors found by exposing relevant code through
api.pyand updating imports to use that.My hope is that over time we can grow this list, and enforce a clear API separation for packages that are designed that way.
I'm looking for feedback on this approach!