Different Pylint Standards for Test Files

Viewed 396

I have a typical Python codebase with hundreds of .py files in dozens of directories, and dozens of /tests/ directories filled with test_whatever.py files (that I run using pytest).

But, honestly, I have a much lower bar for caring about pylint things in test files than in real code. How do I ignore these four things for files in /tests/ folders?

  1. missing-module-docstring
  2. missing-class-docstring
  3. missing-function-docstring
  4. protected-access

The only one I've managed to mostly solve is the function docstring one, by putting this in my pylintrc:

no-docstring-rgx=^_|^test_

Is it possible to fix the others? (I cannot fully exclude test files from linting.)

1 Answers

you can use tox which is a generic virtualenv management and test command line tool. tox helps you :

  • checking that your package installs correctly in different python versions
  • running your tests in each of the environments
  • acting as a frontend to Continuous Integration servers

you can read more about tox in their official website

install tox with pip install tox

add tox.ini file on the root level. see project structure in the image below

enter image description here

in the tox.ini a basic configuration to run tests and pylint would be like this:

[tox]
minversion = 2.0
envlist = py36, pylint
skipsdist = True

[testenv]
commands = {envbindir}/pytest {posargs}
deps = -r{toxinidir}/requirements.txt
       -r{toxinidir}/dev-requirements.txt
recreate = False
passenv = *

[testenv:pylint]
basepython=python3.6
deps = -r{toxinidir}/requirements.txt
       -r{toxinidir}/dev-requirements.txt
commands=pylint src
         pylint --disable=missing-module-docstring,missing-class-docstring tests

as you can see, I split pylint to 2 commands which let me control how to lint my src and tests differently.

to run tox (tests + lint) just run tox in the terminal from tox.ini directory.

if you would like only to run pylint then you can run tox -e pylint

Related