首页 > 解决方案 > mypy 跳过分析自己导入的模块并引发错误

问题描述

我似乎无法弄清楚为什么 mypy 在自己的模块导入时抛出错误。

$ python -m mypy ./reader/api/response/response.py
reader/api/response/response.py:5: error: Skipping analyzing 'reader.api.response.answer': found module but no type hints or library stubs
reader/api/response/response.py:5: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 1 source file)

response.py从同一存储库中的另一个模块导入一个类

from __future__ import annotations

from typing import List
from dataclasses import dataclass
from reader.api.response.answer import Answer  # <- mypy fails here.


@dataclass
class ReaderResponse:
...

的内容answer.py是这样的:

from __future__ import annotations

from dataclasses import dataclass, fields


@dataclass
class Answer:

    answer: str
    score: float
    source: str
    url: str
    type: str

...

为什么 mypy 认为我导入的模块中没有类型提示?我已在此处阅读有关缺少导入的文档,但不明白它是如何应用的。它不是第三方库。这是我自己的模块。

任何线索我在这里做错了什么?

谢谢你。

编辑:所以从命名空间包切换到常规包,并切换到相对导入(点导入)解决了我的问题。不完全确定为什么...

标签: python-3.xmypypython-dataclasses

解决方案


推荐阅读