简体   繁体   English

如何为 pytest 灯具指定 mypy 类型

[英]How to specify mypy types for pytest fixtures

I am trying to specify mypy type hints for the pytest native fixtures I am using in my test project eg:我正在尝试为我在测试项目中使用的 pytest 本机夹具指定 mypy 类型提示,例如:

import pytest

def pytest_configure(config):
    # Do something useful here

The config fixture returns a _pytest.config.Config object. config夹具返回_pytest.config.Config object。 If I try to model this naively:如果我天真地尝试 model 这个:

import pytest

def pytest_configure(config: Config) -> None:
    # Do something useful here

I receive a mypy error: conftest.py:3: error: Name 'Config' is not defined [name-defined]我收到一个 mypy 错误: conftest.py:3: error: Name 'Config' is not defined [name-defined]

I could do from _pytest.config import Config , but this doesn't seem to be a good way, because _pytest is private.我可以from _pytest.config import Config ,但这似乎不是一个好方法,因为 _pytest 是私有的。 Another option would be to ignore the type with # type: ignore .另一种选择是使用# type: ignore If this is the recommended way I would of course do this, but I wonder if there is a better option.如果这是推荐的方式,我当然会这样做,但我想知道是否有更好的选择。

I have the same issues in with any kind of pytest native fixtures I use, eg request which is used for parameterized fixtures.对于我使用的任何类型的 pytest 本地装置,我都有同样的问题,例如用于参数化装置的request This would be a _pytest.fixtures.FixtureRequest .这将是一个_pytest.fixtures.FixtureRequest

Importing from _pytest.config from _pytest.config导入

Since pytest doesn't currently export Config (as of 6.2), the only way for typing is to use from _pytest.config import Config .由于pytest当前不导出Config (从 6.2 开始),因此键入的唯一方法是使用from _pytest.config import Config This is how I also type config , as can be seen eg in this question of mine :这也是我输入config的方式,例如在我的这个问题中可以看到:

from _pytest.config import Config

def pytest_configure(config: Config) -> None:
    ...

You can track the typing progress in this pytest issue: #7469 .您可以在此pytest问题中跟踪打字进度: #7469

Custom type stubs自定义类型存根

You can also introduce a small custom type stub that hides the reexport.您还可以引入一个隐藏重新导出的小型自定义类型存根。 It's questionable whether it will be useful here, only worth to mention for an alternative solution.它是否在这里有用是值得怀疑的,只值得一提的是替代解决方案。 If you create a file _typeshed/pytest.pyi with the following contents:如果您创建一个包含以下内容的文件_typeshed/pytest.pyi

from typing import Any
from _pytest.config import Config as Config

def __getattr__(name: str) -> Any: ...  # incomplete

and make it accessible to mypy in mypy.ini :并使其对mypy中的mypy.ini可访问:

[mypy]
mypy_path = _typeshed

Now you can import from pytest import Config at least in type checking mode - the runtime import will still fail.现在您至少可以在类型检查模式下from pytest import Config - 运行时导入仍然会失败。 So the imports would look like所以进口看起来像

from typing import Any, TYPE_CHECKING

if TYPE_CHECKING:
    from pytest import Config
else:
    Config = Any


def pytest_configure(config: Config) -> None:
    pass

The only benefit of that solution is that the private import is now hidden;该解决方案的唯一好处是私有导入现在被隐藏了; I'd still go with the private import though.不过,我仍然会使用私人进口 go 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM