42

We have a Python project laid out like this:

project/
├── .pylintrc
├── module1.py
├── module2.py
└── tests/
    ├── test_module1.py
    └── test_module2.py

Our unit and function tests reside in the folder called tests/. When it comes to tests the pylint warnings missing-docstring, invalid-name and protected-access are not relevant. On the other hand, these warnings are very useful for the actual code in the project.

My question is whether it is possible to add ignores for missing-docstring, invalid-name and protected-access in the .pylintrc-file that apply to modules in the tests/-folder only?

If possible, we do not want to add #-disables for these warnings to every test-module inside the folder.

imolit
  • 7,743
  • 3
  • 25
  • 29

2 Answers2

4

As far as I'm aware you can't disable specific warnings for entire directories or files.

However, you can disable all warnings for specific directories using the following on the command line:

--ignore=<file[,file]>

The file here can be a directory.

Personally, and I know you said you'd rather not, I'd add a disable to the top of each file.

  • 19
    As I said, I want to disable specific warnings for specific folders and/or modules, not *all* warnings. It appears that it's not possible to do, however. Apparently, there's an issue on Github for it: https://github.com/PyCQA/pylint/issues/618 – imolit Mar 29 '16 at 09:23
  • The pylint issue is quite old. No official solution yet. Maybe use flake8. Also see https://github.com/pylint-dev/pylint/issues/3767#issuecomment-1319916278 – Stefan Apr 14 '23 at 08:09
4

Yes, you can create .pylintrc in the tests folder, and another in the project folder.

Add tests to the "ignore" section of the project

[MASTER]
ignore=tests

See: https://docs.pylint.org/en/1.6.0/run.html

Then run separately:

pylint project
pylint project/tests
Erik Aronesty
  • 11,620
  • 5
  • 64
  • 44
  • 5
    The problem with that approach is that if you want to enable other pylint checks on your test code, you have to duplicate your configuration. – Christopher Barber May 07 '21 at 14:54
  • 2
    Also, I think that many IDEs will not be able to switch between the two different `.pylintrc` files depending on the current file. – Kurt Bourbaki Oct 28 '21 at 09:25