Skip to content

mypy checks parent package's init, even though child directory is specified #6861

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

Closed
Peilonrayz opened this issue May 18, 2019 · 2 comments
Closed

Comments

@Peilonrayz
Copy link
Contributor

With nothing but the following two files:

project/src/package/__init__.py

import external

project/src/package/sub_package/__init__.py

def fn(a:int, b:str):
    return a + b

Mypy runs on the first file, even though the path passed is src/package/sub_package/.

project $ mypy src/package/sub_package/
src\package\__init__.py:1: error: Cannot find module named 'external'
src\package\__init__.py:1: note: See https://mypy.readthedocs.io/en/lates/running_mypy.html#missing-imports
src\package\sub_package\__init__.py:2: error: Unsupported operand types for + ("int" and "str")

I think this is strange, as I didn't specify src/package/. It should be noted that src/package/sub_package/__init__.py can be empty, but if it doesn't exist then the following error is returned:

There are no .py[i] files in directory 'src/package/sub_package'

@gvanrossum
Copy link
Member

It's intentional. Mypy crawls up from the given filename(s) until it finds a directory without __init__.py, so it can follows imports. Without following imports, it wouldn't be able to check a lot of code, and without crawling up, it wouldn't be able to follow imports. If you don't want it to follow imports, you can use --follow-imports=error or --follow-imports=skip (but it will still crawl up to determine the file's module name). For full docs see https://mypy.readthedocs.io/en/latest/command_line.html.

@Peilonrayz
Copy link
Contributor Author

@gvanrossum I've read the documentation as well now, and it makes sense to me. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants