Skip to content

mypy not finding modules located above the current working directory #6660

Closed
@ada-waffles

Description

@ada-waffles

I have a project with a structure similar to:

  • project
    • mypy.ini
    • src
      • main.py
      • pkg_a
        • __init__.py
        • mod_a.py
      • pkg_b
        • __init__.py
        • mod_b.py

Where main.py has from pkg_a.mod_a import ThingFromA and mod_a has from pkg_b.mod_b import ThingFromB

If I run MyPy from project or from src, either pointing it at main.py or at pkg_a/mod_a.py, it works fine. If, however, I cd into pkg_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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions