Hey! These docs are for version 2.8, which is no longer officially supported. Click here for the latest version, 2.11!

Add a typechecker

How to add a new typechecker to the check goal.

Adding a typechecker is almost identical to adding a linter, except for these differences:

  1. Subclass CheckRequest from pants.core.goals.check, rather than LintRequest. Register a UnionRule(CheckRequest, CustomCheckRequest).
  2. Return CheckResults in your rule—which is a collection of CheckResult objects—rather than returning LintResults. Both types are defined in pants.core.goals.check.

The rule will look like this:

from dataclasses import dataclass

from pants.core.goals.check import CheckRequest, CheckResult, CheckResults
from pants.engine.target import FieldSet
from pants.engine.rules import collect_rules, rule
from pants.engine.unions import UnionRule
from pants.util.logging import LogLevel


@dataclass(frozen=True)
class MyPyFieldSet(FieldSet):
    required_fields = (PythonSourceField,)

    source: PythonSourceField


class MyPyRequest(CheckRequest):
    field_set_type = MyPyFieldSet


@rule(desc="Typecheck using MyPy", level=LogLevel.DEBUG)
async def mypy_typecheck(request: MyPyRequest, mypy: MyPy) -> CheckResults:
    if mypy.skip:
        return CheckResults([], checker_name="MyPy")
    ...
    return CheckResults(
        [CheckResult.from_fallible_process_result(result)], checker_name="MyPy"
    )

def rules():
    return [*collect_rules(), UnionRule(CheckRequest, MyPyRequest)]

Refer to Add a linter. See pants/backend/python/typecheck/mypy/rules.py for an example of MyPy.


Did this page help you?