-
Notifications
You must be signed in to change notification settings - Fork 543
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
[gazelle] Support relative python imports #2203
Comments
Yeah, the issues you've mentioned is a real pain. That said, I think To have something that works and is still experimental, you could implement it in stages:
What other things would we need to have around this issue? |
Is this unique for relative imports? Normal python imports hit the same issue, which is resolved by the language extension walking up the module import path and looking in the gazelle rule index ( I started working on relative imports myself, but abandoned it after hitting some issues with getting the tests to pass with other internal patches that I've made on the extension. I was able to convince our devs to migrate to absolute imports, so getting this landed upstream wasn't a priority. I stripped out my changes to have just the relative import behavior and the tests appear to pass. If someone wants to take on getting additional tests in place and landing it, go ahead. All of the changes are on the HEAD commit of this branch. Out-of-scope, but noteworthy for this change -- It would be much easier to maintain the parser if it used treesitter queries. The tests for the parser seem sufficient, but I'm not 100% confident that my change in the parser didn't break something. Using treesitter queries would simplify the code, but I'm not sure if there was a reason for not doing so when the extension was originally written. |
No, I don't think it's unique to relative imports. I do think that using a relative import for a module member is rare, though. At least, I've never seen
Nice! Personally I'd be just fine with Gazelle not supporting relative imports (even though I opened this issue, haha). When projects get to be the size that Bazel/Gazelle is really helpful, relative imports cause, IMO, undue confusion.
TIL! I'd be happy to review a PR that switches to queries, though I admit I know very little about tree-sitter.
That would be a question for @hunshcn who added it in #1895. It's possible it's just another "TIL" haha. |
🚀 feature request
Relevant Rules
Description
Sometimes a python code base uses relative imports:
While I don't necessarily agree with this style, it's something that Python supports (see PEP 328) and thus I believe that Gazelle should support it too.
Right now, Gazelle simply ignores these imports. Here's an example:
If we change a.py to use an absolute import:
Gazelle will add the dep:
Describe the solution you'd like
I'm somewhat familiar with the Gazelle code base. I believe it will be pretty easy (for the most part - see Issues below) below) to generate the full target path for the current file being processed, and then adjust the target based on how many dots
.
are in the relative import.Because of the Issues, I'd imagine that this would be an experimental, opt-in feature for a while. It would be guarded by a directive:
Here are some examples of the logic, assuming a slightly more complex dir structure:
The above examples assume file-level generation. More thought will be needed to support package-level generation.
Issues
It's difficult to know if
from ..foo import bar
should be//:foo
or//foo:bar
. Isbar
a class/function? Or isbar
another module? In the example above, it's a python module, but that's not always the case.In the
from .. import baz
case, it's possible thatbaz
is an identifier defined in the parent package's__init__.py
, and thus the dep target would be//:__init__
.Describe alternatives you've considered
I tried convincing the developers to use absolute imports, but they just weren't having it 🤣
So I also tried using the "package" generation mode, but the issue was that unit test files also used relative imports and would not include the dep.
The current workaround is to add
# gazelle:include_dep //example:b
annotations, but those are prone to diverging from the actual code and can be tedious to write for large projects.The text was updated successfully, but these errors were encountered: