Skip to content

Commit

Permalink
Make the repo buildifier-clean
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Apr 14, 2022
1 parent 901ed5c commit b8045bc
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 31 deletions.
11 changes: 7 additions & 4 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ package(default_visibility = ["//visibility:private"])

buildifier(
name = "buildifier.check",
exclude_patterns = [
"./.git/*",
],
lint_mode = "warn",
mode = "diff",
)

buildifier(
name = "buildifier",
lint_mode = "fix",
mode = "fix",
)

licenses(["notice"]) # MIT

exports_files([
Expand All @@ -26,9 +29,9 @@ filegroup(
"BUILD",
"LICENSE",
"config.bzl",
"current_mypy_version.txt",
"mypy.bzl",
"rules.bzl",
"current_mypy_version.txt",
"//mypy:distribution",
"//repositories:distribution",
"//templates:distribution",
Expand Down
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ http_archive(

http_archive(
name = "rules_pkg",
sha256 = "4ba8f4ab0ff85f2484287ab06c0d871dcb31cc54d439457d28fd4ae14b18450a",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.2.4/rules_pkg-0.2.4.tar.gz",
"https://github.com/bazelbuild/rules_pkg/releases/download/0.2.4/rules_pkg-0.2.4.tar.gz",
],
sha256 = "4ba8f4ab0ff85f2484287ab06c0d871dcb31cc54d439457d28fd4ae14b18450a",
)
2 changes: 2 additions & 0 deletions examples/tools/linting/aspect.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"Shows how to hook up a linter"

load("@linting_system//:generator.bzl", "linting_aspect_generator")

lint = linting_aspect_generator(
Expand Down
16 changes: 5 additions & 11 deletions mypy.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"Public API"

load("@bazel_skylib//lib:shell.bzl", "shell")
load("@bazel_skylib//lib:sets.bzl", "sets")
load("//:rules.bzl", "MyPyStubsInfo")

MyPyAspectInfo = provider(
"TODO: documentation",
fields = {
"exe": "Used to pass the rule implementation built exe back to calling aspect.",
"out": "Used to pass the dummy output file back to calling aspect.",
Expand Down Expand Up @@ -31,7 +34,6 @@ DEFAULT_ATTRS = {
),
}


def _sources_to_cache_map_triples(srcs, is_aspect):
triples_as_flat_list = []
for f in srcs:
Expand All @@ -49,15 +51,12 @@ def _sources_to_cache_map_triples(srcs, is_aspect):
])
return triples_as_flat_list


def _is_external_dep(dep):
return dep.label.workspace_root.startswith("external/")


def _is_external_src(src_file):
return src_file.path.startswith("external/")


def _extract_srcs(srcs):
direct_src_files = []
for src in srcs:
Expand All @@ -66,15 +65,13 @@ def _extract_srcs(srcs):
direct_src_files.append(f)
return direct_src_files


def _extract_transitive_deps(deps):
transitive_deps = []
for dep in deps:
if MyPyStubsInfo not in dep and PyInfo in dep and not _is_external_dep(dep):
transitive_deps.append(dep[PyInfo].transitive_sources)
return transitive_deps


def _extract_stub_deps(deps):
# Need to add the .py files AND the .pyi files that are
# deps of the rule
Expand All @@ -87,21 +84,20 @@ def _extract_stub_deps(deps):
stub_files.append(src_f)
return stub_files


def _extract_imports(imports, label):
# NOTE: Bazel's implementation of this for py_binary, py_test is at
# src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonSemantics.java
mypypath_parts = []
for import_ in imports:
if import_.startswith("/"):
# buildifier: disable=print
print("ignoring invalid absolute path '{}'".format(import_))
elif import_ in ["", "."]:
mypypath_parts.append(label.package)
else:
mypypath_parts.append("{}/{}".format(label.package, import_))
return mypypath_parts


def _mypy_rule_impl(ctx, is_aspect = False):
base_rule = ctx
if is_aspect:
Expand Down Expand Up @@ -192,8 +188,7 @@ def _mypy_rule_impl(ctx, is_aspect = False):
]
return DefaultInfo(executable = exe, runfiles = runfiles)


def _mypy_aspect_impl(target, ctx):
def _mypy_aspect_impl(_, ctx):
if (ctx.rule.kind not in ["py_binary", "py_library", "py_test", "mypy_test"] or
ctx.label.workspace_root.startswith("external")):
return []
Expand Down Expand Up @@ -223,7 +218,6 @@ def _mypy_aspect_impl(target, ctx):
),
]


