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

Hacking around with transitive dep imports #24

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions mypy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ def _extract_srcs(srcs):

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


def _extract_stub_deps(deps):
Expand All @@ -88,7 +90,7 @@ def _extract_stub_deps(deps):
return stub_files


def _extract_imports(imports, label):
def _extract_imports(imports, label, transitive_imports):
# 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 = []
Expand All @@ -99,6 +101,10 @@ def _extract_imports(imports, label):
mypypath_parts.append(label.package)
else:
mypypath_parts.append("{}/{}".format(label.package, import_))
for transitive_import_ in transitive_imports:
new_import = transitive_import_.replace("__main__/", "")
if new_import not in mypypath_parts:
mypypath_parts.append(new_import)
return mypypath_parts


Expand All @@ -118,11 +124,13 @@ def _mypy_rule_impl(ctx, is_aspect = False):
direct_src_files = _extract_srcs(base_rule.attr.srcs)

if hasattr(base_rule.attr, "deps"):
transitive_srcs_depsets = _extract_transitive_deps(base_rule.attr.deps)
transitive_srcs_depsets, transitive_srcs_imports = _extract_transitive_deps(base_rule.attr.deps)
stub_files = _extract_stub_deps(base_rule.attr.deps)

base_imports = []
if hasattr(base_rule.attr, "imports"):
mypypath_parts = _extract_imports(base_rule.attr.imports, ctx.label)
base_imports = base_rule.attr.imports
mypypath_parts = _extract_imports(base_imports, ctx.label, transitive_srcs_imports)

final_srcs_depset = depset(transitive = transitive_srcs_depsets +
[depset(direct = direct_src_files)])
Expand All @@ -133,6 +141,9 @@ def _mypy_rule_impl(ctx, is_aspect = False):
mypypath_parts += [src_f.dirname for src_f in stub_files]
mypypath = ":".join(mypypath_parts)

print("Source files: ", direct_src_files)
print("Path: ", mypypath)

# Ideally, a file should be passed into this rule. If this is an executable
# rule, then we default to the implicit executable file, otherwise we create
# a stub.
Expand Down