-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Adding relative import support (#60) #379
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! I should be able to review it during this weekend. |
Ah I wasn't quite able to make it... been finishing the final (hopefully!) corrections to my PhD dissertation and recovering from a cold. I should have more time the next week. |
I played around with it a bit. It seems that full names of references via modules imported using a relative import are not normalized during semantic analysis. Here's a failing test case that I wrote (it would go to mypy/test/data/semanal-modules.test):
It works as expected if I replace the relative import with an absolute one. |
Here's another test case that fails:
It fails with an exception:
|
Ah, I forgot to fix the semantic analyzer ( # file: bar.py
## `semanal.py:598`; self.cur_mod_id; => "bar"
from . import foo
# file: baz/__init__.py
## `semanal.py:598`; self.cur_mod_id; => "baz"
from .. import foo
## Does not work in file `baz/__init__.py`, instead searches for `baz/foo.py`:
"""
from . import foo
"""
## => ImportError: cannot import name 'foo' In my fix for Perhaps adding a function to This is a much bigger change to the node classes than my rather minor changes to the |
Could you use the Adding a method such as |
That sounds good (didn't realize it had a |
Of course that would also allow other common functionality to be refactored onto the node class, which may reduce code/complexity. |
I don't think it's an issue to have both |
@mason-bially Just checking if you are still working on this, as this would be a great feature to have? :-) |
@JukkaL Sometime this weekend I should have the finished patch for review again. |
@mason-bially Good! |
@mason-bially Are you still working on this? It would be great to have relative import support, and I might work on this myself at some point. I'm cleaning up pull requests and wondering if this should be closed for now. |
* Changing the `all_imported_modules_in_file(self, file)` projection in build which gathers imports from file ASTs root nodes. * Adding support for the relative import syntax in noderepr.py and output.py. * Adding a relative counter int to the AST nodes and fixing up treetransform.py. * Changing the `parse_import_from(self)` function to parse relative imports.
* Cleaning up and adding relevant tests.
So this is working. I have a few more tests to add, with specific attention to
Fails. It appears that placing imports in inits (or perhaps any code) causes them to fail with an assertion error exactly like:
This is where I was stuck a couple of months ago. I wasn't able to figure out why it was failing. |
I guess we had this problem previously as well. I don't quite understand what's going wrong there. What does that error mean? Any ideas why imports in |
I think I have a better understanding of what is happening. The relative imports in |
I can look into this. The code that deals with module dependencies and different build states is pretty hairy. |
There was an unrelated bug in build.py. I fixed it and things started working :-D |
This provides #60.
all_imported_modules_in_file(self, file)
projection in build which gathers imports from file ASTs root nodes.parse_import_from(self)
function to parse relative imports.