5

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)]
KFL
  • 17,162
  • 17
  • 65
  • 89
  • It would be useful if you could provide the version of pylint you're using (output of pylint --version), as well as the version of py.test. – gurney alex May 18 '12 at 10:16
  • @gurneyalex Hi Gurney, I've updated the original question to including the version info. Thanks. – KFL May 19 '12 at 23:08

2 Answers2

3

You can silence this in a pylintrc file with: ignored-classes=pytest

Danny Staple
  • 7,101
  • 4
  • 43
  • 56
  • This answer would have gotten my upvote but you left out some information I would have liked to see. The correct section for the option: `ignored-classes=pytest` should be put under `[TYPECHECK]` in a pylintrc file. Also, a pylintrc file can be put in a few different locations, as exemplified by this post: http://stackoverflow.com/a/16273555/955014 – imolit Dec 12 '14 at 13:04
2

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)
gurney alex
  • 13,247
  • 4
  • 43
  • 57
  • 3
    Hey Alex. pytest does not use the "pylib" dynamic import logic anymore, since a couple of years, version 2.0 actually. However, plugins are allowed to add to the "pytest" namespace which is actually happening for "pytest.raises". – hpk42 May 23 '12 at 19:43
  • Thanks for the precision @hpk42. Dynamically adding to the pytest namespace is however a showstopper for pylint's static analysis, so the conclusions in my answer above still hold. – gurney alex May 28 '12 at 06:50
  • Is there a way for pytest to inform pylint about its namespace? – hpk42 Jun 03 '12 at 12:45
  • 2
    @hpk42 this would be through an astng plugin. See http://www.logilab.org/blogentry/78354 for a short intro on the topic. The best place to get help is on python-projects@lists.logilab.org but you may also want to try #pylint on freenode – gurney alex Jun 04 '12 at 06:39
  • 1
    See https://bitbucket.org/pfctdayelise/pylint-pytest/ for a project which aims to provide a pylint plugin for py.test. – flub Feb 05 '14 at 14:24
  • @hpk42 Wouldn't passing [`--extension-pkg-whitelist=pytest`](https://pylint.readthedocs.io/en/latest/technical_reference/features.html) help here? – Zev Spitz Nov 20 '17 at 05:34