-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Relative import from toplevel cause exception #2974
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
It seems (The assert is still a bug, the code should produce a user-facing error message about a relative import in what it thinks of as a top-level module, PS. If you really don't want to add an |
Sorry, indentation not quite right in the original post - I've corrected it and made it a little clearer. There is an I've also noticed that if I use absolute imports (i.e. Can anyone reproduce this? My python version is 3.6.0 and mypy is 0.501. |
Sorry to hear it. Can you publish the full source code and exactly how you run mypy to repro this? There's probably something specific in it that causes the breakage. |
Here is an example project that shows the issue: https://github.com/aquavitae/mypy-test. I hope it is just something I'm doing wrong! |
OK, the user error is that in the crashing runs you are attempting to treat cli.py as a top-level module, but a top-level module cannot use a relative import. The mypy bug is that it doesn't give a reasonable error, but instead just crashes. |
I didn't realize there was that limitation with relative imports. But surely the code shouldn't run then? Also, if I change it to use an absolute import ( |
Indeed, that code won't run from inside the Changing it to an absolute import requires Python (and mypy) to be able to find the toplevel module -- which means that you would have to have something like (I feel that you ought to educate yourself about the ins and outs of this issue independently -- the mypy issue should be purely about the crash, which should be fixed so it produces a regular error message.) |
Thanks, and you're right - I'll read up more on it. |
I'm running into this issue, and unfortunately I'm not quite following the explanation. I'm well aware of the issue about running a module inside a package, but I'm not seeing the connection to this issue (I'm not trying to run my program, just to typecheck it). What's the proper way to typecheck a file that uses relative imports? |
You typecheck from the project folder (and outside the package) and then select the file you want to have reports about. Just like you can't run something with relative imports from within the package, mypy will refuse to lint something that does this. The solution is to go up one level and outside of the package. |
Thanks, but I still don't follow: why doesn't mypy chose the appropriate directory on its own? |
In order to determine the correct module name of the file you pass it, mypy
searches up until it finds a directory that does *not* contain __init__.py.
However it doesn't search above the current directory (unless you gave it
an absolute path).
|
Thanks, that makes sense.
But I do :/ Should it work in that case? |
To clarify, the context is automatic linting (in an IDE plugin); I'm just trying to get a sense of how we should call mypy so that it works without requiring user configuration. |
I guess this is the same error?
|
That crash seems similar to #4881. From your messages I can't figure out exactly what it is you're doing so I can't help you further. If all you get is that crash, that obviously is a mypy bug and shouldn't happen. If you get some other failure, please boil it down to a simple example that can be reproduced outside your IDE integration. |
Thanks.
Sorry. Here's clarification. I'm attempting to maintain a mypy plugin for Emacs. Currently, the plugin always calls mypy from the current directory, on the file being edited. It passes absolute paths to mypy, and uses I'm attempting to understand whether 1. the Emacs extension is doing the right thing, and I just need to wait for or help implement a mypy fix, or 2. the Emacs extension is doing the wrong thing, and I need to change it to run in a different directory. From your previous statement, I understand that mypy's behavior (raising an assertion error despite being passed full paths) is unexpected. Am I understanding right? Here's a concrete repro: I create three files
|
Ah, The usage for |
When I implemented mypy linting for Sublime Text, I used to determine the top level folder (without init.py) manually before starting the lint and launched mypy from that folder. Now, however, the linting framework just launches linter processes from the project dir, so I don't need to do this anymore. |
FWIW the best way to think of
is equivalent to
(except for the obvious side effect that afterwards, A is overwritten. ;-) |
Ah, thanks a lot. It makes plenty of sense then. And passing the full name works great now. |
Apparently required for mypy to work on external scripts: python/mypy#2974
It looks like this has been fixed at some point, as I can't reproduce the crash on master (but can using mypy 0.600). |
Uh oh!
There was an error while loading. Please reload this page.
I have a folder structure something like this:
cli.py
contains a relative import,from .lib import options
. Running mypy from the root works fine, but if I run it from insideproject
, I get an exception:Digging into the source code, it looks like the issue is in add_ancestors(). When the relative path in encountered,
parent == '.lib'
, soparent.rsplit('.', 1) == ['', 'lib']
. The empty string gets added toancestors
, is eventually used as theid
parameter when initializingState
, and this assertion then fails.I'm not certain exactly how the module resolver is supposed to handle relative imports so I'm not clear on what is needed to fix this.
The text was updated successfully, but these errors were encountered: