rules_mypy

Bazel rules for the Python linter mypy.

Setup

First ensure rules_venv is setup by referring to rules_venv setup.

Next, the mypy rules work mostly off of toolchains which are used to provide the necessary python targets (aka mypy) for the process wrappers. Users will want to make sure they have a way to get the necessary python dependencies. Tools such as req-compile can provide these.

With the appropriate dependencies available, a py_mypy_toolchain will need to be configured:

load("@rules_venv//python:defs.bzl", "py_library")
load("@rules_venv//python/mypy:defs.bzl", "py_mypy_toolchain")

py_library(
    name = "mypy_deps",
    deps = [
        "@pip_deps//:mypy",

        # Types libraries can also be added here.
    ],
)

py_mypy_toolchain(
    name = "toolchain_impl",
    mypy = ":mypy_deps",
    visibility = ["//visibility:public"]
)

toolchain(
    name = "toolchain",
    toolchain = ":toolchain_impl",
    toolchain_type = "@rules_venv//python/mypy:toolchain_type",
    visibility = ["//visibility:public"]
)

This toolchain then needs to be registered in the MODULE.bazel file.

register_toolchains("//tools/python/mypy:toolchain")

From here, py_mypy_test and the py_mypy_aspect should now be usable. Both of these rules use a global flag to determine which mypy configuration file to use with in actions. The following snippet can be added to the .bazelrc file to chose the desired configuration file

build --@rules_mypy//python/mypy:config=//:.mypyrc.toml

Note that these files will need to be available via exports_files

Rules

Aspects

py_mypy_test

load("@rules_venv//python/mypy:defs.bzl", "py_mypy_test")

py_mypy_test(name, config, target)

A rule for running mypy on a Python target.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
configThe config file (mypy.ini) containing mypy settings.Labeloptional"@rules_venv//python/mypy:config"
targetThe target to run mypy on.Labelrequired

py_mypy_toolchain

load("@rules_venv//python/mypy:defs.bzl", "py_mypy_toolchain")

py_mypy_toolchain(name, mypy)

A toolchain for the mypy formatter rules.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
mypyThe mypy py_library to use with the rules.Labelrequired

py_mypy_aspect

load("@rules_venv//python/mypy:defs.bzl", "py_mypy_aspect")

py_mypy_aspect()

An aspect for running mypy on targets with Python sources.

ASPECT ATTRIBUTES

ATTRIBUTES