isort
Bazel rules for the Python formatter isort.
Setup
First ensure rules_venv
is setup by referring to rules_venv setup.
Next, the rules_venv
rules work mostly off of toolchains which are used to provide the necessary python targets (aka isort
)
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_isort_toolchain will need to be configured:
load("@rules_venv//python/isort:defs.bzl", "py_isort_toolchain")
py_isort_toolchain(
name = "toolchain_impl",
isort = "@pip_deps//:isort",
visibility = ["//visibility:public"]
)
toolchain(
name = "toolchain",
toolchain = ":toolchain_impl",
toolchain_type = "@rules_venv//python/isort:toolchain_type",
visibility = ["//visibility:public"]
)
This toolchain then needs to be registered in the MODULE.bazel
file.
register_toolchains("//tools/python/isort:toolchain")
Usage
Python code can be formatted using the following command:
bazel run @rules_venv//python/isort
In addition to this formatter, a check can be added to bazel build
invocations using the py_isort_aspect
aspect. Simply add the following to a .bazelrc
file to enable this check.
build --aspects=@rules_venv//python/isort:defs.bzl%py_isort_aspect
build --output_groups=+py_isort_checks
Sections and Ordering
Of all isort sections, FIRSTPARTY
and
THIRDPARTY
follow unique rules to ensure ordering makes sense on a per-target basis.
First party
- Direct source dependencies passed to the
srcs
attribute. - Repository relative packages.
Third party
- Other
py_*
targets (thedeps
attribute). Note thatdeps
which use repo absolute import paths will be considered first party.
Tips
Isort is sensitive to the legacy_create_init
attribute on python rules. For more correct
and consistent behavior, this value should always be 0
or if the default of -1
is set, the
--incompatible_default_to_explicit_init_py
flag should be added to the workspace's
.bazelrc
file to ensure the behavior is disabled.
Rules
Aspects
py_isort_test
load("@rules_venv//python/isort:defs.bzl", "py_isort_test") py_isort_test(name, config, target)
A rule for running isort on a Python target.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
config | The config file (isort.cfg) containing isort settings. | Label | optional | "@rules_venv//python/isort:config" |
target | The target to run isort on. | Label | required |
py_isort_toolchain
load("@rules_venv//python/isort:defs.bzl", "py_isort_toolchain") py_isort_toolchain(name, isort)
A toolchain for the isort formatter rules.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
isort | The isort py_library to use with the rules. | Label | required |
py_isort_aspect
load("@rules_venv//python/isort:defs.bzl", "py_isort_aspect") py_isort_aspect()
An aspect for running isort on targets with Python sources.
ASPECT ATTRIBUTES
ATTRIBUTES