Incorporating linters into your service will enforce a coding standard and prevent errors from getting merged into your codebase. The baseplate.lint module consists of custom Pylint checkers which add more lint to Pylint. These lints are based on bugs found at Reddit.


Getting Started

Install Pylint and ensure you have it and its dependencies added to your requirements-dev.txt file.

Follow the Pylint user guide for instructions to generate a default pylintrc configuration file and run Pylint.

Adding Custom Checkers

In your pylintrc file, add baseplate.lint to the [MASTER] load-plugins configuration.

# List of plugins (as comma separated values of python modules names) to load,
# usually to register additional checkers.

This will allow you to use all the custom checkers in the baseplate.lint module when you run Pylint.

Custom Checkers List

  • W9000: no-database-query-string-format

Creating Custom Checkers

If there is something you want to lint and a checker does not already exist, you can add a new one to baseplate.lint.

The following is an example checker you can reference to create your own.

# Pylint documentation for writing a checker:
# This is an example of a Pylint AST checker and should not be registered to use
# In an AST (abstract syntax tree) checker, the code will be represented as nodes of a tree
# We will use the astroid library: to visit and leave nodes
# Libraries needed for an AST checker
from astroid import nodes
from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker
from pylint.lint import PyLinter

# Basic example of a Pylint AST (astract syntax tree) checker
# Checks for variables that have been reassigned in a function. If it finds a reassigned variable, it will throw an error
class NoReassignmentChecker(BaseChecker):
    __implements__ = IAstroidChecker

    # Checker name
    name = "no-reassigned-variable"
    # Set priority to -1
    priority = -1
    # Message dictionary
    msgs = {
        # message-id, consists of a letter and numbers
        # Letter will be one of following letters (C=Convention, W=Warning, E=Error, F=Fatal, R=Refactoring)
        # Numbers need to be unique and in-between 9000-9999
        # Check
        # for numbers that are already in use
        "W9001": (
            # displayed-message shown to user
            "Reassigned variable found.",
            # message-symbol used as alias for message-id
            # message-help shown to user when calling pylint --help-msg
            "Ensure variables are not reassigned.",

    def __init__(self, linter: PyLinter = None):
        self.variables: set = set()

    # The following two methods are called for us by pylint/astroid
    # The linter walks through the tree, visiting and leaving desired nodes
    # Methods should start with visit_ or leave_ followed by lowercase class name of nodes
    # List of available nodes:

    # Visit the Assign node:
    def visit_assign(self, node: nodes) -> None:
        for variable in node.targets:
            if not in self.variables:
                self.add_message("non-unique-variable", node=node)

    # Leave the FunctionDef node:
    def leave_functiondef(self, node: nodes) -> nodes:
        self.variables = set()
        return node

Add a test to the baseplate test suite following this example checker test.

# Libraries needed for tests
import astroid
import pylint.testutils

from baseplate.lint import example_plugin

# CheckerTestCase creates a linter that will traverse the AST tree
class TestNoReassignmentChecker(pylint.testutils.CheckerTestCase):
    CHECKER_CLASS = example_plugin.NoReassignmentChecker

    # Use astroid.extract_node() to create a test case
    # Where you put #@ is where the variable gets assigned
    # example, assign_node_a = test = 1, assign_node_b = test = 2
    def test_finds_reassigned_variable(self):
        assign_node_a, assign_node_b = astroid.extract_node(
        test = 1 #@
        test = 2 #@

            pylint.testutils.Message(msg_id="reassigned-variable", node=assign_node_a)

    def test_ignores_no_reassigned_variable(self):
        assign_node_a, assign_node_b = astroid.extract_node(
        test1 = 1 #@
        test2 = 2 #@

        with self.assertNoMessages():

    def test_ignores_variable_outside_function(self):
        func_node, assign_node_a, assign_node_b = astroid.extract_node(
        def test1(): #@
            test = 1 #@

        def test2():
            test = 2 #@

        with self.assertNoMessages():

Register your checker by adding it to the register() function:

from pylint.lint import PyLinter

from baseplate.lint.db_query_string_format_plugin import NoDbQueryStringFormatChecker

def register(linter: PyLinter) -> None:
    checker = NoDbQueryStringFormatChecker(linter)

Lastly, add your checker message-id and name to Custom Checkers List.