为什么 mypy 状态 "found module but no typehints..." 即使实际上有类型提示?

Why does mypy state "found module but no typehints..." eventhough there are actually type hints?

我有一项服务可以从包含大量包和模块的目录中导入模块。该目录中的大多数模块都带有类型提示注释。

现在,我想使用 mypy 检查服务中的类型提示。但出于某种原因,mypy 无法识别该目录中的提示,我收到很多错误,例如:

service/some.py:16: error: Skipping analyzing "directory.some_package.some_arbitrary_module": found module but no type hints or library stubs

我也已经尝试 运行 stubgen some_arbitrary_module.py 并将生成的 some_arbitrary_module.pyi 文件放在同一目录中,以检查 [=] 中的类型提示是否有问题15=]..但我得到了同样的错误。

是否还有其他原因会导致此类错误?

从评论中,您发现您正在导入的模块来自可安装的依赖项。如果您想使用该依赖项的类型提示,它需要每个 PEP 561 - Packaging Type Information.

有一个 py.typed

Package maintainers who wish to support type checking of their code MUST add a marker file named py.typed to their package supporting typing.

以下是来自 mypy 文档的更多信息 Creating PEP 561 compatible packages:

For example, here is a typical directory structure:

setup.py package_a/
    __init__.py
    lib.py
    py.typed

The setup.py file could look like this:

from distutils.core import setup

setup(
    name="SuperPackageA",
    author="Me",
    version="0.1",
    package_data={"package_a": ["py.typed"]},
    packages=["package_a"]
)