配置中的 mypy_path 没有被拾取

问题描述 投票:0回答:2

我有这样的结构:

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

一些简单的文件:

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

当我从包外部运行时,所以../mypy_minrepro,以下命令:

mypy -m mypy_minrepro.test.main

我得到:

mypy_minrepro est\main.py:8: 错误:找不到实现或 名为“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

所以没有找到stubs目录。现在我不想真正移动这些目录,那么我该如何修复这个结构呢?

python python-3.x mypy
2个回答
3
投票

好的,我已经理解了 mypy_path 语义。 2个问题:

首先,除非使用

--config-file
传递,否则 mypy 将在调用命令的当前工作目录(然后在其他全局位置)中查找 mypy.ini 。 来自文档(强调我自己的):

默认设置从 mypy.ini、.mypy.ini、pyproject.toml 读取, 或 setup.cfg

在当前目录。设置覆盖 mypy 的 内置默认值和命令行标志可以覆盖设置。

这就是为什么在详细日志 (-v) 中我得到

Config File: Default

 而不是 
Config File: mypy.ini

第二个问题:如果

mypy_path

 是相对的(例如 
mypy_path = test/stubs
),那么它是相对于运行 mypy 命令的当前目录。因此,即使 mypy 找到我的配置文件(通过手动传递它),如果我从父目录或子目录运行它,解析的
mypy_path
也会有所不同。

要解决此问题,请使用

$MYPY_CONFIG_FILE_DIR/stubs

 定义相对于配置文件(无论其位于何处)的路径。


0
投票
对于那些来到这里寻找答案的人来说,这是另一个花絮/可能性。格里多的自我回答给了我所需的线索。

我正在使用 PyCharm、VSCode 和 lintrunner 来运行 MyPy(不要问!)。

    PyCharm
来自 Roberto Leinardi 的功能更强大的 PyCharm MyPy 插件允许您在单个文件或模块上运行 MyPy。不幸的是,我们根本不清楚它是从定义源文件或模块的目录运行它,而不是从项目目录或 MyPy

.ini

 文件的位置运行它。这意味着 ini 文件中的文件名被解析,嗯,不是按照您希望的方式。 :-) 我正在使用共享的 
.mypy.ini
,因此我在 
mypy_path
 中的文件名前面加上了 
"$MYPY_CONFIG_FILE_DIR"
,以防我的同事的路径中有空格。

    Visual Studio Code(又名 VSCode)
不幸的是,引号破坏了来自 Matan Gover 的 VSCode 功能更强大的 mypy 插件。

叹气。

© www.soinside.com 2019 - 2024. All rights reserved.