Greedo
Greedo

Reputation: 5543

Stuck with mypy_path in config not getting picked up

I have this structure:

mypy_minrepro
|   mypy.ini
|   __init__.py
|   
+---test
|   |   main.py
|   |   __init__.py
|   |   
|   \---stubs
|       \---untyped
|               main.pyi
|               __init__.pyi
|               
\---untyped
        main.py
        __init__.py

With some simple files:

mypy.ini

[mypy]
mypy_path = test/stubs

untyped/main.py

def foo(x):
    return x ** 2

test/stubs/untyped/main.pyi

def foo(x: float) -> float: ...

test/main.py

from untyped.main import foo

if __name__ == "__main__":
    foo("invalid") # I want mypy to spot this error using my .pyi file

When I run from outside the package, so ../mypy_minrepro, the following command:

mypy -m mypy_minrepro.test.main

I get:

mypy_minrepro\test\main.py:8: error: Cannot find implementation or library stub for module named "untyped.main"

LOG:  Mypy Version:           0.910
LOG:  Config File:            Default
LOG:  Configured Executable:  C:\Dev\anaconda3\envs\spy\python.exe
LOG:  Current Executable:     C:\Dev\anaconda3\envs\spy\python.exe
LOG:  Cache Dir:              .mypy_cache
LOG:  Compiled:               True
LOG:  Exclude:
LOG:  Found source:           BuildSource(path='C:\\Dev\\Code\\mypy_minrepro\\test\\main.py', module='mypy_minrepro.test.main', has_text=False, base_dir=None) 
LOG:  Could not load cache for mypy_minrepro.test.main: mypy_minrepro\test\main.meta.json
LOG:  Metadata not found for mypy_minrepro.test.main
LOG:  Parsing C:\Dev\Code\mypy_minrepro\test\main.py (mypy_minrepro.test.main)
LOG:  Metadata fresh for mypy_minrepro.test: file C:\Dev\Code\mypy_minrepro\test\__init__.py
LOG:  Metadata fresh for mypy_minrepro: file C:\Dev\Code\mypy_minrepro\__init__.py
LOG:  Metadata fresh for builtins: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\builtins.pyi
LOG:  Metadata fresh for sys: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\sys.pyi
LOG:  Metadata fresh for types: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\types.pyi
LOG:  Metadata fresh for _typeshed: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\_typeshed\__init__.pyi
LOG:  Metadata fresh for ast: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\ast.pyi
LOG:  Metadata fresh for io: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\io.pyi
LOG:  Metadata fresh for typing: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\typing.pyi
LOG:  Metadata fresh for typing_extensions: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\typing_extensions.pyi
LOG:  Metadata fresh for importlib.abc: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\importlib\abc.pyi
LOG:  Metadata fresh for importlib.machinery: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\importlib\machinery.pyi
LOG:  Metadata fresh for array: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\array.pyi
LOG:  Metadata fresh for mmap: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\mmap.pyi
LOG:  Metadata fresh for os: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\os\__init__.pyi
LOG:  Metadata fresh for _ast: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\_ast.pyi
LOG:  Metadata fresh for codecs: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\codecs.pyi
LOG:  Metadata fresh for collections: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\collections\__init__.pyi
LOG:  Metadata fresh for abc: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\abc.pyi
LOG:  Metadata fresh for importlib: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\importlib\__init__.pyi
LOG:  Metadata fresh for posix: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\posix.pyi
LOG:  Metadata fresh for subprocess: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\subprocess.pyi
LOG:  Metadata fresh for os.path: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\os\path.pyi
LOG:  Metadata fresh for _collections_abc: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\_collections_abc.pyi
LOG:  Metadata fresh for ntpath: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\ntpath.pyi
LOG:  Metadata fresh for genericpath: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\genericpath.pyi
LOG:  Metadata fresh for posixpath: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\posixpath.pyi
LOG:  Loaded graph with 28 nodes (0.084 sec)
LOG:  Found 4 SCCs; largest has 25 nodes
LOG:  Processing 3 queued fresh SCCs
LOG:  Processing SCC singleton (mypy_minrepro.test.main) as inherently stale
mypy_minrepro\test\main.py:8: error: Cannot find implementation or library stub for module named "untyped.main"
mypy_minrepro\test\main.py:8: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
LOG:  Deleting mypy_minrepro.test.main C:\Dev\Code\mypy_minrepro\test\main.py mypy_minrepro\test\main.meta.json mypy_minrepro\test\main.data.json
LOG:  No fresh SCCs left in queue
LOG:  Build finished in 0.199 seconds with 28 modules, and 2 errorsLOG:  Mypy Version:           0.910
LOG:  Config File:            Default
LOG:  Configured Executable:  C:\Dev\anaconda3\envs\spy\python.exe
LOG:  Current Executable:     C:\Dev\anaconda3\envs\spy\python.exe
LOG:  Cache Dir:              .mypy_cache
LOG:  Compiled:               True
LOG:  Exclude:
LOG:  Found source:           BuildSource(path='C:\\Dev\\Code\\mypy_minrepro\\test\\main.py', module='mypy_minrepro.test.main', has_text=False, base_dir=None) 
LOG:  Could not load cache for mypy_minrepro.test.main: mypy_minrepro\test\main.meta.json
LOG:  Metadata not found for mypy_minrepro.test.main
LOG:  Parsing C:\Dev\Code\mypy_minrepro\test\main.py (mypy_minrepro.test.main)
LOG:  Metadata fresh for mypy_minrepro.test: file C:\Dev\Code\mypy_minrepro\test\__init__.py
LOG:  Metadata fresh for mypy_minrepro: file C:\Dev\Code\mypy_minrepro\__init__.py
LOG:  Metadata fresh for builtins: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\builtins.pyi
LOG:  Metadata fresh for sys: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\sys.pyi
LOG:  Metadata fresh for types: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\types.pyi
LOG:  Metadata fresh for _typeshed: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\_typeshed\__init__.pyi
LOG:  Metadata fresh for ast: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\ast.pyi
LOG:  Metadata fresh for io: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\io.pyi
LOG:  Metadata fresh for typing: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\typing.pyi
LOG:  Metadata fresh for typing_extensions: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\typing_extensions.pyi
LOG:  Metadata fresh for importlib.abc: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\importlib\abc.pyi
LOG:  Metadata fresh for importlib.machinery: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\importlib\machinery.pyi
LOG:  Metadata fresh for array: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\array.pyi
LOG:  Metadata fresh for mmap: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\mmap.pyi
LOG:  Metadata fresh for os: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\os\__init__.pyi
LOG:  Metadata fresh for _ast: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\_ast.pyi
LOG:  Metadata fresh for codecs: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\codecs.pyi
LOG:  Metadata fresh for collections: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\collections\__init__.pyi
LOG:  Metadata fresh for abc: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\abc.pyi
LOG:  Metadata fresh for importlib: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\importlib\__init__.pyi
LOG:  Metadata fresh for posix: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\posix.pyi
LOG:  Metadata fresh for subprocess: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\subprocess.pyi
LOG:  Metadata fresh for os.path: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\os\path.pyi
LOG:  Metadata fresh for _collections_abc: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\_collections_abc.pyi
LOG:  Metadata fresh for ntpath: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\ntpath.pyi
LOG:  Metadata fresh for genericpath: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\genericpath.pyi
LOG:  Metadata fresh for posixpath: file C:\Dev\anaconda3\envs\spy\lib\site-packages\mypy\typeshed\stdlib\posixpath.pyi
LOG:  Loaded graph with 28 nodes (0.084 sec)
LOG:  Found 4 SCCs; largest has 25 nodes
LOG:  Processing 3 queued fresh SCCs
LOG:  Processing SCC singleton (mypy_minrepro.test.main) as inherently stale
mypy_minrepro\test\main.py:8: error: Cannot find implementation or library stub for module named "untyped.main"
mypy_minrepro\test\main.py:8: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
LOG:  Deleting mypy_minrepro.test.main C:\Dev\Code\mypy_minrepro\test\main.py mypy_minrepro\test\main.meta.json mypy_minrepro\test\main.data.json
LOG:  No fresh SCCs left in queue
LOG:  Build finished in 0.199 seconds with 28 modules, and 2 errors

So the stubs directory is not found. Now I don't want to really move these directories around, so how can I fix this structure?

Upvotes: 1

Views: 5005

Answers (2)

Raymond Peck
Raymond Peck

Reputation: 11

Here's another tidbit/possibility, for those who land here looking for an answer. Greedo's self-answer gave me the clue I needed.

I'm using PyCharm, VSCode, and lintrunner to run MyPy (don't ask!).

  • PyCharm

The more functional PyCharm MyPy plugin, from Roberto Leinardi, allows you to run MyPy on a single file or module. Unfortunately, it's not at all clear that it runs it from the directory in which the source file or module is defined, not from the project dir or location of the MyPy .ini file. This means that filenames in the ini file are resolved, um, not in the way you might hope. :-) I am working with a shared .mypy.ini, so I prefixed the filenames in the mypy_path with "$MYPY_CONFIG_FILE_DIR" just in case my coworkers had spaces in their path.

  • Visual Studio Code (aka VSCode)

Unfortunately, the quotes break the VSCode more functional mypy plugin, from Matan Gover.

Sigh.

Upvotes: 1

Greedo
Greedo

Reputation: 5543

Ok, so I've understood the mypy_path semantics. 2 problems:

Firstly, unless passed with --config-file, mypy will look for mypy.ini in the current working directory where the command was called from (then in other global places). From the docs (emphasis my own):

By default settings are read from mypy.ini, .mypy.ini, pyproject.toml, or setup.cfg in the current directory. Settings override mypy’s built-in defaults and command line flags can override settings.

This is why in the verbose log (-v) I got Config File: Default rather than Config File: mypy.ini.

Second problem: If mypy_path is relative (e.g. mypy_path = test/stubs) then it is relative to the current directory where mypy command is run. Therefore even if mypy finds my config file (by passing it manually), if I run it from a parent directory or a child directory the resolved mypy_path will be different.

To get around this, use $MYPY_CONFIG_FILE_DIR/stubs to define a path relative to the config file wherever it may be.

Upvotes: 3

Related Questions