Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[py] Format the code using black #13679

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ load("@apple_rules_lint//lint:setup.bzl", "lint_setup")
# Add your linters here.
lint_setup({
"java-spotbugs": "//java:spotbugs-config",
"py-black": "//py:black-config",
"rust-rustfmt": "//rust:enable-rustfmt",
})

Expand Down
13 changes: 10 additions & 3 deletions py/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ load("@bazel_skylib//rules:select_file.bzl", "select_file")
load("@py_dev_requirements//:requirements.bzl", "requirement")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_pkg//pkg:pkg.bzl", "pkg_tar")
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@rules_python//python:packaging.bzl", "py_package", "py_wheel")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("//common:defs.bzl", "copy_file")
load("//py:defs.bzl", "generate_devtools", "py_test_suite")
load("//py:defs.bzl", "black_config", "generate_devtools", "py_binary", "py_import", "py_library", "py_test_suite")
load("//py/private:browsers.bzl", "BROWSERS")
load("//py/private:import.bzl", "py_import")

black_config(
name = "black-config",
line_length = 120,
visibility = ["//visibility:public"],
)

alias(
name = "twine",
Expand All @@ -23,6 +27,9 @@ genrule(
],
outs = ["pypi_upload_complete.txt"],
cmd = "(twine upload $(location :selenium-wheel) $(location :selenium-sdist) && touch $@)",
tags = [
"manual",
],
tools = [":twine"],
)

Expand Down
5 changes: 5 additions & 0 deletions py/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
load("//py/private:generate_devtools.bzl", _generate_devtools = "generate_devtools")
load("//py/private:import.bzl", _py_import = "py_import")
load("//py/private:pytest.bzl", _pytest_test = "pytest_test")
load("//py/private:py_with_lint_macro.bzl", _py_binary = "py_binary", _py_library = "py_library")
load("//py/private:suite.bzl", _py_test_suite = "py_test_suite")
load("//py/private:black_config.bzl", _black_config = "black_config")

black_config = _black_config
generate_devtools = _generate_devtools
pytest_test = _pytest_test
py_binary = _py_binary
py_library = _py_library
py_import = _py_import
py_test_suite = _py_test_suite
21 changes: 20 additions & 1 deletion py/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
load("@py_dev_requirements//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_binary")

package(default_visibility = ["//visibility:public"])

py_binary(
name = "untar",
srcs = [
"untar.py",
],
legacy_create_init = False,
visibility = ["//visibility:public"],
)

alias(
name = "black",
actual = ":black-bin",
)

py_binary(
name = "black-bin",
srcs = [
# If the source is called `black` you can't then import black into black
"black-bin.py",
],
legacy_create_init = False,
deps = [
requirement("black"),
],
)
25 changes: 25 additions & 0 deletions py/private/black-bin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import re
import sys

from black import patched_main

if __name__ == "__main__":
sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0])
sys.exit(patched_main())
31 changes: 31 additions & 0 deletions py/private/black_config.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
BlackConfigInfo = provider(
fields = {
"black": "binary: The binary to execute for black",
"line_length": "`int`: Maximum line length",
"python_version": "`array of strings`: The versions of Python to target",
},
)

def black_config_impl(ctx):
return [
BlackConfigInfo(
black = ctx.attr.black,
line_length = ctx.attr.line_length,
python_version = ctx.attr.python_version,
),
]

black_config = rule(
black_config_impl,
attrs = {
"black": attr.label(
cfg = "exec",
default = "//py/private:black",
executable = True,
),
"line_length": attr.int(
default = 88,
),
"python_version": attr.string_list(),
},
)
63 changes: 63 additions & 0 deletions py/private/black_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
load(":black_config.bzl", "BlackConfigInfo")

def black_test_impl(ctx):
config = ctx.attr.config[BlackConfigInfo]

all_inputs = []
black = config.black[DefaultInfo].files_to_run.executable
all_inputs.append(black)

args = ctx.actions.args()

for version in config.python_version:
args.add_all(["-t", version])

args.add_all(["--line-length", config.line_length])

args.add("--check")

args.add_all(ctx.files.srcs)
all_inputs.extend(ctx.files.srcs)

# Run on a single core
args.add_all(["--workers", "1"])

if config.python_version:
args.add_all(["--target-version", config.python_version])

args.use_param_file("@%s", use_always = True)
args_file = ctx.actions.declare_file("%s-black-params" % ctx.label.name)
ctx.actions.write(args_file, args)
all_inputs.append(args_file)

output = ctx.actions.declare_file("%s-black-test" % ctx.label.name)
ctx.actions.write(
output = output,
content = """#!/usr/bin/env bash
%s $(< %s) """ % (black.short_path, args_file.short_path),
is_executable = True,
)

return [DefaultInfo(
executable = output,
files = depset(),
runfiles = ctx.runfiles(files = all_inputs).merge(
config.black[DefaultInfo].default_runfiles,
),
)]

black_test = rule(
black_test_impl,
attrs = {
"srcs": attr.label_list(
allow_files = [".py"],
),
"config": attr.label(
mandatory = True,
providers = [
[BlackConfigInfo],
],
),
},
test = True,
)
34 changes: 34 additions & 0 deletions py/private/py_with_lint_macro.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
load("@apple_rules_lint//lint:defs.bzl", "get_lint_config")
load("@rules_python//python:defs.bzl", _py_binary = "py_binary", _py_library = "py_library", _py_test = "py_test")
load(":black_test.bzl", "black_test")

def create_lint_tests(name, **kwargs):
config = get_lint_config("py-black", kwargs.get("tags", []))
if config:
black_test(
name = "%s-black" % name,
srcs = kwargs.get("srcs", []),
config = config,
tags = kwargs.get("tags", []) + [
"lint",
"black",
"py-black",
],
)

def _add_lint_tests(actual, name, **kwargs):
actual(
name = name,
**kwargs
)

create_lint_tests(name, **kwargs)

def py_binary(name, **kwargs):
_add_lint_tests(_py_binary, name, **kwargs)

def py_library(name, **kwargs):
_add_lint_tests(_py_library, name, **kwargs)

def py_test(name, **kwargs):
_add_lint_tests(_py_test, name, **kwargs)
6 changes: 6 additions & 0 deletions py/private/pytest.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_python//python:defs.bzl", "PyInfo", "py_test")
load(":py_with_lint_macro.bzl", "create_lint_tests")

def _stringify(paths):
return repr(paths)
Expand Down Expand Up @@ -59,6 +60,11 @@ _pytest_runner = rule(
def pytest_test(name, srcs, deps = None, args = None, data = None, python_version = None, **kwargs):
runner_target = "%s-runner.py" % name

create_lint_tests(
name = name,
srcs = srcs,
)

_pytest_runner(
name = runner_target,
testonly = True,
Expand Down
1 change: 1 addition & 0 deletions py/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
async-generator==1.10
attrs==23.1.0
black==24.2.0
certifi==2023.7.22
cffi==1.16.0
cryptography==42.0.4
Expand Down
46 changes: 45 additions & 1 deletion py/requirements_lock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ attrs==23.1.0 \
# -r py/requirements.txt
# outcome
# trio
black==24.2.0 \
--hash=sha256:057c3dc602eaa6fdc451069bd027a1b2635028b575a6c3acfd63193ced20d9c8 \
--hash=sha256:08654d0797e65f2423f850fc8e16a0ce50925f9337fb4a4a176a7aa4026e63f8 \
--hash=sha256:163baf4ef40e6897a2a9b83890e59141cc8c2a98f2dda5080dc15c00ee1e62cd \
--hash=sha256:1e08fb9a15c914b81dd734ddd7fb10513016e5ce7e6704bdd5e1251ceee51ac9 \
--hash=sha256:4dd76e9468d5536abd40ffbc7a247f83b2324f0c050556d9c371c2b9a9a95e31 \
--hash=sha256:4f9de21bafcba9683853f6c96c2d515e364aee631b178eaa5145fc1c61a3cc92 \
--hash=sha256:61a0391772490ddfb8a693c067df1ef5227257e72b0e4108482b8d41b5aee13f \
--hash=sha256:6981eae48b3b33399c8757036c7f5d48a535b962a7c2310d19361edeef64ce29 \
--hash=sha256:7e53a8c630f71db01b28cd9602a1ada68c937cbf2c333e6ed041390d6968faf4 \
--hash=sha256:810d445ae6069ce64030c78ff6127cd9cd178a9ac3361435708b907d8a04c693 \
--hash=sha256:93601c2deb321b4bad8f95df408e3fb3943d85012dddb6121336b8e24a0d1218 \
--hash=sha256:992e451b04667116680cb88f63449267c13e1ad134f30087dec8527242e9862a \
--hash=sha256:9db528bccb9e8e20c08e716b3b09c6bdd64da0dd129b11e160bf082d4642ac23 \
--hash=sha256:a0057f800de6acc4407fe75bb147b0c2b5cbb7c3ed110d3e5999cd01184d53b0 \
--hash=sha256:ba15742a13de85e9b8f3239c8f807723991fbfae24bad92d34a2b12e81904982 \
--hash=sha256:bce4f25c27c3435e4dace4815bcb2008b87e167e3bf4ee47ccdc5ce906eb4894 \
--hash=sha256:ca610d29415ee1a30a3f30fab7a8f4144e9d34c89a235d81292a1edb2b55f540 \
--hash=sha256:d533d5e3259720fdbc1b37444491b024003e012c5173f7d06825a77508085430 \
--hash=sha256:d84f29eb3ee44859052073b7636533ec995bd0f64e2fb43aeceefc70090e752b \
--hash=sha256:e37c99f89929af50ffaf912454b3e3b47fd64109659026b678c091a4cd450fb2 \
--hash=sha256:e8a6ae970537e67830776488bca52000eaa37fa63b9988e8c487458d9cd5ace6 \
--hash=sha256:faf2ee02e6612577ba0181f4347bcbcf591eb122f7841ae5ba233d12c39dcb4d
# via -r py/requirements.txt
certifi==2023.7.22 \
--hash=sha256:539cc1d13202e33ca466e88b2807e29f4c13049d6d87031a3c110744495cb082 \
--hash=sha256:92d6037539857d8206b8f6ae472e8b77db8058fec5937a1ef3f54304089edbb9
Expand Down Expand Up @@ -172,6 +196,10 @@ charset-normalizer==3.3.2 \
--hash=sha256:fd1abc0d89e30cc4e02e4064dc67fcc51bd941eb395c502aac3ec19fab46b519 \
--hash=sha256:ff8fa367d09b717b2a17a052544193ad76cd49979c805768879cb63d9ca50561
# via requests
click==8.1.7 \
--hash=sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28 \
--hash=sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de
# via black
cryptography==42.0.4 \
--hash=sha256:01911714117642a3f1792c7f376db572aadadbafcd8d75bb527166009c9f1d1b \
--hash=sha256:0e89f7b84f421c56e7ff69f11c441ebda73b8a8e6488d322ef71746224c20fce \
Expand Down Expand Up @@ -359,6 +387,10 @@ multidict==6.0.2 \
--hash=sha256:feba80698173761cddd814fa22e88b0661e98cb810f9f986c54aa34d281e4937 \
--hash=sha256:feea820722e69451743a3d56ad74948b68bf456984d63c1a92e8347b7b88452d
# via -r py/requirements.txt
mypy-extensions==1.0.0 \
--hash=sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d \
--hash=sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782
# via black
nh3==0.2.15 \
--hash=sha256:0d02d0ff79dfd8208ed25a39c12cbda092388fff7f1662466e27d97ad011b770 \
--hash=sha256:3277481293b868b2715907310c7be0f1b9d10491d5adf9fce11756a97e97eddf \
Expand Down Expand Up @@ -389,11 +421,20 @@ packaging==23.2 \
--hash=sha256:8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7
# via
# -r py/requirements.txt
# black
# pytest
pathspec==0.12.1 \
--hash=sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08 \
--hash=sha256:a482d51503a1ab33b1c67a6c3813a26953dbdc71c31dacaef9a838c4e29f5712
# via black
pkginfo==1.9.6 \
--hash=sha256:4b7a555a6d5a22169fcc9cf7bfd78d296b0361adad412a346c1226849af5e546 \
--hash=sha256:8fd5896e8718a4372f0ea9cc9d96f6417c9b986e23a4d116dda26b62cc29d046
# via twine
platformdirs==4.2.0 \
--hash=sha256:0614df2a2f37e1a662acbd8e2b25b92ccf8632929bc6d43467e17fe89c75e068 \
--hash=sha256:ef0cc731df711022c174543cb70a9b5bd22e5a9337c8624ef2c2ceb8ddad8768
# via black
pluggy==1.3.0 \
--hash=sha256:cf61ae8f126ac6f7c451172cf30e3e43d3ca77615509771b3a984a0730651e12 \
--hash=sha256:d89c696a773f8bd377d18e5ecda92b7a3793cbe66c87060a6fb58c7b6e1061f7
Expand Down Expand Up @@ -492,7 +533,9 @@ toml==0.10.2 \
tomli==2.0.1 \
--hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \
--hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f
# via pytest
# via
# black
# pytest
trio==0.22.0 \
--hash=sha256:ce68f1c5400a47b137c5a4de72c7c901bd4e7a24fbdebfe9b41de8c6c04eaacf \
--hash=sha256:f1dd0780a89bfc880c7c7994519cb53f62aacb2c25ff487001c0052bd721cdf0
Expand All @@ -513,6 +556,7 @@ typing-extensions==4.9.0 \
--hash=sha256:af72aea155e91adfc61c3ae9e0e342dbc0cba726d6cba4b6c72c1f34e47291cd
# via
# -r py/requirements.txt
# black
# rich
urllib3[socks]==2.0.7 \
--hash=sha256:c97dfde1f7bd43a71c8d2a58e369e9b2bf692d1334ea9f9cae55add7d0dd0f84 \
Expand Down
5 changes: 5 additions & 0 deletions scripts/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ section "Java"
echo " google-java-format" >&2
find "$PWD/java" -type f -name '*.java' | xargs "$GOOGLE_JAVA_FORMAT" --replace

section "Python"
echo " black" >&2
# Keep the flags here in sync with what we have in `//py:black-config`
bazel run //py/private:black -- --line-length 120 "$(pwd)/py"

section "Rust"
echo " rustfmt" >&2
bazel run @rules_rust//:rustfmt
Expand Down