pylint抱怨py.test:"模块'pytest'没有'加注'成员"

KFL*_*KFL 5 python pylint pytest

使用以下代码:

import pytest
def test_a():
    with pytest.raises(Exception):
        1/0
Run Code Online (Sandbox Code Playgroud)

如果我对它运行pylint,它会抱怨"raises"不是模块pytest的成员:

E:  3,9:test_a: Module 'pytest' has no 'raises' member
Run Code Online (Sandbox Code Playgroud)

这显然不是真的.知道为什么pylint犯了这样的错误吗?这是一个已知的错误?

py.test版本:

> py.test --version
This is py.test version 2.2.3, imported from C:\Python27\lib\site-packages\pytest.pyc
Run Code Online (Sandbox Code Playgroud)

PyLint版本:

> pylint --version
No config file found, using default configuration
pylint 0.25.1,
astng 0.23.1, common 0.57.1
Python 2.7.2 (default, Jun 24 2011, 12:22:14) [MSC v.1500 64 bit (AMD64)]
Run Code Online (Sandbox Code Playgroud)

gur*_*lex 2

上次我看 pylib 在低级 python 中做了一些繁重的动态工作,例如完全重新定义导入代码。这很可能完全困扰 pylint/astng,并阻止它获取 pytest 模块内部的内容: pylint/astng 不会导入它分析的代码,而是解析它,这意味着在导入时动态初始化的内容将通常会被忽视,从而产生误报,例如您报告的情况。

从那时起,您将面临以下选择:

  • 使用另一个单元测试框架,比 py.test 动态性低
  • 手动消除测试代码上的警告/错误
  • 在 py.test 上使用另一个比 pylint 更快乐的 linter (我有兴趣知道 pychecker / pyflakes 在该代码上的表现如何)
  • 编写 astng 插件,它将帮助 astng 掌握 pylib 技巧,并将其作为补丁提交给 astng 维护者(并从中获得额外的积分)

  • 嘿亚历克斯。pytest 不再使用“pylib”动态导入逻辑,实际上是几年前的 2.0 版本。但是,插件可以添加到“pytest”命名空间,这实际上发生在“pytest.raises”中。 (3认同)
  • @hpk42 这将通过 astng 插件实现。有关该主题的简短介绍,请参阅 http://www.logilab.org/blogentry/78354。获得帮助的最佳地点是 python-projects@lists.logilab.org,但您可能还想在 freenode 上尝试 #pylint (2认同)