【发布时间】:2021-04-06 00:53:10
【问题描述】:
我正在尝试为我在测试项目中使用的 pytest 本机装置指定 mypy 类型提示,例如:
import pytest
def pytest_configure(config):
# Do something useful here
config 夹具返回一个 _pytest.config.Config 对象。如果我尝试天真地建模:
import pytest
def pytest_configure(config: Config) -> None:
# Do something useful here
我收到一个 mypy 错误:
conftest.py:3: error: Name 'Config' is not defined [name-defined]
我可以做from _pytest.config import Config,但这似乎不是一个好方法,因为 _pytest 是私有的。
另一种选择是忽略带有# type: ignore 的类型。如果这是推荐的方式,我当然会这样做,但我想知道是否有更好的选择。
我使用的任何类型的 pytest 本机装置都有相同的问题,例如request 用于参数化夹具。这将是_pytest.fixtures.FixtureRequest。
【问题讨论】:
-
from _pytest.config import Config是要走的路,因为由于某些原因,Config没有被pytestATM 导出。不过,请随意open a new issue。对于request的输入,请查看my other recent answer 和相关的issue #8073。 -
谢谢,如果你把它作为答案,我会接受的。已打开github.com/pytest-dev/pytest/issues/8202