-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Support relative imports #60
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
Comments
Also |
I ran into this error too, there appears a need to add the ability to deal with a dot here in the parse unfortunately, I'm not sure what that would mean for the rest of the import code which will need to support it. |
Would need to expand this node (and the one below) to deal with it, likely a relative file-name (or just a bool if it's available elsewhere in the tree). |
* 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.
I guess anybody can finish the implementation, as it seems that @mason-bially is no longer working on this. If nobody else picks this up, I might work on this in the next couple of weeks, since this is a pretty major missing feature. |
* 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.
I am working on this now. I burned out during the period I was working on this due to other projects. I've had almost passing code in my repo for ages. I'm cleaning this up ASAP, apologies. |
* Cleaning up and adding relevant tests.
* Cleaning up and adding relevant tests.
Relative imports work now! |
I'm running today's version from git (both globally and in the virtualenv) from mypy (just started, but so far love the project!), still get error with relative imports. My from . import mongo
from . import devicetracker
from . import website
from . import constants
from . import fileimporter
LOGFORMAT = '%(levelname)s %(asctime)s %(message)s'
__all__ = [mongo, devicetracker, website, constants, fileimporter] result:
Anything obvious I'm missing? |
This seems to be an error related to relative imports in the main package. It might be conflicting with the fact that the main package is also an For a temporary fix, try using a separate main file which imports the library? |
So I originally suspected this. The use case you are trying is not supported by python. Consider:
The temporary fix I proposed should work correctly. I'm assuming you already have a main script which imports and uses the module with the Although it might be worth fixing relative imports to support this behavior simply for usability, but that would be an additional feature (in my opinion). |
Right, this makes sense. I guess that's why there is an I am hoping to convince my editor to check each file each time I save it, guess I should accept that not working in this case. Would indeed be nice to have at some point, but I see how this is not top priority now. |
Unfortunately, I believe the correct way to do that at the moment is to check the whole project from the beginning every time (i.e. from the root Since I'm not actually part of the team maintaining this project I don't know what the plans are for that. |
Mypy should probably support type checking any file within a package, instead of having to start from a top-level module or having to use
Now all of these should work:
I need to think about this a bit more, though. |
I just hit this while fixing the test script to actually test everything, not just a subset. It is unrelated to which module is the "main" module. Rather, it seems that relative imports are supported in |
Actually, it looks related to the Now to fix the actual bugs in the stubs that weren't being tested ... |
The following is currently a parse error:
The text was updated successfully, but these errors were encountered: