pylint complains on py.test: "Module 'pytest' has no 'raises' member"

pylint, pytest, python

Solution

Last time I looked pylib does some heavy dynamic in low level python stuff, such as completely redefining the import code. It is very likely that this completely baffles pylint/astng, and prevents it from getting what is inside the pytest module: pylint/astng does not import the code it analyzes, it parses it, meaning that stuff which is dynamically initialized at import time will usually go unnoticed, which in turn generates false positives such as the one you report.

From there, you face the following choices:

- use another unittest framework, less dynamic than py.test

- silence the warnings / errors on your test code manually

- use another linter which is happier than pylint on py.test (I'm interested to know how pychecker / pyflakes fare on that code)

- write the astng plugin which will help astng grok the pylib tricks and submit it as a patch to the astng maintainers (and get extra credit from that)

Problem

With the following code: ``` import pytest def test_a(): with pytest.raises(Exception): 1/0 ``` If I run pylint on it, it will make a complain that "raises" is not a member of module pytest: ``` E: 3,9:test_a: Module 'pytest' has no 'raises' member ``` Which is obviously not true. Any idea why pylint is making such a mistake? Is this a known bug? py.test version: ``` > py.test --version This is py.test version 2.2.3, imported from C:\Python27\lib\site-packages\pytest.pyc ``` PyLint version: ``` > 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)] ```

Original source

Related problems