Skip to main content
Version: 2.25 (dev)

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 LintTargetsRequest.
  2. Register a UnionRule(CheckRequest, CustomCheckRequest) in your rules() instead of unpacking <RequestType>.rules(...).
  3. Return CheckResults in your rule—which is a collection of CheckResult objects—rather than returning a LintResult.

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
name = "mypy"


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

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.