Adding Linters to Django Project

Problem: I needed a poor-person’s solution to static analyzing / linting our Django application.

Solution: Added tests that run pycodestyle and pylint.

The tests:

"""Test project"""
import os
from django.test import TestCase


class LinterTest(TestCase):
"""Test project against linters"""
def test_project_against_pycodestyle(self):
"""Test project against pycodestyle"""
pass_linter = 0
self.assertEqual(
os.WEXITSTATUS(os.system('pycodestyle . -qq')),
pass_linter
)

def test_source_code_against_pylint(self):
"""Test project against Pylint"""
pass_linter = 0
self.assertEqual(
os.WEXITSTATUS(os.system(
'find . -iname "*.py" |'
'xargs pylint --load-plugins pylint_django >/dev/null 2>&1'
)),
pass_linter
)

Notes:

The plugin pylint-django helps avoid findings when working the Django framework.

requirements.txt:

pycodestyle>=2.3.1
pylint>=1.7.1
pylint-django>=0.7.2
Like what you read? Give Zhao Li a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.