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:
- Subclass
CheckRequest
frompants.core.goals.check
, rather thanLintTargetsRequest
. - Register a
UnionRule(CheckRequest, CustomCheckRequest)
in yourrules()
instead of unpacking<RequestType>.rules(...)
. - Return
CheckResults
in your rule—which is a collection ofCheckResult
objects—rather than returning aLintResult
.
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.