Skip to content

Commit

Permalink
Replace Black with Ruff as the Python formatter (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenmj authored Oct 26, 2023
1 parent ddf1bcc commit e4cfbf4
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 118 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ This leads to some minor differences in how they are used in rules_lint.

| Language | Formatter | Linter(s) |
| ------------------------- | --------------------- | ---------------- |
| Python | [Black] | [flake8], [ruff] |
| Python | [ruff] | [flake8], [ruff] |
| Java | [google-java-format] | [pmd] |
| Kotlin | [ktfmt] | |
| JavaScript/TypeScript/TSX | [Prettier] | [ESLint] |
Expand All @@ -63,7 +63,6 @@ This leads to some minor differences in how they are used in rules_lint.

[prettier]: https://prettier.io
[google-java-format]: https://github.com/google/google-java-format
[black]: https://pypi.org/project/black/
[flake8]: https://flake8.pycqa.org/en/latest/index.html
[pmd]: https://docs.pmd-code.org/latest/index.html
[buf lint]: https://buf.build/docs/lint/overview
Expand Down
2 changes: 1 addition & 1 deletion docs/format.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ load("@aspect_rules_lint//format:defs.bzl", "multi_formatter_binary")
multi_formatter_binary(
name = "format",
# register languages, e.g.
# python = "@pip_black//:rules_python_wheel_entry_point_black",
# python = "//:ruff",
)
```

Expand Down
2 changes: 1 addition & 1 deletion docs/ruff.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions example/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,5 @@ java_binary(
runtime_deps = ["@net_sourceforge_pmd"],
)

# We can test that it works with:
# bazel run :ruff -- help
native_binary(
name = "ruff",
src = select({
"@platforms//os:osx": "@ruff_aarch64-apple-darwin//:ruff",
"//conditions:default": "@ruff_x86_64-unknown-linux-gnu//:ruff",
}),
out = "ruff",
visibility = ["//visibility:public"],
)

# bazel run :shellcheck -- --help
shellcheck_binary(name = "shellcheck")
1 change: 0 additions & 1 deletion example/requirements.in
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
black
flake8
52 changes: 0 additions & 52 deletions example/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,6 @@
#
# bazel run //:requirements.update
#
black==23.9.1 \
--hash=sha256:031e8c69f3d3b09e1aa471a926a1eeb0b9071f80b17689a655f7885ac9325a6f \
--hash=sha256:13a2e4a93bb8ca74a749b6974925c27219bb3df4d42fc45e948a5d9feb5122b7 \
--hash=sha256:13ef033794029b85dfea8032c9d3b92b42b526f1ff4bf13b2182ce4e917f5100 \
--hash=sha256:14f04c990259576acd093871e7e9b14918eb28f1866f91968ff5524293f9c573 \
--hash=sha256:24b6b3ff5c6d9ea08a8888f6977eae858e1f340d7260cf56d70a49823236b62d \
--hash=sha256:403397c033adbc45c2bd41747da1f7fc7eaa44efbee256b53842470d4ac5a70f \
--hash=sha256:50254ebfa56aa46a9fdd5d651f9637485068a1adf42270148cd101cdf56e0ad9 \
--hash=sha256:538efb451cd50f43aba394e9ec7ad55a37598faae3348d723b59ea8e91616300 \
--hash=sha256:638619a559280de0c2aa4d76f504891c9860bb8fa214267358f0a20f27c12948 \
--hash=sha256:6a3b50e4b93f43b34a9d3ef00d9b6728b4a722c997c99ab09102fd5efdb88325 \
--hash=sha256:6ccd59584cc834b6d127628713e4b6b968e5f79572da66284532525a042549f9 \
--hash=sha256:75a2dc41b183d4872d3a500d2b9c9016e67ed95738a3624f4751a0cb4818fe71 \
--hash=sha256:7d30ec46de88091e4316b17ae58bbbfc12b2de05e069030f6b747dfc649ad186 \
--hash=sha256:8431445bf62d2a914b541da7ab3e2b4f3bc052d2ccbf157ebad18ea126efb91f \
--hash=sha256:8fc1ddcf83f996247505db6b715294eba56ea9372e107fd54963c7553f2b6dfe \
--hash=sha256:a732b82747235e0542c03bf352c126052c0fbc458d8a239a94701175b17d4855 \
--hash=sha256:adc3e4442eef57f99b5590b245a328aad19c99552e0bdc7f0b04db6656debd80 \
--hash=sha256:c46767e8df1b7beefb0899c4a95fb43058fa8500b6db144f4ff3ca38eb2f6393 \
--hash=sha256:c619f063c2d68f19b2d7270f4cf3192cb81c9ec5bc5ba02df91471d0b88c4c5c \
--hash=sha256:cf3a4d00e4cdb6734b64bf23cd4341421e8953615cba6b3670453737a72ec204 \
--hash=sha256:cf99f3de8b3273a8317681d8194ea222f10e0133a24a7548c73ce44ea1679377 \
--hash=sha256:d6bc09188020c9ac2555a498949401ab35bb6bf76d4e0f8ee251694664df6301
# via -r requirements.in
click==8.1.7 \
--hash=sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28 \
--hash=sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de
# via black
flake8==6.1.0 \
--hash=sha256:d5b3857f07c030bdb5bf41c7f53799571d75c4491748a3adcd47de929e34cd23 \
--hash=sha256:ffdfce58ea94c6580c77888a86506937f9a1a227dfcd15f245d694ae20a6b6e5
Expand All @@ -40,22 +12,6 @@ mccabe==0.7.0 \
--hash=sha256:348e0240c33b60bbdf4e523192ef919f28cb2c3d7d5c7794f74009290f236325 \
--hash=sha256:6c2d30ab6be0e4a46919781807b4f0d834ebdd6c6e3dca0bda5a15f863427b6e
# via flake8
mypy-extensions==1.0.0 \
--hash=sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d \
--hash=sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782
# via black
packaging==23.2 \
--hash=sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5 \
--hash=sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7
# via black
pathspec==0.11.2 \
--hash=sha256:1d6ed233af05e679efb96b1851550ea95bbb64b7c490b0f5aa52996c11e92a20 \
--hash=sha256:e0d8d0ac2f12da61956eb2306b69f9469b42f4deb0f3cb6ed47b9cce9996ced3
# via black
platformdirs==3.11.0 \
--hash=sha256:cf8ee52a3afdb965072dcc652433e0c7e3e40cf5ea1477cd4b3b1d2eb75495b3 \
--hash=sha256:e9d171d00af68be50e9202731309c4e658fd8bc76f55c11c7dd760d023bda68e
# via black
pycodestyle==2.11.0 \
--hash=sha256:259bcc17857d8a8b3b4a2327324b79e5f020a13c16074670f9c8c8f872ea76d0 \
--hash=sha256:5d1013ba8dc7895b548be5afb05740ca82454fd899971563d2ef625d090326f8
Expand All @@ -64,11 +20,3 @@ pyflakes==3.1.0 \
--hash=sha256:4132f6d49cb4dae6819e5379898f2b8cce3c5f23994194c24b77d5da2e36f774 \
--hash=sha256:a0aae034c444db0071aa077972ba4768d40c830d9539fd45bf4cd3f8f6992efc
# via flake8
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via black
typing-extensions==4.8.0 \
--hash=sha256:8f92fc8806f9a6b641eaa5318da32b44d401efaac0f6678c9bc448ba3605faa0 \
--hash=sha256:df8e4339e9cb77357558cbdbceca33c303714cf861d1eef15e1070055ae8b7ef
# via black
14 changes: 9 additions & 5 deletions example/tools/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ alias(
}),
)

# bazel run :black -- --help
py_console_script_binary(
name = "black",
pkg = "@pip//black:pkg",
alias(
name = "ruff",
actual = select({
"@bazel_tools//src/conditions:linux_x86_64": "@ruff_x86_64-unknown-linux-gnu//:ruff",
"@bazel_tools//src/conditions:linux_aarch64": "@ruff_aarch64-unknown-linux-gnu//:ruff",
"@bazel_tools//src/conditions:darwin_arm64": "@ruff_aarch64-apple-darwin//:ruff",
"@bazel_tools//src/conditions:darwin_x86_64": "@ruff_x86_64-apple-darwin//:ruff",
}),
)

prettier.prettier_binary(
Expand Down Expand Up @@ -77,7 +81,7 @@ multi_formatter_binary(
javascript = ":prettier",
jsonnet = ":jsonnetfmt",
kotlin = ":ktfmt",
python = ":black",
python = ":ruff",
starlark = "@buildifier_prebuilt//:buildifier",
swift = ":swiftformat",
terraform = ":terraform",
Expand Down
2 changes: 1 addition & 1 deletion example/tools/lint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pmd = pmd_aspect(
pmd_test = make_lint_test(aspect = pmd)

ruff = ruff_aspect(
binary = "@@//:ruff",
binary = "@@//tools:ruff",
config = "@@//:.ruff.toml",
)

Expand Down
10 changes: 5 additions & 5 deletions format/private/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ case "$mode" in
check)
swiftmode="--lint"
prettiermode="--check"
blackmode="--check"
ruffmode="format --check"
javamode="--set-exit-if-changed --dry-run"
ktmode="--set-exit-if-changed --dry-run"
gofmtmode="-l"
Expand All @@ -61,7 +61,7 @@ case "$mode" in
fix)
swiftmode=""
prettiermode="--write"
blackmode=""
ruffmode="format"
javamode="--replace"
ktmode=""
gofmtmode="-w"
Expand Down Expand Up @@ -100,10 +100,10 @@ if [ "$#" -eq 0 ]; then
else
files=$(find "$@" -name '*.py' -or -name '*.pyi')
fi
bin=$(rlocation {{black}})
bin=$(rlocation {{ruff}})
if [ -n "$files" ] && [ -n "$bin" ]; then
echo "Running black..."
echo "$files" | tr \\n \\0 | xargs -0 $bin $blackmode
echo "Running ruff..."
echo "$files" | tr \\n \\0 | xargs -0 $bin $ruffmode
fi

if [ "$#" -eq 0 ]; then
Expand Down
4 changes: 2 additions & 2 deletions format/private/formatter_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ load("@aspect_bazel_lib//lib:paths.bzl", "to_rlocation_path")

_attrs = {
"javascript": attr.label(doc = "a binary target that runs prettier", executable = True, cfg = "exec", allow_files = True),
"python": attr.label(doc = "a binary target that runs black", executable = True, cfg = "exec", allow_files = True),
"python": attr.label(doc = "a binary target that runs ruff", executable = True, cfg = "exec", allow_files = True),
"starlark": attr.label(doc = "a binary target that runs buildifier", executable = True, cfg = "exec", allow_files = True),
"jsonnet": attr.label(doc = "a binary target that runs jsonnetfmt", executable = True, cfg = "exec", allow_files = True),
"terraform": attr.label(doc = "a binary target that runs terraform", executable = True, cfg = "exec", allow_files = True),
Expand All @@ -20,7 +20,7 @@ def _formatter_binary_impl(ctx):
# We need to fill in the rlocation paths in the shell script
substitutions = {}
tools = {
"black": ctx.attr.python,
"ruff": ctx.attr.python,
"buildifier": ctx.attr.starlark,
"jsonnetfmt": ctx.attr.jsonnet,
"terraform": ctx.attr.terraform,
Expand Down
70 changes: 35 additions & 35 deletions lint/ruff.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -98,42 +98,42 @@ def ruff_aspect(binary, config):

# Generated by running ./mirror_ruff.sh
RUFF_VERSIONS = {
"v0.1.0": {
"aarch64-apple-darwin": "2e4e91d65d9453705753c0a80d773e2e613f07db7ba75b94edd12eef0e8e268d",
"aarch64-pc-windows-msvc": "a2f66624f000c30c0768ddf2ca55c7437181fdcf937ead004cb7ef8504da2fd5",
"aarch64-unknown-linux-gnu": "63bf7d3eec474d4ee32fac3fd396f6fabb992328eb3fdc22f68ca3c7bdfcb8ee",
"aarch64-unknown-linux-musl": "fba0010be960178866c98c987ab5b6c639c68ccc8f062fc6287c4076ed13b42a",
"armv7-unknown-linux-gnueabihf": "a888123359edb40d08d406ed00c9d378ec1c8a033352ae8dd88fef59fe25f354",
"armv7-unknown-linux-musleabihf": "94ca8c41c8e4c7a75305beb75bd2f594b428228fbcc335607f69695c8a6efdf5",
"i686-pc-windows-msvc": "2c9e7dde93a1d2d458f03c98dbf4c0de1630d1a41a82bbaf6d91d4ffe7114523",
"i686-unknown-linux-gnu": "582fdf2c2fe63be823a4bb6078a88c7e3e13b4146ee94df523a06224cd417d06",
"i686-unknown-linux-musl": "c6e281540dc628b789aee96cd31c219025b32430edb2544f5e75ddcf44d95e61",
"powerpc64-unknown-linux-gnu": "d4f4f4e8675879e01086667319955a5a36d0183fe8a86227f658f5fb01936bb1",
"powerpc64le-unknown-linux-gnu": "076cc6973de196d05673f6671f68ee4110e1475fc8cb2cbbae09aad688ed7c82",
"s390x-unknown-linux-gnu": "31061944ad922b7aa2cc4f7fae5a4a29abb72d5ee64538f0297eec5f20fcc4dd",
"x86_64-apple-darwin": "f7d89c2f6b9b62ffd8f1ec0f5f9344a338acf560971b8a1ba22a2a65c94d1920",
"x86_64-pc-windows-msvc": "a6974efb00ff6b162927691e616b45223195444a1fc28886ded8faca825312be",
"x86_64-unknown-linux-gnu": "4d669faf7d0964cde56589ad626f45a27ca41f2c91671b3a5aafb5d1c7bcd2e1",
"x86_64-unknown-linux-musl": "867bf01477096ae8a2b20758e189fe8fa8f0b53a6b8d090066724bc37d6df61e",
},
"v0.0.292": {
"aarch64-apple-darwin": "70807eaab0f081ba0fd17fde56e860faa7bfb4a938bcadd829eb96d648eca8ac",
"aarch64-pc-windows-msvc": "bbdf4aa474899196290734f45dabebc4faf796039367125c171ec017f7bcfa85",
"aarch64-unknown-linux-gnu": "08b842e3cfe3c60e50ff0ebc07cf099b8fd0d881e3d40bd89b3a6d7722a058e2",
"aarch64-unknown-linux-musl": "7dd8e7aacfb7217bd672efec59aa55711fad36b149181d6eb23468fab3f74c40",
"armv7-unknown-linux-gnueabihf": "476c62c7a844fac476311f34ccdf194e9d77059d098352459490783825e293e7",
"armv7-unknown-linux-musleabihf": "a28175dfe75d38f54c39df92f16c4ba4ff84a4e5a9b81351c91246ab0f6af1f4",
"i686-pc-windows-msvc": "3dec6513e8bcbbee8a57c868acd808eb1bccd9e00b07e35c1ba70ff78fb84224",
"i686-unknown-linux-gnu": "a7f47a1d8d599208743bfe9f4f3a96ebb65771d5923e0e68b0394c1c41bb66c0",
"i686-unknown-linux-musl": "d7185d63840cae1a89bc63747d90f958e415bd5798a2996004481ebbd4a935bf",
"powerpc64-unknown-linux-gnu": "7b6aa52afb3a28257825a544b0a23b1675d70c5ff9fde12dce4721c28a0e9980",
"powerpc64le-unknown-linux-gnu": "cbf3454356b6245c8afadc7b2f02fe442c3f40c0cae7db7987f784a4559b2bee",
"s390x-unknown-linux-gnu": "0dc6c8bd253d1f7d7fd887c9cf557c57d581989003d0bbef640d55236164f8a6",
"x86_64-apple-darwin": "d9123a94d7d0fce2140acd7cb86f83e2b24b29d031b3a84956238e5ce2d15f42",
"x86_64-pc-windows-msvc": "76f1fd5b40a9325a7638f0a5d1d260aa7c10b9d63456ab6d8345afa68db907fe",
"x86_64-unknown-linux-gnu": "39a488a2e21d3bca379ba6ee2ec5cf2d641d578efb4b16c27c90272138dfa37d",
"x86_64-unknown-linux-musl": "bb43ca7bb678bc995abbdfa005dfdd084061851c53e9c8cea9813478326eca50",
"v0.1.2": {
"aarch64-apple-darwin": "99a245e703c1d95afc9b0c2e00bd47bca6ac5c17e6cc046e58bf3f70d6536330",
"aarch64-pc-windows-msvc": "644390067887b8b0f1bff2805211de6bd006c97ce259760260b27da5356a7db6",
"aarch64-unknown-linux-gnu": "fef09ce9591c72efd1ef4ce2b0956f1edba21328e29ad865e795d16186a20a06",
"aarch64-unknown-linux-musl": "a96338c7e96228531a0c2cef39ebe17a215fca3346e2352a00b9f88bc25d5197",
"armv7-unknown-linux-gnueabihf": "a15894de509ae15eeb48d0121e08536b1b8f16855d88cccb61ecab0c9f4e537d",
"armv7-unknown-linux-musleabihf": "1c6bf703c489d60ae1db402508d1527292411d9c4fac5d43cf79f08e444e6167",
"i686-pc-windows-msvc": "aca5b93beac3a76edd7c2ea55f0e09672d2d7f85fcf5c9f4f65c252db643b5b4",
"i686-unknown-linux-gnu": "fa42d9559589cee38f8ac34c48ce3f9981d204fa2be48fa72945e9c1f026db8c",
"i686-unknown-linux-musl": "f84d9770205f3b0387ba74dbc62a47f2b234f52fc58ec0e9b8ceb0697035a6cf",
"powerpc64-unknown-linux-gnu": "b8731842d1d762538a8d41e49853cac4b06b827b0f762bd08e951eb47ec2129c",
"powerpc64le-unknown-linux-gnu": "36015e708b571465e12ecb655e8165e51ee1d7295800484b94ab21e04c2b17b1",
"s390x-unknown-linux-gnu": "592fc30494bf63ddb510ac929233a6fa44488273099e064fcf91efc10a3c49ab",
"x86_64-apple-darwin": "cb339e9684043dae3cf530bca22f532198b9f151b01812f701ed0b85fec95b0a",
"x86_64-pc-windows-msvc": "5e515c69653469736e76848f401d9f72b4396d8b7be2be77f748109e04542d26",
"x86_64-unknown-linux-gnu": "cab189b03ed0d882d45691d6cb4a7843e9d8389ba4e2e43cb4616a46ae69545b",
"x86_64-unknown-linux-musl": "368594d2e96a8a7e14fc610259f1c0a29fab4a84cd061a9373ce415c742df04e"
},
"v0.1.1": {
"aarch64-apple-darwin": "e2444e4c7bda7133a0c47a368993be55f7bbe3c3a68d14be83dca480fe624c5e",
"aarch64-pc-windows-msvc": "8eb1f83b0e3c9ddd818176fc26065b87b7cb119764c84b109c70d12dff283d21",
"aarch64-unknown-linux-gnu": "2d56dd602aa62e5e8185971e19c77d6df1e9f14a9190176d8f8212dcde827dde",
"aarch64-unknown-linux-musl": "ee1117ea3e4c32c92f598592c946bb941cca437cc0ce62bd4e86ea889a081e29",
"armv7-unknown-linux-gnueabihf": "f0f0b4db10954496149ddb7b6a9d7c16418f386210f3da2823904bde713ba369",
"armv7-unknown-linux-musleabihf": "894e6f0c5f8a8830fcddf38afa81960fb902211082ef7a1aec00637a2c6e2710",
"i686-pc-windows-msvc": "a6d5601c9704790409a3540d6dc6cc2a955b234f07df9553e5b4555f9b8ac7e5",
"i686-unknown-linux-gnu": "9617cd3a921f9d65be973482fae5b888e9e1a9617e779d7d47f3d5f2cdb5c332",
"i686-unknown-linux-musl": "55d574637179362a2ddf10125d0386054a0cfb3daef0943e9082a87a1a3424e6",
"powerpc64-unknown-linux-gnu": "143b227ace509cb0a2e2411a5675935d67910f7a280f35931e112e137dc3b894",
"powerpc64le-unknown-linux-gnu": "94158cc4d4208b29962a6be2245fa391ce158673c4e8c8baa5e9f592c080dc29",
"s390x-unknown-linux-gnu": "22dcfb5338e546713b86acc1c25d0c11bb72175df652bbd0ad58082c4e273b8b",
"x86_64-apple-darwin": "da0e3156387a419db6c123fd7d009d7f6153dfb631a17e7aa1551f4d6aec5c7a",
"x86_64-pc-windows-msvc": "080877770e3494637a01cca05e88ed9a5a450f6129b90053e7f3014ce761f178",
"x86_64-unknown-linux-gnu": "0f70840c8c10ac8013f734e76977494311b7264a12782bc95be5f9d2e1414176",
"x86_64-unknown-linux-musl": "2b5555abb21598969be862a05101f35079ff6c72466d74eb22f9a1c773d646d0"
}
}

def fetch_ruff(version = RUFF_VERSIONS.keys()[0]):
Expand Down

0 comments on commit e4cfbf4

Please sign in to comment.