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

Add support for generated files with custom imports #40

Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions examples/demo/generated_file_imports/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
py_binary(
name = "foo",
srcs = ["foo.py"],
deps = [
"//generated_file_imports/nested:bar",
],
)
3 changes: 3 additions & 0 deletions examples/demo/generated_file_imports/foo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from bar import foo

foo()
12 changes: 12 additions & 0 deletions examples/demo/generated_file_imports/nested/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
genrule(
name = "generate_source",
outs = ["bar.py"],
cmd = "echo 'def foo(): print(\"hi\")' > $(OUTS)",
)

py_library(
name = "bar",
srcs = ["bar.py"],
imports = ["."],
visibility = ["//visibility:public"],
)
6 changes: 5 additions & 1 deletion mypy/private/mypy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ def _mypy_impl(target, ctx):
# file roots?

unique_generated_dirs = generated_dirs.keys()
generated_custom_imports = []
for generated_dir in unique_generated_dirs:
for custom_import in custom_imports:
generated_custom_imports.append("{}/{}".format(generated_dir, custom_import))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess some of these may just not exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea. I figured that shouldn't hurt

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy to roll with it -- only a mild concern about potential size of the env var, but that's a problem for another time


# types need to appear first in the mypy path since the module directories
# are the same and mypy resolves the first ones, first.
mypy_path = ":".join(types + external_deps + custom_imports + unique_generated_dirs)
mypy_path = ":".join(types + external_deps + custom_imports + unique_generated_dirs + generated_custom_imports)

output_file = ctx.actions.declare_file(ctx.rule.attr.name + ".mypy_stdout")

Expand Down