def _mypy_test_impl(ctx):
info = _mypy_rule_impl(ctx, is_aspect = False)
if not info:
Expand Down
3 changes: 2 additions & 1 deletion repositories/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ repository.

load(":py_repositories.bzl", "py_deps")

def deps(mypy_requirements_file, python_interpreter=None, python_interpreter_target=None, extra_pip_args=None):
def deps(mypy_requirements_file, python_interpreter = None, python_interpreter_target = None, extra_pip_args = None):
"""Pull in external dependencies needed by rules in this repo.
Pull in all dependencies needed to run rules in this
repository.
Expand Down
2 changes: 2 additions & 0 deletions repositories/py_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ Provides functions to pull the external Mypy package dependency.

load("@rules_python//python:pip.bzl", "pip_install")

# buildifier: disable=function-docstring-args
def py_deps(mypy_requirements_file, python_interpreter, python_interpreter_target, extra_pip_args):
"""Pull in external Python packages needed by py binaries in this repo.
Pull in all dependencies needed to build the Py binaries in this
repository. This function assumes the repositories imported by the macro
'repositories' in //repositories:repositories.bzl have been imported
Expand Down
7 changes: 2 additions & 5 deletions repositories/repositories.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
"""Rules to load all dependencies of this project."""

load(
"@bazel_tools//tools/build_defs/repo:http.bzl",
"http_archive",
"http_file",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# buildifier: disable=function-docstring
def repositories():
excludes = native.existing_rules().keys()

Expand Down
2 changes: 2 additions & 0 deletions rules.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"mypy_stubs rule"
MyPyStubsInfo = provider(
"TODO: docs",
fields = {
"srcs": ".pyi stub files",
},
Expand Down
18 changes: 12 additions & 6 deletions test/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package(default_testonly = 1)

load("//:mypy.bzl", "mypy_test")

package(default_testonly = 1)

py_library(
name = "empty_srcs_lib",
srcs = [],
Expand All @@ -22,13 +22,19 @@ py_library(

py_library(
name = "correct_imported_mypy_typings",
srcs = [":correct_generated_mypy_typings", "correct_imported_typings.py"],
srcs = [
"correct_imported_typings.py",
":correct_generated_mypy_typings",
],
srcs_version = "PY3",
)

py_library(
name = "broken_imported_mypy_typings",
srcs = [":broken_generated_mypy_typings", "broken_imported_typings.py"],
srcs = [
"broken_imported_typings.py",
":broken_generated_mypy_typings",
],
srcs_version = "PY3",
)

Expand Down Expand Up @@ -60,21 +66,21 @@ mypy_test(
genrule(
name = "correct_generated_mypy_typings",
outs = ["correct_generated.py"],
local = True,
cmd =
("echo -e '" +
"from typing import Callable\n\n" +
"def thrice(i: int, next_: Callable[[int], int]) -> int:\n" +
" return next_(next_(next_(i)))\n' > $@"),
local = True,
)

genrule(
name = "broken_generated_mypy_typings",
outs = ["broken_generated.py"],
local = True,
cmd =
("echo -e '" +
"from typing import Callable\n\n" +
"def thrice(i: int, next_: Callable[[int], int]) -> str:\n" +
" return next_(next_(next_(i)))\n' > $@"),
local = True,
)
4 changes: 2 additions & 2 deletions test/foo/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "foo",
srcs = ["bar.py"],
srcs_version = "PY3",
visibility = ["//test:__subpackages__"],
deps = [
"//test/types",
],
srcs_version = "PY3",
visibility = ["//test:__subpackages__"],
)
2 changes: 1 addition & 1 deletion test/types/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ load("@rules_python//python:defs.bzl", "py_library")
py_library(
name = "types",
srcs = ["__init__.py"],
deps = [],
srcs_version = "PY3",
visibility = ["//test:__subpackages__"],
deps = [],
)

0 comments on commit b8045bc

Please sign in to comment.