Usage

  1. Install pyglint.
  2. Write a linter.

A checker takes a node and yields Problem objects.

class pyglint.Problem(name, text, explanation, id)[source]

A problem found by a checker.

Parameters:
  • name (str) – The name of the problem. Usually 2-4 words, hyphenated.
  • text – The message text for display to the user. str.format() syntax is
  • Usually one short sentence. (supported.) –
  • explanation (str) – Prose description of the problem. Usually a few sentences.

Define a Problem beforehand with CheckerGroup.problem() and reference it with CheckerGroup.check().

class pyglint.CheckerGroup(name, checkers=NOTHING, problems=NOTHING, id_prefix='E')[source]

The main object for defining linters with Pyglint.

check(node_type)[source]

Check for one or more pre-defined Problem s.

Parameters:node_type (Type[NodeNG]) – The checker will be invoked with each instance of the given node type that pylint finds.
problem(name, text, explanation)[source]

Define a reusable Problem.

import astroid

import pyglint


group = pyglint.CheckerGroup("mylinter")


BAD_NAME = group.problem(
    name="bad-name",
    text="The name '{name}' is against the guidelines.",
    explanation="It's a good idea to have a useful and descriptive name. For example, Counter instead of ctr.",
)

IMPORT_FROM = group.problem(
    "import-from",
    text="`from ... import` is not allowed.",
    explanation="Namespaces are one honkin' great idea.",
)


@group.check(astroid.node_classes.Name)
def find_short_names(checker, node):
    if len(node.name) < 4:
        yield pyglint.message(problem=BAD_NAME, node=node, name=node.name)


@group.check(astroid.node_classes.ImportFrom)
def find_import_from(checker, node):
    yield pyglint.message(problem=IMPORT_FROM, node=node)


def register(linter):
    """Register checkers."""
    checker = pyglint.make_pylint_checker(group)
    linter.register_checker(checker(linter))
  1. Register it with Pylint.
def register(linter):
    """Register checkers."""
    checker = pyglint.make_pylint_checker(chk)
    linter.register_checker(checker(linter))
  1. Run Pylint with it.

    python -m pylint --load-plugins examples.mylinter examples/to-be-linted.py
    

Or enable it in your Pylint configuration file.

# .pylintrc
load-plugins=examples.mylinter