-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
mypy not finding modules located above the current working directory #6660
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
Is it also a problem with the official mypy PyCharm plugin? |
My understanding was that that plugin doesn't work on Windows (based on this open issue). I can give it a try anyway... |
Ah, yes sorry, it might not work on Windows indeed. |
Yeah we tried that plugin and it does not, in fact, work on Windows (it's looking for Do you know if the behavior I describe with module resolution is correct? Could there be something wrong with how we're configuring MyPy or structuring our project? |
What if you used an absolute path? I.e. (assuming you're inside |
That does work! So it looks like it's not the cwd that's the issue, but the way the file is specified. |
Took my best stab at a bit of code spelunking now that I was armed with that extra bit of information. It looks to my completely-unfamiliar-with-your-codebase eye that the crawling is being done using string manipulation on the path, and that the behavior I'm seeing is being caused by the use of |
Looks like I've found two separate issues here, after all. Turns out that PyCharm plugin is passing I still find the behavior described in this issue odd, so I'll leave it open until someone at least confirms the behavior is intentional. |
Good find. I don't actually know why it is done that way, but I can reproduce that if we replace that with abspath() it does find the sibling module. However I note that asking Python to execute that module (from inside the package) doesn't find the sibling module either. And mypy in general tries to emulate the import behavior of Python (with some exceptions). So this feels like it's intentional. |
That is true. I guess the conflict of assumptions here then would be that I was assuming that calling MyPy on a file doesn't necessarily imply that that file is intended as an entry point, just that I want to type-check that file. I could totally understand the entry point assumption, though. I would, however, think that the documentation should be modified to reflect it if that assumption is taken as the canonical one. Right now the import discovery process is not described as having any such restriction. |
You're right that the docs probably don't explain subtleties like this very
well. Do you feel up to making a PR for the docs so they document what you
were missing?
|
I would be happy to. I'd want guidance on how exactly to write the clarification, though. Would it be sufficient to adjust https://mypy.readthedocs.io/en/latest/running_mypy.html#how-imports-are-found by following the sentence
with
? I confirmed the above description by calling MyPy on "main.py" (no .\) from src. It was still able to resolve the imports from main, so it does consider the cwd even if it's not explicitly a segment of the given path. |
Yes, that sounds right.
|
I have a project with a structure similar to:
Where
main.py
hasfrom pkg_a.mod_a import ThingFromA
andmod_a
hasfrom pkg_b.mod_b import ThingFromB
If I run MyPy from
project
or fromsrc
, either pointing it atmain.py
or atpkg_a/mod_a.py
, it works fine. If, however, I cd intopkg_a
and run it on./mod_a.py
, it fails to resolve the import to package B.The MyPy docs state that the behavior of absolute import resolution is to walk up the file tree until the first folder that does not have an __init__.py and treat that as the root of the project by adding it to the search path. It seems that there is an additional restriction here that MyPy will only walk as far up as the cwd it is called from. The docs make no mention of this that I can find. Is this an intentional limitation? I can reproduce the problem on a colleague's computer, but hours of googling have not managed to find anyone else complaining about this so I feel like I'm missing something obvious here.
The reason this is an issue is because, while I use VSCode, the team coming onto this project uses PyCharm on Windows, and the MyPy plugin we found for it seems to be calling MyPy from the directory of the file it is checking, meaning PyCharm users are currently stuck without working editor integration (we don't want to use PyCharm's own type checker as we want the type checking on our development machines to behave the same as the type checking in CI (which will be MyPy)).
I am able to reproduce this with the latest MyPy 0.7. The project is Python 3.6.
The text was updated successfully, but these errors were encountered: